%% Vibrating Spring
% A few examples of vibrating spring motion
clear all 
close all

%% Undamped
syms omega
ode = 'D2u + u = cos(omega*t)';
undampedsoln = simplify(dsolve(ode,'u(0)=0','Du(0)=0'))
ezplot(subs(undampedsoln, omega, 1/2 ), [0 10*pi])
figure; ezplot(subs(undampedsoln, omega, 3/2 ), [0 10*pi])
%%
% now for omega close to the natural frequency
figure; ezplot(subs(undampedsoln, omega, 0.9 ), [0 100*pi])
%%
% there's the beats; now let's try for resonance
figure; ezplot(subs(undampedsoln, omega, 1 ), [0 10*pi])
%%
% Arghh! Need to convince Mathematica to evaluate 
% correctly at omega = 1.
figure; ezplot(limit(undampedsoln, omega, 1), [0 30*pi])

%% Damped 
clear all
close all
syms omega
ode2 = 'D2u + Du/10 + u = cos(omega*t)';
dsolve(ode2,'u(0)=0','Du(0)=0')
%%
% I guess we'll suppress the algebriac output and 
% concentrate on the graphs from here on.
dampedsoln = simplify(dsolve(ode2,'u(0)=0','Du(0)=0'));
ezplot(subs(dampedsoln, omega, 1/2 ), [0 10*pi])
figure; ezplot(subs(dampedsoln, omega, 3/2 ), [0 10*pi])

%% now for resonance
figure; ezplot(subs(dampedsoln, omega, 1 ), [0 50*pi])
figure; ezplot(subs(dampedsoln, omega, 0.995 ), [0 50*pi])