Clear Filters
Clear Filters

How to write a for loop to generate a new set of initial conditions based on a input value that changes over different time intervals.

3 views (last 30 days)
I have a ODE system with four equations and a input variable "Input".
I would like to show the output of species B, C and Input on seperate plots over time, similar to the plots attached in the screenshot (generated in Python).
The Input changes over time as follows:
Input = 0.5 for t < 50,
Input = 1 for t < 100,
Input = 1.5 for t < 150,
Input = 1 for t < 200,
Input = 0.5 for t < 250.
The Matlab code I have so far is:
% Set the initial values
A = 1;
B = 1;
C = 1;
D = 1;
Input = 1;
% Set the model parameters
k1 = 1;
k2 = 3;
k3 = 2;
k4 = 1;
k5 = 50;
k6 = 1;
k = [k1,k2,k3,k4,k5,k6];
init = [A B C D];
tspan = [0 200];
%t = linspace (0,200,100);
Looping Process
%Add code here..
% Perform the numerical integration
[t,u] = ode45(@(t,u) gene(t,u,k,Input), tspan, init);
plot(t,u(:,1),'--', t,u(:,2),'-', t,u(:,3),'--',t,u(:,4),'--','LineWidth',2.0)
title('');
xlabel('Time t');
ylabel('Solution y');legend('u_1','u_2','u_3','u_4')
ODE System
function eqns = gene(t,u,k,Input)
eqns = zeros(4,1); % To start with we havefour empty equations
% Using u = [A B C D]
% Using k = [k1,k2,k3,k4,k5,k6]
eqns(1) = k(1)*u(4) - k(2)*u(1);
eqns(2) = k(3)*Input*u(1) - k(4)*u(2);
eqns(3) = k(4)*u(2) - k(5)*u(3)*u(4);
eqns(4) = k(6) - k(5)*u(3)*u(4);
end
Thanks in advance.

Accepted Answer

William Rose
William Rose on 22 Dec 2022
See attached code. I think you will be able to uunderstand what I have done by comparing the code to your version. It generates the plot below. I define Input inside function gene(). Good luck!

More Answers (1)

VBBV
VBBV on 22 Dec 2022
Edited: VBBV on 22 Dec 2022
% Set the initial values
A = 1;
B = 1;
C = 1;
D = 1;
% % Input = 1;
% Set the model parameters
k1 = 1;
k2 = 3;
k3 = 2;
k4 = 1;
k5 = 50;
k6 = 1;
k = [k1,k2,k3,k4,k5,k6];
init = [A B C D];
tspan = [0 250];
%t = linspace (0,200,100);
Input = [0.5 1 1.5 1 0.5];
[t1,u1] = ode45(@(t,u) gene(t,u,k,Input(1)), tspan, init);
idx1 = find(t1 < 50);
[t2,u2] = ode45(@(t,u) gene(t,u,k,Input(2)), tspan, init);
idx2 = find(t2 > 50 & t2 < 100 );
[t3,u3] = ode45(@(t,u) gene(t,u,k,Input(3)), tspan, init);
idx3 = find(t3 > 100 & t3 < 150 );
[t4,u4] = ode45(@(t,u) gene(t,u,k,Input(4)), tspan, init);
idx4 = find(t4 > 150 & t4 < 200 );
[t5,u5] = ode45(@(t,u) gene(t,u,k,Input(5)), tspan, init);
idx5 = find(t5 > 200 & t5 < 250 );
t = [t1(idx1); t2(idx2); t3(idx3); t4(idx4); t5(idx5)];
U1 = [u1(idx1,1); u2(idx2,1); u3(idx3,1); u4(idx4,1); u5(idx5,1)];
U2 = [u1(idx1,2); u2(idx2,2); u3(idx3,2); u4(idx4,2); u5(idx5,2)];
U3 = [u1(idx1,3); u2(idx2,3); u3(idx3,3); u4(idx4,3); u5(idx5,3)];
U4 = [u1(idx1,4); u2(idx2,4); u3(idx3,4); u4(idx4,4); u5(idx5,4)];
figure(1)
plot(t,U1,t,U2,'--',t,U3,'-.',t,U4,'k-')
title('');
xlabel('Time t');
ylabel('Solution y');
legend('u_1','u_2','u_3','u_4')
% ODE System
function eqns = gene(t,u,k,Input)
eqns = zeros(4,1); % To start with we havefour empty equations
% Using u = [A B C D]
% Using k = [k1,k2,k3,k4,k5,k6]
eqns(1) = k(1)*u(4) - k(2)*u(1);
eqns(2) = k(3)*Input*u(1) - k(4)*u(2);
eqns(3) = k(4)*u(2) - k(5)*u(3)*u(4);
eqns(4) = k(6) - k(5)*u(3)*u(4);
end
  8 Comments

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!