How do I plot component curves for system of differential equations? I keep getting errors for this compartment model

F=@(t,x) [-4*x(1)+4*x(2); x(1)-6*x(2)+3*x(3)+2; x(1)+2*x(2)-8*x(3)+5];
[t,x]=ode45(F,[0 3], [0 10]);
plot(t, x(:,1), t, x(:,2), t, x(:,3))

 Accepted Answer

You have to supply the same number of initial conditions to the ODE solver as you have variables in your ODE equation. Assuming you only want ‘x(3)’ to have anything in it at the start, this works:
[t,x]=ode45(F,[0 3], [0 0 10]);
The rest of your code is unchanged, although you might want to add this after your plot call:
legend('C_1(t)', 'C_2(t)', 'C_3(t)', 'Location','NE')

5 Comments

Hi thanks. Is it possible to set initial conditions of the problem, specifically x(1) = x(2) = x(3) = 0 and t = 0 for all 3?
Nothing seems to happen when I try run it after making the change
This code works for me:
F=@(t,x) [-4*x(1)+4*x(2);
x(1)-6*x(2)+3*x(3)+2;
x(1)+2*x(2)-8*x(3)+5];
[t,x]=ode45(F, [0 3], [0 0 10]);
plot(t, x(:,1), t, x(:,2), t, x(:,3))
legend('C_1(t)', 'C_2(t)', 'C_3(t)', 'Location','NE')
It is possible to have zero initial conditions for all 3. They all go from 0 to 1 at the end of 3 time units, each taking a different trajectory.
What do you mean by ‘nothing seems to happen’? When I run that code, ‘t’ is a (93x1) vector, and ‘x’ is a (93x3) matrix. The plot displays appropriately.
Thanks for the help. I finished fixing everything to be the way I want it.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!