MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# 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.

Asked by Michalis on 19 Jan 2013
```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 Wannaz 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.

## Products

No products are associated with this question.