how to define the function of systems of ODE and call the function correctly in matlab 2018?
6 views (last 30 days)
Show older comments
i wrote two m files , one file named DEs.m for defining differencial equations with ODE model constants parameters , and the other one names call_DEs.m for function sloving , my code as follows :
DEs.m code body:
function [dstate]=DEs(~,X)
u1=0.1; % initial guess on control value
u2=0.1;
% set of constants in system dynamic equation
delta_H=0.00004566;
alpha=0.333;
miu=0.04;
ovip=105.000;
delta_L=0.4;
epsilon_L=0.4;
eta=0.07142;
delta_V=0.07142;
epsilon_V=0.1;
lambda_V=1.5;
lambda_H=1;
gamma=0.01;
N=450000;
% put all ODES in one colum vector as in dstate, total of 6 ODEs
dstate=[delta_H*N-(1-u1)*lambda_H*X(6)/N*X(1)-delta_H*X(1)+alpha*X(3);
(1-u1)*lambda_H*X(6)/(N)*X(1)-(delta_H+gamma+miu*u1)*X(2);
(gamma+miu*u1)*X(2)-(delta_H+alpha)*X(3);
ovip -(eta+epsilon_L+delta_L)*X(4);
eta*X(4)-(1-u1)*lambda_V*(X(2)/N)*X(5)-(epsilon_V*u2+delta_V)*X(5);
(1-u1)*lambda_V*(X(2)/N)*X(5)-(epsilon_V*u(2)+delta_V)*X(6)];
end
the function solver file is with following code:
function [tSOL, Xsol]=call_DEs(t,X)
tspan=[0 100];
% initial conditions on each state
IC1=400000;
IC2=40000;
IC3=10000;
IC4=150000;
IC5=50000;
IC6=20000;
IC=[IC1 IC2 IC3 IC4 IC5 IC6];
[tSOL, Xsol]=ode45(DEs, tspan,IC);
plot(tSOL,Xsol(:,2),'b')
plot(tSOL,Xsol(:,6),'r')
end
but the system give me errors like :
>> call_DEdef
Not enough input arguments.
Error in DEs (line 20)
[dstate]=[delta_H*N-(1-u1)*lambda_H*X(6)/N*X(1)-delta_H*X(1)+alpha*X(3);
Error in call_DEdef (line 14)
[tSOL, Xsol]=ode45(DEs, tspan,IC);
0 Comments
Answers (1)
Steven Lord
on 7 Jan 2020
You don't want to call DEs with 0 inputs and pass whatever that call returns into ode45. DEs requires two inputs to execute correctly.
Instead you want to pass a function handle to DEs into ode45 and let ode45 call DEs (via that function handle) with the appropriate inputs when it needs the values of the right-hand side of your system of ODEs. To make a function handle to DEs, use @.
[tSOL, Xsol]=ode45(@DEs, tspan,IC);
2 Comments
MissEngineer
on 8 Jan 2020
Thanks, that make sense, another problem is when i write the function as :
function [dstate]=DEs(t, X) and saved it in m file which also named as DEs.m , but the system always returns an error saying " function with duplicated name can not be defined as DEs, " my matlab is 2018b version, isn't the file name should be same with the defined function name in that file ? i am not undertanding why that giveing me error .
Steven Lord
on 8 Jan 2020
isn't the file name should be same with the defined function name in that file
Yes, with an except ...
My guess is that you have some commands prior to the definition of the DEs function in that file, making this a function defined in a script file. If that's the case, as mentioned in the Note in the table in the Syntax for Function Definition section on this documentation page "Script files cannot have the same name as a function in the file."
Give your script or your function a different name or move the commands that are prior to the definition of DE in the file to a different file (thus making DE a function defined in a function file, and for those you should have the file and function names match also as called out in that Note.)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!