Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Euler's method for systems

  1. Mar 22, 2006 #1
    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. jcsd
  3. Mar 22, 2006 #2
    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].
     
  4. Mar 23, 2006 #3

    HallsofIvy

    User Avatar
    Science Advisor

    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.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook