% Script to solve the pendulum ODE BVP
%
%    u" + sin(u) = 0, u(0) = a, u(T) = b
%
% for specified a, b, T using the shooting method. The script is set up to 
% duplicate the parameters chosen for fig. 2.4 of the notes.  This
% implementation uses the matlab zero-finding function fsolve and the two
% two user-defined functions shoot.m and pendulum_dydt.m.

  a = 0.7;
  b = 0.7;
  T = 2*pi;
  tspan = [0 T];

  v0 = 0;                  % Initial guess at v0 = du/dt(0)
  v0 = fzero('pendulum_shooterr',v0)  
                           % Get true solution for v0 via shooting. 
                                                   
  % Solution can now be recovered from ode45 with init cond. [a v0]

  subplot(2,2,2) 
  y0 = [a v0];
  [t, y] = ode45('pendulum_dydt',tspan,y0);
  u = y(:,1);
  plot(t,u,'r-')
  xlabel('t')
  ylabel('\theta (radians)')
  xlim([0 T])
  ylim([-1 1])
  title('Shooting method using fzero')

