MATLAB: Lsqcurvefit error when using example

lsqcurvefit

When I copy/paste
myfun = @(x,xdata) x(1)*exp(x(2)*xdata);
% Assume you determined xdata and ydata experimentally
xdata = [0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
ydata = [455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];
x0 = [100; -1];
[x] = lsqcurvefit(myfun,x0,xdata,ydata);
into the command window I get the following error.
??? Attempt to reference field of non-structure array.
Error in ==> optimget>optimgetfast at 95
value = defaultopt.(name);
Error in ==> optimget at 25
o = optimgetfast(options,name,default);
Error in ==> lsqncommon at 24
switch optimget(options,'Display',defaultopt,'fast')
Error in ==> lsqcurvefit at 182
[x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...

Best Answer

  • myfun = @(x,xdata) x(1)*exp(x(2)*xdata);
    % Assume you determined xdata and ydata experimentally
    xdata = [0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
    ydata = [455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];
    x0 = [100; -1];
    [x] = lsqcurvefit(myfun,x0,xdata,ydata);
    Local minimum possible.
    lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the default value of the function tolerance.
    stopping criteria details
    x
    x =
    498.83
    -0.10126
    myfun(x,xdata)
    ans =
    Columns 1 through 7
    455.38 428.54 123.34 67.18 43.466 28.698 14.126
    Columns 8 through 10
    1.1124 0.26145 0.13267
    Works for me. Do you have another function 'myfun' somewhere?