Issue with lsqcurvefit and langmuir type equation

3 views (last 30 days)
Hi,
I have used this function quite a number of time in different codes and now I am having issues with what I thought it was going to be easy. I will need to fit a langmuir type equation (k*K*C/(1+K*C))to a set of data. I was testing the function with a fake set of data and I keep getting:
Error using lsqcurvefit (line 244) Function value and YDATA sizes are not equal.
I have tested other equations and the code seem to work for them. the problem seem to be having the C repeated at the denominator. Here the code I used:
myfun = @(x,xdata) (x(1)*x(2)*xdata)/(1+x(2)*xdata);
ydata=[0.25 0.3 0.333333333 0.357142857 0.375 0.388888889 0.4 0.409090909 0.416666667 0.428571429];
xdata=[20 30 40 50 60 70 80 90 100 120];
x0=[0.15; 0.03];
lb=[1e-5; 1e-5];
ub=[1e5; 1e5];
options=optimset('Algorithm','levenberg-marquardt');
[x,resnorm,residual,exitflag,output,lambda,jacobian] = lsqcurvefit(myfun,x0,xdata,ydata,lb,ub, options);
I have tested the same data and equation in the CFTOOL and it works fine. I am trying to use a code just because the equation will get more complicated in the future. Thanks in advance for any suggestion.
Regards
Massi

Answers (1)

bym
bym on 17 May 2013
see comments in revised code below
myfun = @(x,xdata) (x(1)*x(2).*xdata)./(1+x(2).*xdata); %added elementwise operators ./ .*
ydata=[0.25 0.3 0.333333333 0.357142857 0.375 0.388888889 0.4 0.409090909 0.416666667 0.428571429];
xdata=[20 30 40 50 60 70 80 90 100 120];
x0=[0.15; 0.03];
lb=[1e-5; 1e-5];
ub=[1e5; 1e5];
%options=optimset('Algorithm','levenberg-marquardt'); this algorithm does
%not support bounds
[x,resnorm,residual,exitflag,output,lambda,jacobian] = lsqcurvefit(myfun,x0,xdata,ydata,lb,ub);

Categories

Find more on Interpolation 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!