simultaneouslty fIt ode soln to multiple datasets with lsqcurvefit

4 views (last 30 days)
I have a data matrix, A, whose columns I want to simultaneously fit to the solution of the ode below using lsqcurvefit. Each column of A corresponds to a different value of L. For example, if
L=1:25
time=[0:0.001:3.8]'
The data matrix has the dimensions time x L. I have tried using meshgrid to represent the independent variables, but my attempts return the error:
"The entries in tspan must strictly increase or decrease."
So it seems that the function is reading matrix generated by meshgrid as a continuous time series.
Any guidance would be greatly appreciated.
function [C]=conformselect(K,time)
Y0=[1;1;0];
[tSol,YSol]=ode45(@diffeq,time,Y0);
function dYdt = diffeq(time,Y)
a=Y(1);
b=Y(2);
c=Y(3);
k1=K(1);
km1=K(2);
k2=K(3);
km2=K(4);
dadt=-k1*a+km1*b;
dbdt=k1*a-(km1+k2*L)*b+km2*c;
dcdt=k2*L*b-km2*c;
dYdt=[dadt;dbdt;dcdt];
end
A=YSol(:,1);
C=YSol(:,3);
end

Accepted Answer

Star Strider
Star Strider on 22 Mar 2018
Your function appears to be essentially correct.
The lsqcurvefit function can fit matrix dependent variables. However you need to return all the columns you want to fit in your ‘C’ output. You are currently returning only one column, ‘YSol(:,3)’.
For various approaches to this sort of problem, see:
  9 Comments
John Hackett
John Hackett on 23 Mar 2018
Yes, thanks for your help. As you mentioned, the function needed to return all of the columns of C. I was distracted by looking for an alternative way to achieve that without a loop, but it turned out to work fine.
Star Strider
Star Strider on 23 Mar 2018
My pleasure.
If my Answer helped you solve your problem, please Accept it!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!