# MatLab Spring Damping and Energy

This is the entire problem. I am looking for help with part 3.

For the mass-spring system with damping, we obtain the following DE
my′′ + gammay′ + ky = 0, or y′′ + cy′ + omega^2y = 0, where c = gamma/m and omega^2 = k/m. Given m = 1,k = 4, gamma= 1, and initial conditions y(0) = 0.1, y′(0) = 0.
(1) Plot solutions y and v = y′ = dy/dt as functions of time.
(2) Plot v vs y (phase plot). Comment on the behavior of the curve.
(3) Plot the quantity E = 1/2mv^2 + 1/2ky^2 as a function of time. What do you observe?
(4) Show that dE/dt < 0 for gamma > 0 while dE/dt > 0 for gamma < 0 (analytically or by graphic).

For part 1
function Q2part1
m = 1; % mass [kg]
k = 4; % spring constant [N/m]
gamma = 1; % friction coefficient [Ns/m]
omega = sqrt(k/m); c = gamma/m;
y0 = 0.1; v0 = 0; % initial conditions
[t,Y] = ode45(@f,[0,10],[y0,v0],[],omega,c); % solve for 0<t<10
y = Y(:,1); v = Y(:,2); % retrieve y, v from Y
figure(1); plot(t,y,'b+-',t,v,'ro-'); % time series for y and v
%-------------------------------------------
function dYdt = f(t,Y,omega,c)
y = Y(1); v = Y(2);
dYdt = [ v ;-c*(v)-omega^2*y];

for part 2

function Q2part2
m = 1;
k = 4;
gamma = 1;
omega = sqrt(k/m); c = gamma/m;
y0 = 0.1; v0 = 0;
[t,Y] = ode45(@f,[0,10],[y0,v0],[],omega,c);
y = Y(:,1); v = Y(:,2);
figure(1); plot(v,y);
function dYdt = f(t,Y,omega,c)
y = Y(1); v = Y(2);
dYdt = [ v ;-c*(v)-omega^2*y];

I know that i need to plug in formulas for y (and v as well?) so that I have E(t), but is it as simple as solving for and plugging in y? I am in need of direction here I suppose...a nice big fat hint to put me in the correct direction. Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
I don't know which part you need help with, but if you are on part three you should look into a contour plot.

For example, here is a way to do an energy contour of a undamped oscillator with a mass of 4.5kg and a spring constant of 2N/m:

[x y]=meshgrid(-1:0.01:1,-1:0.01:1);

m=4.5;
k=2;

contour(x,y,((1/2)*m*y.^2)+((1/2)*k*x.^2),4)
title('Energy Contour Plot for Undamped Oscillations')
ylabel('Velocity')
xlabel('Position')

Oh whoops, they asked for it as a function of time... which I don't quite understand. Maybe you could overlay the potential energy vs. time and the kinetic energy vs. time onto one plot.