Why doesn't M2 function get called? Could somebody please explain what this error is and how to fix it?
Show older comments
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
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
on 18 Jun 2023
Edited: cindyawati cindyawati
on 18 Jun 2023
Answers (1)
Lakshya
on 17 Jun 2023
0 votes
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
on 17 Jun 2023
Lakshya
on 18 Jun 2023
I have saved FRK4M2, FRK4M1 and main functions in seperate files, and now when I'm running the main function, there is the below error in main function. You are passing only 2 arguments in FRK4M1 instead of 5.
you need to provide the missing input arguments when calling the fRK4M1.
M3, O, and P are not defined/initialized.
Not enough input arguments.
Error in fRK4M1 (line 14)
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));
Error in answer (line 18)
k1M1= dt*fRK4M1(M1(j),M2(j));
Image Analyst
on 18 Jun 2023
The error message is fairly explicit -- you're not passing in all the arguments that it needs. You define fRK4M1 as needing 5 input arguments. You are passing it only two. Why? Why are you not passing it 5?
Lakshya
on 18 Jun 2023
Yaa I was telling cindyawati this exact point only ( it seems, it looked like a question rather than a statement ). I am just curious as to why the OP didnt get this error.
cindyawati cindyawati
on 18 Jun 2023
Edited: cindyawati cindyawati
on 18 Jun 2023
cindyawati cindyawati
on 18 Jun 2023
Torsten
on 18 Jun 2023
The equations you are trying to solve are coupled. Thus you must use Runge-Kutta in a vectorized form - you can't update M1, M2, M3, O and P "one after the other" as you do in your code, but simultaneously. With your abilities to program and your numerical background, you only have a chance to solve the system if you transfer your problem to a ready-to-use integrator. MATLAB has many of them, e.g. ODE45 as already mentionned.
cindyawati cindyawati
on 18 Jun 2023
Function definitions in a script must occur after all other code in the script:
So for your code:
[t,M1] = ode45(@mymode, [0,100],[0,1])
function CM1 = mymode (t,M1)
M1= 10; % this overwrite the input value
M2 = 0;
M3 = 1; % you did not define this value
O = 1; % you did not define this value
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;
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);
end
cindyawati cindyawati
on 19 Jun 2023
Moved: Torsten
on 19 Jun 2023
Torsten
on 19 Jun 2023
If you mean the code with ODE45, it's most probably because you solve the wrong differential equation. But we cannot tell.
Where do you input a sigmoid function ?
I already told you that your Runge-Kutta code is incorrect in many respects.
Concerning the sigmoid function:
You update M1 as
M1(j+1)= M1(j)+1./(1+exp(-T(j))); %this is sigmoid function
and then overwrite this setting at the end of the loop by
M1(j+1) = M1(j)+(1/6*(k1M1+(2*k2M1)+(2*k3M1)+k4M1));
What is the motivation for this ?
And there are many, many other examples of this kind in the Runge-Kutta code.
So I can only repeat: Set up your problem using an ODE solver. This way, you have at least the guarantee that the numerical method integrates your ODE system numerically correct. Whether you set up the system correctly is a different question.
cindyawati cindyawati
on 19 Jun 2023
cindyawati cindyawati
on 20 Jun 2023
Categories
Find more on 2-D and 3-D Plots 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!
