%
%  Second example for fop0
%
%  set the initial parameters
%
 clear
  s0 = [0;0;0];
  tf = 1.0;
  k = 0.001;
 % k = 0.02;
  told = 5e-6;
  tols = 5e-6;
  mxit = 50
%
%  set the number of samples
%
  N = 200;
%
%  determine the initial guess for u
%
    t = linspace(0.0,tf,N);
    u = 2*linspace(1,0,N);
    u = 15*sin(2*pi*t+pi);
    tu = [t' u'];
%
%  interpolate between old values as we refine
%  comment these lines out for your initial estimate
%  uncomment them as you refine
%
%   load old_ut
%   t = linspace(0,tf,N);
%   u = interp1(old_t,old_u,t');
%   tu = [t' u];
%   figure; plot(old_t,old_u,'+',t,u,'o'); legend('old','new');
%   title([ 'N = ' num2str(N)]); grid;  

%  run the code
%
  [tu,ts,la0] = fop0('bobs_fop0_b',tu,tf,s0,k,told,tols,mxit);
%
%  save the most recent results
%
  old_u = tu(:,2);
  old_t = tu(:,1);
  save old_ut old_u old_t  
%
%  Now plot the results
%
 t = ts(:,1);
 x1 = ts(:,2);
 x2 = ts(:,3);
 ut = tu(:,1);
 u = tu(:,2);
 r = ones(1,length(t));
 r = sin(2*pi*t);
  figure;
  orient tall
  subplot(2,1,1); plot(ut,u); grid; ylabel('u(t)');
  subplot(2,1,2); plot(t,r,':',t,x1,'-',t,x2,'--'); legend('r(t)','x_1(t)','x_2(t)'); grid; xlabel('Time (sec)');
%
      