A problem using ode23s and lsqcurvefit

6 views (last 30 days)
guangde mu
guangde mu on 9 May 2011
Hi all. I try to use Matlab to the nonlinear fit to my ODE, here is my code:
function dydt = modelsq(t,y,k) %UNTITLED6 Summary of this function goes here % Detailed explanation goes here dydt=zeros(2,1) dydt(1)=0.0321*k(1)*(k(2)-y(1))-k(3)*y(1) dydt(2)=0.25*k(4)*exp(-k(4)*t)*k(2); end
function y = numcal(k,x) %UNTITLED4 Summary of this function goes here % Detailed explanation goes here global y0 tspan=[0 max(x)]; [m,n]=size(x); [tt yy] = ode23s(@dydt,tspan,y0,[],k); yc=spline(tt',yy',x); y=yc; end
global y0 xdata=[0,0.048,0.058,0.061,0.064,0.065]; ydata=[0.0487,1.6657,0.86,1.156,1.391,1.675]; k0=[0,0,0,0]; lb=[0,0,0,0]; ub=[6.5,5,6]; options=optimset('TolFun',1e-20,'TolX',1e-20,'MaxFunEvals',100,'Algorithm','trust-region-reflective','Display','iter'); beta=lsqcurvefit(@numcal,k0,xdata,ydata,lb,ub,options); Jc=numcal(k,xdata); plot(xdata,ydata,'o',xdata,Jc);
But it always return to me error, can somebody help me please?
Thanks, Guangde
  1 Comment
Andrew Newell
Andrew Newell on 10 May 2011
Please format your code so that there is one command per line.

Sign in to comment.

Answers (1)

Andrew Newell
Andrew Newell on 10 May 2011
You need to learn some debugging techniques. Look at Preventing and Identifying Coding Problems, particularly the part about the message bar. If you look at the little orange warning lines in the message bar, you'll find one that says you never set the value of y0.
If you fix that and run the code, you'll get the message
??? Error using ==> feval
Undefined function or method 'dydt' for input arguments of type 'double'.
and a few lines below,
Error in ==> numcal at 5
[tt yy] = ode23s(@dydt,tspan,y0,[],k);
When you see such messages, you should start by clicking on the links to lines in your code. dydt is the output of the function modelsq. You need @modelsq. When you fix that, you'll find more errors, but you should try fixing those on your own.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!