%% Models the simple pendulum using Euler's method. %% Usage: [t,th,w] = EulerPendulum([ic1,ic2],tmin,tmax,N) %% Input: ic1 and ic2 - the intial values of theta (th) and w (which is th'). %% tmin - initial time %% tmax - final time %% N - number of steps %% Output: t - Nx1 vector of time values %% th - Nx1 vector of angle values %% w - Nx1 vector of angular velocity values function [t,th,w] = EulerPendulum(ic,tmin,tmax,N) g = 9.81; L=1; % gravity and rod length t = linspace(tmin,tmax,N)'; h = t(2) - t(1); % step size th = zeros(size(t)); w = th; th(1)=ic(1); w(1)=ic(2); % initial conditions for j=1:N-1 th(j+1) = th(j)+h*w(j); w(j+1) = w(j) + h*(-g/L*sin(th(j))); end end