Why do I receive "Not enough input arguments"? or How can I better code this ODE?
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(3 个)
Dyuman Joshi
2023-3-15
编辑:Dyuman Joshi
2023-3-15
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
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
0 个评论
Davide Masiello
2023-3-15
编辑:Davide Masiello
2023-3-15
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
0 个评论
Sachin
2023-3-16
编辑:Sachin
2023-3-16
I understand that you are getting error ‘ Not enough input argument’. Here are some points that might be helpful to you :
- ‘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 :
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!