How to fit data with a solution of a non linear differential equation containing multiple unknown coefficient
Show older comments
Hi everybody,
So it's been quite some time since I have used Matlab and now I have had a hard time figurint out how to proceed with my problem.
So I have measured some data and I would like to fit it with a certain function.
This latter function is found by solving numerically a non linear differential equation with 6 unknown coefficients.
What I want to do is by using a least-squares method (lsqcurvefit) I would vary the 6 unknown coefficients of the non linear ODE so that after the loop routine the solution to this equation fits my data.
And I'm stuck in thinking how to label all this. I understand the examples of the lsqcurvefit but it applies to a function. And I want to apply it to a equation so that the solution fits the data.
Here is the equation with the unknown coefficients a, b1, b2, c1, c2 and d. I aleady have a guess for all these values and a range in which they should lie within.
When I solve this equation numerically in matlab (with certain values), I can plot the function u(t). But what I want to plot and fit is actually
.
So in summary: I want to write a routine loop varying my 6 coefficients in my non linear equation in order for the p(t) function to fit my data.
Any suggestion from you guys ?
Thanks a lot.
Best,
Thibaut
Accepted Answer
More Answers (2)
function main
Time = [0,0.0625 0.125 0.1875 0.25 0.3125 0.3750 0.4375 0.5 0.5625 0.625 0.6875 0.75 0.8125 0.875 0.9375 1 1.0625 1.125 1.1875 1.25 1.3125 1.375 1.4375 1.5 1.625 1.75 1.875];
PLdata = [1 0.8 0.6 0.57 0.53 0.5 0.45 0.4 0.35 0.3 0.27 0.23 0.2 0.18 0.16 0.13 0.115 0.08 0.07 0.07 0.06 0.05 0.05 0.06 0.06 0.045 0.04 0.035];
B0 = rand(6,1)*100; %creating a vector for the 6 unknown parameters
[B,Rsdnrm,Rsd,ExFlg,OptmInfo,Lmda,Jmat] = lsqcurvefit(@plmodel,B0,Time,PLdata); %initiating the lsqcurvefit loop
end
function u = plmodel(B,t)
%variables: x=U pl=P
%Parameters: p_0=B(1), tau_p=B(2), tau_n=B(3), c_n=B(4), c_p=B(5), k*=B(6)
u0 = 1.0; %you must specify the correct starting value u0 for u !!!
[~,u] = ode45(@(tf,uf)DifEq(tf,uf,B),t,u0);
u = B(6)*u.*(B(1)+u);
end
function du = DifEq(t,u,B)
du = -u*(-u+B(1))*(1/(B(2)*u+B(3)*(u+B(1)))-B(4)*u-B(5)*(B(1)+u)-B(6));
end
4 Comments
Thibarely
on 11 Dec 2018
Thibarely
on 11 Dec 2018
Torsten
on 12 Dec 2018
In the call
[B,resnorm,residual,exitflag,output] = lsqcurvefit(@plmodel,B0,t,u);
t and u must have the same length.
In plmodel, t and u also must have the same length.
And note that you start with u0=1. So t=0 should be included in your t-vector.
5 Comments
I don't get what you mean.
What you do in your code is to fit your input vector "u" to the output vector "u" of "plModel", thus to B(4).*udiff.*(3*10^15+udiff) where "udiff" is the solution of the differential equation
d udiff/dt = -udiff.*(-udiff+3*10^15).*(1/(B(1).*udiff+B(2).*(udiff+3*10^15))-B(3).*udiff-B(4))
If you want to fit your input vector u directly to udiff obtained from the solution of the differential equation, remove the line
u= B(4).*u.*(3*10^15+u);
in your code.
And please don't create new "Answers" every time you make "Comments".
Torsten
on 13 Dec 2018
Say "u" is the solution of my non linear equation. I want the lsqcurvefit to fit my data not with "u" but with
B(4).*u.*(3*10^15+u)
But that's exactly what lsqcurvefit does in your code:
If u is the solution of the differential equation
udot = -u.*(-u+3*10^15).*(1/(B(1).*u+B(2).*(u+3*10^15))-B(3).*u-B(4));
, lsqcurvefit fits B(4)*u*(3e15+u) to your data vector
[1; 0.8; 0.7; 0.6; 0.55; 0.5; 0.45; 0.4; 0.35; 0.3; 0.27; 0.23; 0.2; 0.18; 0.16; 0.13; 0.115; 0.118; 0.09; 0.092; 0.068; 0.07; 0.045; 0.045; 0.047; 0.03; 0.026; 0.022]
Torsten
on 14 Dec 2018
Although then I don't understand t(1)=0, u(1)=1 in your data vector. If you wanted to fit against B(4)*u*(3e15+u), your first u(1) should be much larger than 1. Seems we get our wires crossed.
Thibarely
on 14 Dec 2018
Categories
Find more on Get Started with Curve Fitting Toolbox 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!