consider the differential equation dy dx = f(x, y) = x + y on the interval [0,2] with y(0)=2. Write down all the commands for the MATLAB function euler that computes the Euler method.

2 views (last 30 days)
function [X,Y] = euler1(x,xf,y,n)
h = (xf - x)/n; % step size
X = x; % initial x
Y = y; % initial y
for i = 1 : n % begin loop
y = y + h*f(x,y); % Euler iteration
x = x + h; % new x
X = [X;x]; % update x-column
Y = [Y;y]; % update y-column
end % end loop
This is what i 've done but i don't know if it's correct. I would appreciate it if you tell me if everything is ok.
  1 Comment
Cedric
Cedric on 19 Jan 2013
You could test it on a particular case from which you know the answer. This could be done step by step (using the debugger), or you could have the function print values (h,x,y, the latter two from the loop) and check them by hand using a small number of steps.

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!