How to solve ODEs with time dependent parameters by RK4 method?
Show older comments
Hello, everyone! I have an issue with solving ODEs by the RK4 method. The bacteria growth can be described by the couples of equation, the total number N can be divided into two parts (N1 and N2, which represented by (1) and (2)). The growth rate umax and m depend on temperature (T). T changed with time t linearly between two points (time, temp). I know there is something wrong with my code, but I have just begun to learn matlab. Does anybody can help me figure it out? Any help will be gratefully appreciated.
t = [0, 5, 10, 15, 20, 25, 30, 35, 50, 75, 100]; %time
T = [10, 4, 2, 15, 15, 2, 10, 2, 2, 15, 10]; %Temp
function main
clear all
clc
close all
t_in = [0, 5, 10, 15, 20, 25, 30, 35, 50, 75, 100];%Time
Temp_in = [10, 4, 2, 15, 15, 2, 10, 2, 2, 15, 10]; %Time
dt = 0.1; %stepsize
n = max(t_in)/dt+1; %DataPoints
% parameters
k = 3.76e-3;
gamma = 1.0;
a = 8.86e-2;
Tmin = 8.91;
Ymax = 8.86; %log10(Nmax)
Nmax = 10^Ymax;
f = @(t,N)[-gamma*rate*N(1);gamma*rate*N(1)*m + rate*N(2)*(1-m*(N(1)+N(2))/Nmax)]; %lag cells and dividing cells
% Intial conditions
t(1) = 0;
N(:,1) = [100,0];%intial cell numbers,N1=1000和N2=0
function umax = rate(i)
%ti = linspace(0, max(t_in), n);
Temp = interp1(t_in,Temp_in,n);% T at ti0,1,2..n
i = 1:n;
if Temp(i) < Tmin
umax = k*(Temp(i) -Tmin);
else
umax = a^2*(Temp(i) -Tmin)^1.5;
end
%end
end
function m
i = 1:n;
if Temp(i) < Tmin
m = 0;
else
m = 1;
end
end
%RK4 update loop
for i = 1:n
t(i+1)=t(i)+dt;
k1 = f(t(i) ,N(:,i) );
k2 = f(t(i)+dt/2,N(:,i)+k1*dt/2);
k3 = f(t(i)+dt/2,N(:,i)+k2*dt/2);
k4 = f(t(i)+dt ,N(:,i)+k3*dt );
N(:,i+1) = N(:,i)+dt/6*(k1+2*k2+2*k3+k4);
end
y = log((N(1,:))+N(2,:))/2.303; %N = N1+N2,transfe to log10
%plot the solution
plot(t,y)
xlablel(Time)
ylablel(Number)
end


4 Comments
Jan
on 13 Mar 2019
Please note, that clear all is completely useless on top of a function, but wastes time only. The brute clearing header "clear all, clc, close all" is cargo-cult-programming (link): It might have look useful in another context, but now it has become a useless piece of magic.
" I know there is something wrong with my code" - then please be so kind and share with the readers, what let you assume this. Providing all useful information is a good strategy, if you want others to help you.
What is the purpose of this part:
function m
i = 1:n;
if Temp(i) < Tmin
m = 0;
else
m = 1;
end
end
I cannot guess its intention. Because i is a vector, the command if Temp(i) < Tmin is equivalent to:
if all(Temp(1:n) < Tmin)
Is this really wanted? It is at least strange to use the name of a nested function as output variable. Calling the function "m" will define teh variable "m", such that the function is shadowed for following calls.
You define "rate" as a function, which expects 1 input argument::
function umax = rate(i)
But then you call it without inputs:
... [-gamma*rate*N(1); ...
You are using anonymous and nested functions. This increases the complexity of the code without any need. I recommend to use standard functions to avoid an unnecessary confusion. As soon as f is defined as standard function, interpolating the temperature will be much easier.
Shanshui Li
on 13 Mar 2019
Jan
on 14 Mar 2019
You are welcome. This is a forum for solving questions concerning Matlab.
Gorka Bidegain
on 13 Jun 2022
Hi Sanshui,
what do you want to represent by the parameter gamma in equation 1 and 2 (gamma=1)?
Thanks
Gorka
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!