How to fit data with a solution of a non linear differential equation containing multiple unknown coefficient
54 views (last 30 days)
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
0 Comments
Accepted Answer
More Answers (2)
Torsten
on 7 Dec 2018
Edited: Torsten
on 7 Dec 2018
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
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.
See Also
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!