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!