# Euler's method for systems

1. Mar 22, 2006

### grimster

this is the classic euler's method, but i'd like to modify it so that it can handle two-dimensional systems on the form
x'=...
y'=...

what needs to be done?

function [X,Y] = euler1(x,y,x1,n)
h=(x1-x)/n;
X=x;
Y=y;
for i=1:n
y=y+h*f(x,y);
x=x+h;
X=[X;x];
Y=[Y;y];
end
X
Y
plot(X,Y,x,y)

function yp = f(x,y)
yp=y*sin(x);

2. Mar 22, 2006

### LeBrad

If you want to solve a system of equations, say [y1' ; y2'] = f(x,[y1;y2]), just define y as a vector to start and make your function f return a vector [yp1;yp2].

3. Mar 23, 2006

### HallsofIvy

Staff Emeritus
Of course, you now have 3 variables: 2 dependent and 1 independent. I would use "x" and "y" as the dependent variables, "t" as the independent variable. Just do x and y "in parallel": to solve x'= f(x,y,t), y'= g(x,y,t), xnew= xold+ hf(xold,yold,told), ynew= yold+ h*g(xold,yold,told), tnew= told+ h.

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?