Example of the Euler Method in 2D
We will use the Euler method to solve the damped pendulum equation written as a system
for initial conditions , , and . % Set up initial conditions and parameters
t=linspace(0,tfinal,nsteps+1);
% Loop through and run the Euler method
x(k+1) = x(k) + h * y(k);
y(k+1) = y(k) + h * (-gamma*y(k)-sin(x(k)));
xlabel('t'); legend('x','y')
Plot the solution in the phase plane