%linsys.m
%This worksheet shows how to convert a second 
%order equation into a system of first order 
%equations and solve the resulting system using ode45

%Scott Pauls, math 23, Spring 2007

%We will consider the equation y''+sin(y)=0.  Our first step is
% to rewrite this as y'=u, u'=-sin(y)
%We then define a function
eqn=@(t,w) [w(2);-sin(w(1))];        % here t is our independent variable
                                    % w is a vector of length 2 where
                                    % w(1)=y and w(2)=u
                                
[ts,ys]=ode45(eqn,[0 10], [pi/4 0]);     %solve the system on 0<t<10 with  
                                         %initial conditions y(0)=pi/4,u(0)=y'(0)=0
figure;                                    
plot(ts,ys(:,1))                        %plot the solution curve                                    
figure;
plot(ts,ys(:,1),'r-',ts,ys(:,2),'b--'); %plot the solution curve with the derivative curve

