Probably simple - ode45 and lsqcurvefit

조회 수: 1 (최근 30일)
Rhys
Rhys 2014년 5월 26일
댓글: Star Strider 2014년 5월 26일
This is probably really trivial, but what I need is a function that takes a vector of parameters and a vector of time and, with ode45, outputs the p values of the ode: dp/dt=b*p-d*p^2.
What I have is:
function dpdt=diffeqn(t,p,par)
b=par(1);
d=par(2);
dpdt=b*p-d*p^2
end
and then a separate script:
[year pdata]=ode45(@diffeqn,[year],p0,[],[0.015,0.001]);
disp(pdata)
This does work fine for what is needed, but I think I need this all in one function, in order to call it in a lsqcurvefit command.

채택된 답변

Star Strider
Star Strider 2014년 5월 26일
Not trivial, but not obvious either. I did this a while back in my answer to Monod kinetics and curve fitting.
Your equations are fine. To use lsqcurvefit, you just need to wrap them inside the objective function you send to it.
You can even include the initial conditions in the parameters to be estimated if necessary. That’s something we can discuss later if you need to do it.
  댓글 수: 3
Rhys
Rhys 2014년 5월 26일
편집: Rhys 2014년 5월 26일
Thanks, it's good to know I'm on the right track. I don't know how to wrap them into the objective function though. The closest I've come to getting the lsqcurvefit to work is using a new function:
function pdata=curvefit(par,year)
[year pdata]=ode45(@diffeqn,[year],p0,[],[par]);
end
and the command:
lsqcurvefit(@curvefit,[0.015,0.001],[year],pop) % pop is the set of data I'm trying to fit to
I thought that would work but I get the error: "Error using lsqcurvefit (line 247) Function value and YDATA sizes are incommensurate."
EDIT: I tried using the year and pop vectors as column vectors instead, and I got an answer which seems to be good. Thank you for the help! :D
Star Strider
Star Strider 2014년 5월 26일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by