How can I plot a graph by varying two parameters a and b in system of ODE at the same time?
4 views (last 30 days)
Show older comments
How can I plot a graph by varying two parameters say a and b in the system of ordinary differential equations given below at the same time so that the first line of solution will be when a=1, b=2, the second line of solution will be when a=2, b=3, the third line of solution will be when a=3, b=4 and I will have three lines of solutions on the same graph? I want to plot t against
function SYSTEM1ODE ()
global C a b d e g
clear all
clc
tspan=[0 5];
a=1;d=2;C=10000;e=5;g=1;
for b= 2:1:4
[t,x]=ode45(@modmat, tspan, [1200;1150;1000])
plot(t,x(:,2),'linewidth',1.5)
xlabel('Time (Days)'),ylabel('Amonut (X_2)')
hold on
end
function xprime = modmat(t,x)
xprime=[0;0;0];
xprime(1)=C-a*x(1)-b*x(2)+d*x(1);
xprime(2)=a*x(1)-b*x(2)-(d+e)*x(2);
xprime(3)=e*x(2)-g*x(3);
end
end
How can I add 'a' to it, so that 'a' and 'b' will be varying at the same time and still get three lines of solutions on the same graph?
2 Comments
Torsten
on 25 Feb 2024
Can you include the code for a single combination ? We will show you how to make a loop to use it three times.
Accepted Answer
Torsten
on 26 Feb 2024
Edited: Torsten
on 26 Feb 2024
A = [1;2;3];
B = [2;3;4];
C = 10000;
d = 2;
e = 5;
g = 1;
tspan = [0 5];
for i = 1:numel(A)
a = A(i);
b = B(i);
[t,x] = ode45(@(t,x)modmat(t,x,a,b,C,d,e,g),tspan,[1200;1150;1000]);
plot(t,x(:,2),'linewidth',1.5)
xlabel('Time (Days)'),ylabel('Amonut (X_2)')
hold on
end
function xprime = modmat(t,x,a,b,C,d,e,g)
xprime=[0;0;0];
xprime(1)=C-a*x(1)-b*x(2)+d*x(1);
xprime(2)=a*x(1)+b*x(2)-(d+e)*x(2);
xprime(3)=e*x(2)-g*x(3);
end
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!