Unrecognized function or variable " " , when I use my function
Show older comments
Hi,
I write some function that will make a fit for my data. this is my function:
function Xmax = fit_function(x,y)
X1 = x(x>0.5 & x<2);
Y1 = y(x>0.5 & x<2);
% ymax calculated analytically with Wolfram Mathematica
ymax = @(b) (2-b(2)/b(1))*(2*b(1)/b(2)-1)^(-b(2)/2/b(1));
modelfun = @(b,x) b(3)/ymax(b)*exp((x-b(4))*(b(1)-b(2))).*sech(b(1)*(x-b(4)));
bguess=[78, 10, peaks(1),x(t_peaks(1))];
% [alpha, beta, amplitude, x offset]
% 78 and 10 were calculated by cftool (after given there a guess of 80,20)
beta = nlinfit(X1, Y1, modelfun, bguess);
fittedCurve = modelfun(beta, X1);
Xmax=log(-1+(2*beta(1)/beta(2)));
when I apply this function on some data, I have a problem in the line of bguess.
The function is able to read the "peaks(1)" variable, but not the t_peaks(1) variable. both of them save in the workspace, and slao both of them is double file.
as I say in the summary, the error is:
>> Xmax=fit_function(x,y);
Unrecognized function or variable 't_peaks'.
Error in fit_function (line 12)
bguess=[78, 10, peaks(1), x(t_peaks(1))];
why is append?
It's strange that it can read one of the variables and not the other..
Thanks!(:
Accepted Answer
More Answers (1)
The variable t_peaks is not defined in the fit_function. Neither is the peaks variable but this is not throwing an error because peaks is also a MATLAB function.
To fix this, you should define peaks and t_peaks in the function or pass those values in as input arguments.
If it isn't clear why this is an issue, suppose I ask you what c equals in the c=b+9. You can't answer the question without knowing what b is. MATLAB needs to know what t_peaks. MATLAB also needs to know that peaks is a variable and not a function. By setting the value for peaks or passing it in as an input argument, MATLAB will know that it's a variable and it won't call the peaks() function.
MATLAB's peaks function with an input of 1:
t = peaks(1)
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!