% File:  pblm3_2.m  
%
% Computer Problem 3.2

% Part a.

%  ODE tolerance
tol=0.0001;
% Initial conditions
x0=[1;0];
% Try 5 above instead of 1 and you will see the square term dominant over
% the linear term.

% Time interval to study
t0=0;tf=20;

% Solve for x(t)
[t,x] = ode23('func3_2a',t0,tf,x0,tol,0);
% Plot results
plot(t,x(:,1));
title('x(t)')
xlabel('t in sec');
ylabel('x in m');
hold;

% Now solve for x(t) with linear damping
[t,x] = ode23('func3_2b',t0,tf,x0,tol,0);
% Plot results
plot(t,x(:,1),'--');
legend('-','square damping','--','linear damping');

% For part b., just estimate values from graph and plot by hand

% For part c., for critical damping gamma=w_o
tf=5;
[t,x] = ode23('func3_2c',t0,tf,x0,tol,0);
figure(2);
plot(t,x(:,1));
title('Critical dampling')
xlabel('t in sec');
ylabel('x in m');

% Now search for critical damping for the v^2 dampling

figure(3);
tf=10;   % Need to go further in time to reach min < 2% of initial amplitude
% Solve for x(t)
[t,x] = ode23('func3_2d',t0,tf,x0,tol,0);
% Plot results
plot(t,x(:,1));
title('x(t) for square damping, gamma=12.5')
xlabel('t in sec');
ylabel('x in m');

% Trial and error on the above result (by changing gamma in func3_2d.m).
% Pretty good solution is gamma = 12.5.  Time for this min in x(t) is 5.11 sec.