Function that solves ODEs using Euler's Method

2 次查看(过去 30 天)
function [tout,yout]=EulerSolver(f,t,y0)
% f(t,y) is an anonymous function that defines the right-hand side of the ODE ydot = f(t,y)
% t =[t0 t1 ... tfinal] is a vector of grid points with length N
tout=t;
t0=0;
tfinal=1;
t=[t0:h:tfinal];
yout=Y;
Y=zeros(length(t),3);
Y( :,1) = y0; % y0=[a; b; c] is a column vector that contain the initial values y(0)=y0.
h=(t(2)-t(1))/N
for i=1:length(t)-1
fy=f(t(i),yout(:,i));
yout(:,i+1)=yout(:,i)+h*fy(:);
end
end
Hi, I am completing a code that solves ODEs using the Euler method. My code is as shown above, however I keep recieving the error:
" Not enough input arguments.
Error in EulerSolver (line 4).
tout=t; ".
If anyone can please provide me with some suggestions or help on why I am receiving this error, it's greatly appreciated.
Thank you
  1 个评论
Dyuman Joshi
Dyuman Joshi 2022-4-17
编辑:Dyuman Joshi 2022-4-17
How are you calling this function?
Also, You are using h before defining it. And you are defining h from the definition where it requires h as an input.
Same goes for Y.

请先登录,再进行评论。

回答(1 个)

Akshat Dalal
Akshat Dalal 2023-12-20
Hi Alyssar,
I understand that you want to write a function for solving ODE's using the Euler method but are facing some errors.
I went through your code and made the following observations:
  • As Dyuman mentioned, you are using the variables 'h', 'Y' and 'N' before defining them.Thus, the code will error out because of undefined variables. I believe you might have declared them in your 'base workspace'. If that's the case, MATLAB functions are executed in their own 'function workspace', which is different from the 'base workspace'. Thus, your 'EulerSolver' function will not have access to any variables declared in the 'base workspace', and you might need to pass them as inputs to use them in your function. To read more about the function workspace, you could refer the following documentation: https://www.mathworks.com/help/releases/R2020b/matlab/matlab_prog/base-and-function-workspaces.html
  • The error which is thrown in your function is unusual. I recommend rechecking your function call to make sure that the input arguments match your function definition.
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by