Filter löschen
Filter löschen

Function that solves ODEs using Euler's Method

2 Ansichten (letzte 30 Tage)
Alyssar
Alyssar am 17 Apr. 2022
Beantwortet: Akshat Dalal am 20 Dez. 2023
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 Kommentar
Dyuman Joshi
Dyuman Joshi am 17 Apr. 2022
Bearbeitet: Dyuman Joshi am 17 Apr. 2022
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Akshat Dalal
Akshat Dalal am 20 Dez. 2023
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!

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by