I have a short script to optimize a four-coefficient nonlinear equation. When I run the script, lsqnonlin reports an error that seems to fly in the face of the direct MATLAB help file about how to run lsqnonlin.
A=5.5;B=0;C=1;p=2;TM=1;x0=[A,B,C,p];%coefficients of the function.
options = optimset('MaxFunEvals',10000,'MaxIter',10000,'TolFun',1e-12,'TolX',1e-12);x=lsqnonlin(@Error_Curve,x0,[0 0 -1000 0],[1000 1000 1 100],options,I,t);% Calculate the new coefficients using LSQNONLIN.
The vectors I and t are defined previously in the file, but they're long and pointless to include here. Also, my function is defined as follows:
function F = Error_Curve(x,X,Y) A = x(1); B = x(2); C = x(3); p = x(4); tt = A./((X./IM).^p-C)+B; F = (log10(tt)-log10(Y)).^2; end
When the script gets down to run lsqnonlin, I get the following error:
Undefined function 'lsqnonlin' for input arguments of type 'function_handle'.Error in CurveFitting_Fuses (line 79)x=lsqnonlin(@Error_Curve,x0,[0 0 -1000 0],[1000 1000 1 100],options,I,t)
I'd expect this if I didn't have the toolbox installed. But I DO have the toolbox installed (version 7.0 with Matlab R2014a).
>> exist('lsqnonlin')ans = 2
I also receive the help information when I ask for it
>> help lsqnonlin lsqnonlin solves non-linear least squares problems. lsqnonlin attempts to solve problems of the form: min sum {FUN(X).^2} where X and the values returned by FUN can be X vectors or matrices.
This tells me that I have the function. So, I copied the example from the tutorial in the help documentation, but I get the same result.
% SCRIPT optimizer_tutorial.m
x0 = [0.3 0.4] % Starting guess
[x,resnorm] = lsqnonlin(@myfun,x0); % Invoke optimizer
function F = myfun(x) k = 1:10; F = 2 + 2*k-exp(k*x(1))-exp(k*x(2));end>> optimizer_tutorialx0 = 0.3000 0.4000Undefined function 'lsqnonlin' for input arguments of type 'function_handle'.Error in optimizer_tutorial (line 2)[x,resnorm] = lsqnonlin(@myfun,x0); % Invoke optimizer
Why doesn't this function work correctly? Do I have the wrong version of Matlab or something?
Best Answer