Solve a system of equations

3 vues (au cours des 30 derniers jours)
Jacob Barrett-Newton
Jacob Barrett-Newton le 2 Mar 2018
Commenté : Star Strider le 23 Mar 2018
So I have written a system of equations and used ode45 to solve it. I was just wondering if there is a more efficient way to do it. I am creating an ODE model and will later use certain methods to find the unknown parameters, but for now I am just guessing random values.
I have written my model as
function dydt = ODE_Model(t,y,p_1,p_2,p_3,d_1,d_2,d_3,a,b,Tau)
dydt = zeros(3,1);
dydt(1) = p_1*(a*cos(t*2*pi/365-Tau)+b)-d_1*y(1);
dydt(2) = p_2*(a*cos(t*2*pi/365-Tau)+b)-d_2*y(2);
dydt(3) = p_3*y(1)-d_3*y(2)*y(3);
end
and then solved it using
tspan = [0 5];
y0 = [4 5 6];
p_1 = 1;
p_2 = 1;
p_3 = 1;
d_1 = 1;
d_2 = 1;
d_3 = 1;
a=1;
b=1;
Tau=1;
SolExp = ode45(@(t,y) ODE_Model(t,y,p_1,p_2,p_3,d_1,d_2,d_3,a,b,Tau), tspan, y0);
TimePoints = 0:0.1:2;
y = deval(SolExp, TimePoints);
clf
plot(TimePoints, y)
I wanted to do P and D as a combined vector, so i would have P(1) .. P(6) and then i could just write something like P = [1 1 1 1 1 1], but reading about ode45 it seems I can only use one variable? I am not sure. Is there a neater way to do what I want to do, or is this as neat it gets?
Thanks

Réponse acceptée

Star Strider
Star Strider le 2 Mar 2018
These may provide you with an approach appropriate to your problem:
All of these involve estimating the parameters of a system of differential equations, using lsqcurvefit to do the regressions.
  10 commentaires
Jacob Barrett-Newton
Jacob Barrett-Newton le 4 Mar 2018
Yes I just reran my code and it worked. Not sure what the error was, but thank you it looks much more pleasing now.
I am now onto my second problem which is what you initially suggested with those links. I have looked through them, recreated the problems to try and understand them better and I am now looking to apply them to my problem. Apologies for asking so many questions, but I started using matlab a few weeks ago and my supervisor is on strike for weeks so there's not a lot I can do.
My problem is simple, I have a set of data taken over a period of time, with repeats on each taking. I have calculated the means, of all this data, and plotted them. I now have the model that i described above, and wish to estimate the parameters to fit the data that I have. However, from reading through the other links it seems that in those problems some data was already known, such as where the parameter should lie within and what not, but I do not have any of this data. I am simply trying to guess the parameters to fit the graph. Is this similar to those other problems?
Star Strider
Star Strider le 4 Mar 2018
It seems to be similar. The point is that if you believe that guessing the parameters could eventually fit your data, then a more sophisticated and less stochastic approach will likely work.
I would have to have a representative sample and description of your data, what results of your system of differential equations you are fitting to it (for example, the Igor Moura code fits all the results of the integration to the data, while the Monod Kinetics code fits one result), and a description of what you want to do.
The optimization technique you choose to estimate your parameters can be anything. I have use everything from fminsearch to ga (genetic algorithm), depending on the context.
So it is probably possible to estimate your parameters. I can’t say for certain until I see your data, and know what you want to do.

Connectez-vous pour commenter.

Plus de réponses (1)

Jacob Barrett-Newton
Jacob Barrett-Newton le 5 Mar 2018
Although this is causing me a lot of stress, I am a bit hesitant to upload my data and for you to write a code that does it for me like you have done in other questions, as this is for my dissertation and I don't want to inadvertently get done for plagiarism, I'm not really sure how the rules around it work.
But perhaps you can help me with a simialr problem I am having on the same topic. I am trying to make another model that models another aspect of the same data. It uses time dependent variables (I think) and so I have tried to follow the help in ode45 but haven't had much luck.
So far I have for my function:
function dydt = model2(t,y,p,ft,f,gt,g)
f = interp1(ft, f, t);
g = interp1(gt, g, t);
dydt = p(1)*f-p(2)*g*y(1);
and I then try to solve using:
ft = MeansR{8,:};
f =ft
gt = MeansR{11,:};
g = gt
TSPAN = [1 5];
IC = 1;
p = [1 1];
[T Y] = ode45(@(t,y) model2(t, y,p, ft, f, gt, g), TSPAN, IC);
plot(T, Y);
grid
But the plot is blank. The output variable Y returns a column of NaN and I cannot work out why. If i replace MeansR with some other numbers, it works. I have saved mat files of the two Means. Any ideas?
  16 commentaires
Jacob Barrett-Newton
Jacob Barrett-Newton le 23 Mar 2018
Hi, i'm wondering if you can help me with this issue. The code I have attached works perfectly, bar the fact that one line doesn't quite fit to the data, but other than that no problem. However, for whatever reason I am very limited in what I can change values to. For example, If i change y = [1;0;0] to y = [1;1;0], I get the error "Matrix dimensions must agree" and I cannot seem to figure out why this error is occurring.
Star Strider
Star Strider le 23 Mar 2018
When I ran your code with ‘y0(2)~=0’, the warning is:
Warning: Failure at t=2.354295e+02. Unable to meet integration tolerances without
reducing the step size below the smallest value allowed (4.547474e-13) at time t.
So it’s encountering a singularity there, and fails to complete the ‘dY’ vector. That’s the reason it throws the
Matrix dimensions must agree.
error in lsqcurvefit. I don’t see any obvious reason for that to occur. I defer to you to troubleshoot it, and be certain your kinetics equations in ‘DifEq’ are correct.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by