Why doesn't M2 function get called? Could somebody please explain what this error is and how to fix it?

1 view (last 30 days)
I have function for M2
function M2 =fRK4M2(M1,M2)
M1=10;
M2=0;
mu_2=10^-3;
K1= 10^-4;
K2=5*10^-4;
M2 = (K1*M1*M1)-(K2*M1*M2)-(mu_2*M2);
This is my M1 function
function M1 =fRK4M1(M1,M2,M3,O,P)
delta=50;
K1= 10^-4;
Ko=0.1;
n=3;
Oa=10;
Pa=100;
mu_1=10^-3;
K2=5*10^-4;
K3=10^-3;
gamma=75;
M1=(delta*M1*(1-(M1/gamma))-2*K1*M1*M1-M1*(K2.*M2)-((Oa-n)*K3*M1*M3)-((Pa-Oa)*Ko*M1*O)-(mu_1*M1));
And this is my main function
clc;clear;
%input for time
t(1)=0;
dt=0.1; %time interval
t=0:dt:100; %time span
%input empty array
T=zeros(length(t),1); %empty array for t
M1=zeros(length(t),1); %empty array for M1
M2=zeros(length(t),1); %empty array for M2
for j = 1:length((t))
T(j+1)=T(j)+dt;
M1(j+1)= M1(j)+1./(1+exp(-T(j)));
k1M2 = dt*fRK4M2(M2(j),M1(j));
k2M2 = dt*fRK4M2(M2(j)+k1M2/2,M1(j)+k1M2/2);
k3M2 = dt*fRK4M2(M2(j)+k2M2/2,M1(j)+k2M2/2);
k4M2 = dt*fRK4M2(M2(j)+k3M2,M1(j)+k3M2/2);
M2(j+1) = M2(j)+1/6*(k1M2+2*k2M2+2*k3M2+k4M2);
k1M1= dt*fRK4M1(M1(j),M2(j));
k2M1= dt*fRK4M1(M2(j)+k1M2/2,M1(j)+k1M1/2);
k3M1= dt*fRK4M1(M2(j)+k2M2/2,M1(j)+k2M1/2);
k4M1= dt*fRK4M1(M2(j)+k3M2/2,M1(j)+k3M1/2);
M1(j+1) = M1(j)+(1/6*(k1M1+(2*k2M1)+(2*k3M1)+k4M1));
end
the results I get are not as desired, please someone tell me my fault adn how to fix it. Thanks
  2 Comments
Torsten
Torsten on 18 Jun 2023
So the task of your code is to solve the system of differential equations
dM1/dt = (delta*M1*(1-(M1/gamma))-2*K1*M1*M1-M1*(K2.*M2)-((Oa-n)*K3*M1*M3)-((Pa-Oa)*Ko*M1*O)-(mu_1*M1))
dM2/dt = (K1*M1*M1)-(K2*M1*M2)-(mu_2*M2)
?
Since your code has so many errors, I suggest you use ODE45 for the solution.
cindyawati cindyawati
cindyawati cindyawati on 18 Jun 2023
Edited: cindyawati cindyawati on 18 Jun 2023
thank you for response @Torsten yes my code to solve the system of differential equation. If I use Runge Kutta orde 4th is it possible?

Sign in to comment.

Answers (1)

Lakshya
Lakshya on 17 Jun 2023
Hi,
The code you provided initializes M1 and M2 to specific values (M1 = 10 and M2 = 0) within the fRK4M2 function, which might override the values passed as arguments. If you intend to use the passed arguments instead, you can remove those assignments from the fRK4M2 function.
Hope this helps.
  15 Comments
cindyawati cindyawati
cindyawati cindyawati on 19 Jun 2023
Ok, I know where I went wrong when I used the 4th order Runge kutta formula. I tried removing the sigmoid function and my program is still running. But the Y-axis for M3 has a negative value and the O and P graphs remain flat. Why?
cindyawati cindyawati
cindyawati cindyawati on 20 Jun 2023
Hello @Torsten I wanna ask If I use ODE45 for my problem. I must make 5 function (M1,M2, M3,O,and P) for ODE45 in separate file or I make one file include 5 function? Btw I already try solve my code using ODE45 but i get error like this. How to fix it? Thank you
function CM1 = mymode (t,M1)
M1= 10;
M2 = 0;
M3 = 0;
O = 0;
P=0;
delta=50;
K1= 10^-4;
Ko=0.1;
n=3;
Oa=10;
Pa=100;
mu_1=10^-3;
K2=5*10^-4;
K3=10^-3;
gamma=75;
mu_2=10^-3;
mu_3=10^-3;
mu_o=10^-4;
mu_p= 10^-5;
CM1= zeros(2,1);
CM1(1) = (delta*M1*(1-(M1/gamma))-2*K1*M1*M1-M1*(K2*M2)-((Oa-n)*K3*M1*M3)-((Pa-Oa)*Ko*M1*O)-(mu_1*M1));
CM1(2) = (K1*M1*M1)-(K2*M1*M2)-(mu_2*M2);
CM1(3) = (K2*M1*M2)-(K3*M1*M3)-(mu_3*M3);
CM1(4) = (K3*M1*M3)-(Ko*M1*O)-(mu_o*O);
CM1(5) = (Ko*M1*O)-(mu_p*P);
[t,M1] = ode45(mymode, [0,100],[0,1])
plot (t,M1)
end
>> mymode
Out of memory. The likely cause is an infinite recursion within the program.
Error in mymode (line 31)
[t,M1] = ode45(mymode, [0,100],[0,1])

Sign in to comment.

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!