Why do I receive "Not enough input arguments"? or How can I better code this ODE?

5 Ansichten (letzte 30 Tage)
Chiara
Chiara am 15 Mär. 2023
Bearbeitet: Sachin am 16 Mär. 2023
I am trying to code
function dydt = myODE(t,y, r);
global P0 h0 r0 r1 r2 d3 p1 p2
P0=0.7;
h0=0.0000235;
r0=0.01818;
r1=0.08712;
r2=8.0217;
d3=0.1;
p1=0.4783;
p2=0.4987;
% y(1) = x_0;
% y(2) = x_1;
% y(3) = x_2;
% y(4) = x_3;
p0=P0./(1+h0.*y(1));
tmp1 = r0.*y(1).*(p0-1);
tmp2 = 2.*ro.*y(1).*(1-p0) + r1.*y(2).*(2.*p1-1);
tmp3 = 2.*r1.*y(2).*(1-p1) + r2.*y(2).*(2.*p2-1);
tmp4 =2.*r2.*y(3).*(1-p2) - d3.*y(4);
dydt = [tmp1; tmp2; tmp3; tmp4];
end
function exitflag = HematopoeiticSystemTrial
time = [0:1:4];
%------%
x_0 = 1/110;
x_1 = 1/24;
x_2 = 1/22;
x_3 = 1/10;
%------%
% ----- Paremeters -----%
global P0 h0 r0 r1 r2 d3 p1 p2
p = [x_0 x_1 x_2 x_3];
%--------------------------------------------------%
[t,dydt1] = ode45(@myODE,time,[p(1),p(2),p(3),p(4)],[],p);
dydt1
end
What could I be doing wrong?

Antworten (3)

Dyuman Joshi
Dyuman Joshi am 15 Mär. 2023
Bearbeitet: Dyuman Joshi am 15 Mär. 2023
I'm not sure why you receive that error, but your code works properly with some tweaks.
There is no need to use global here.
If you are defining the call to ode inside a function you need to assign variables as output and call that function accordingly.
Note that as you have defined a discrete Time span, thus the result be obtained for those particular values.
[T,Y]=HematopoeiticSystemTrial
T = 5×1
0 1 2 3 4
Y = 5×4
0.0091 0.0417 0.0455 0.1000 0.0090 0.0416 0.0484 0.4497 0.0090 0.0415 0.0513 0.7885 0.0089 0.0415 0.0542 1.1172 0.0089 0.0414 0.0571 1.4370
Here, the first column of the output Y corresponds to x_0, second to x_1, third to x_2 and fourth to x_3.
function [t,dydt1] = HematopoeiticSystemTrial
time = [0:1:4];
%------%
x_0 = 1/110;
x_1 = 1/24;
x_2 = 1/22;
x_3 = 1/10;
% ----- Paremeters -----%
p = [x_0 x_1 x_2 x_3];
%--------------------------------------------------%
[t,dydt1] = ode45(@myODE,time,p);
end
function dydt = myODE(t,y, r);
P0=0.7;
h0=0.0000235;
r0=0.01818;
r1=0.08712;
r2=8.0217;
d3=0.1;
p1=0.4783;
p2=0.4987;
% y(1) = x_0;
% y(2) = x_1;
% y(3) = x_2;
% y(4) = x_3;
p0=P0./(1+h0.*y(1));
tmp1 = r0.*y(1).*(p0-1);
tmp2 = 2.*r0.*y(1).*(1-p0) + r1.*y(2).*(2.*p1-1);
tmp3 = 2.*r1.*y(2).*(1-p1) + r2.*y(2).*(2.*p2-1);
tmp4 =2.*r2.*y(3).*(1-p2) - d3.*y(4);
dydt = [tmp1; tmp2; tmp3; tmp4];
end

Davide Masiello
Davide Masiello am 15 Mär. 2023
Bearbeitet: Davide Masiello am 15 Mär. 2023
Here's a correct version of your code.
1) Time span and initial conditions must be in the same file as the ode solver, not sure why you embedded them in the exitflag function
tspan = [0,4];
x0 = [1/110 1/24 1/22 1/10];
2) I suggest you pass the extra parameters using function handling rather than gloval variables, it's a more robust approach.
P0=0.7;
h0=0.0000235;
r0=0.01818;
r1=0.08712;
r2=8.0217;
d3=0.1;
p1=0.4783;
p2=0.4987;
[t,y] = ode45(@(t,y)myODE(t,y,P0,h0,r0,r1,r2,d3,p1,p2),tspan,x0);
plot(t,y)
function dydt = myODE(t,y,P0,h0,r0,r1,r2,d3,p1,p2);
p0=P0/(1+h0*y(1));
tmp1 = r0*y(1)*(p0-1);
tmp2 = 2*r0*y(1)*(1-p0)+r1*y(2)*(2*p1-1);
tmp3 = 2*r1*y(2)*(1-p1)+r2*y(2)*(2*p2-1);
tmp4 = 2*r2*y(3)*(1-p2)-d3*y(4);
dydt = [tmp1; tmp2; tmp3; tmp4];
end

Sachin
Sachin am 16 Mär. 2023
Bearbeitet: Sachin am 16 Mär. 2023
Hi @Chiara,
I understand that you are getting error ‘ Not enough input argument’. Here are some points that might be helpful to you :
  1. MyODE' function should be below to the HematopoeiticSystemTrial’ function. Because MATLAB scans the code from the first line so it doesn’t find inputs to the function HematopoeiticSystemTrial’’. So place ‘myODE’ function below.
function exitflag = HematopoeiticSystemTrial
time = [0:1:4];
%------%
x_0 = 1/110;
x_1 = 1/24;
x_2 = 1/22;
x_3 = 1/10;
%------%
% ----- Paremeters -----%
global P0 h0 r0 r1 r2 d3 p1 p2
p = [x_0 x_1 x_2 x_3];
%--------------------------------------------------%
[t,dydt1] = ode45(@myODE,time,[p(1),p(2),p(3),p(4)],[],p);
dydt1
end
function dydt = myODE(t,y, r);
global P0 h0 r0 r1 r2 d3 p1 p2
P0=0.7;
h0=0.0000235;
r0=0.01818;
r1=0.08712;
r2=8.0217;
d3=0.1;
p1=0.4783;
p2=0.4987;
% y(1) = x_0;
% y(2) = x_1;
% y(3) = x_2;
% y(4) = x_3;
p0=P0./(1+h0.*y(1));
tmp1 = r0.*y(1).*(2*p0-1);
tmp2 = 2.*r0.*y(1).*(1-p0) + r1.*y(2).*(2.*p1-1);
tmp3 = 2.*r1.*y(2).*(1-p1) + r2.*y(2).*(2.*p2-1);
tmp4 =2.*r2.*y(3).*(1-p2) - d3.*y(4);
dydt = [tmp1; tmp2; tmp3; tmp4];
end
2. Check your equation of tmp1,tmp2,tmp3,tmp4 . Your tmp1 equation doesn't seems to be correct.
3. You can find more details about Parameterizing function here :

Community Treasure Hunt

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

Start Hunting!

Translated by