How can I plot a graph by varying two parameters a and b in system of ODE at the same time?

조회 수: 2 (최근 30일)
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
Torsten
Torsten 2024년 2월 25일
Can you include the code for a single combination ? We will show you how to make a loop to use it three times.
SAHEED AJAO
SAHEED AJAO 2024년 2월 25일
Thanks, I can only vary one variable 'b' alone but unable to vary the two at the same time. Here is the code.
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2024년 2월 26일
편집: Torsten 2024년 2월 26일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by