%%Matlab Assignment 5
%%Tanner Cereghino
%%MAT 275
function Lab5part1
m = 1; % mass [kg]
k = 4; % spring constant [N/m]
omega0 = sqrt(k/m);
y0 = 0.1; v0 = 0; % initial conditions
[t,Y] = ode45(@f,[0,10],[y0,v0],[],omega0); % 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
grid on;
%-----------------------------------------
function dYdt = f(t,Y,omega0)
y = Y(1); v = Y(2);
dYdt = [ v ; -omega0^2*y ];
!
%% Part 1
% a)
% The blue curve represents y=y(t). The blue curve starts at y=.1 m,
% while the red curve starts at y=0 m/s
% b)
% The period is slightly greater than 3
% seconds. Using the equation T = 2pi/omega0, we can calculate the
% period to be exactly pi, 3.14.
% c)
% Without a dampening factor, the mass will never stop because
% no outside forces can stop it.
% d)
% The amplitude is 0.1 m.
% e)
% The maximum velocity is 0.2 m/s. It is reached at pi/2 seconds and
% then repeats every pi/2 seconds.
% f)
% If m is increased, then omega0 decreases and raises the period
% of oscillation. If k is increased, then it raises the value
% of omega0 and decreases the period of oscillation.
0 1 2 3 4 5 6 7 8 9 10
-0.2
-0.15
-0.1
-0.05
0
0.05
0.1
0.15
0.2
%% Part 2
function Lab5part2
m = 1; % mass [kg]
k = 4; % spring constant [N/m]
omega0 = sqrt(k/m);
y0 = 0.1; v0 = 0; % state initial conditions
[t,Y] = ode45(@f,[0,10],[y0,v0],[],omega0); % solve for 0<t<10
y = Y(:,1); v = Y(:,2); % retrieve y, v from Y
E = .5*m*v.^2+.5*k*y.^2;
figure(1);
plot(t,E); % the time series for E
ylim([0,.05]);
grid on;
function dYdt = f(t,Y,omega0)
y = Y(1); v = Y(2);
dYdt = [ v ; -omega0^2*y ];
% a)
% The graph is a straight line at y = .02, proving that energy is
% conserved.
% b)
% dE/dt = m*v*dv/dt + k*x*dx/dt
% m*dv/dt = -k*x dx/dt = v
% dE/dt = k*x*v-k*x*v = 0
% c)
% The curve never gets close to the origin because the energy of the
% system is conserved.
! !
0 1 2 3 4 5 6 7 8 9 10
-0.1
-0.08
-0.06
-0.04
-0.02
0
0.02
0.04
0.06
0.08
0.1
0 1 2 3 4 5 6 7 8 9 10
0
0.005
0.01
0.015
0.02
0.025
0.03
0.035
0.04
0.045
0.05
-0.2 -0.15 -0.1 -0.05 0 0.05 0.1 0.15 0.2
-0.1
-0.08
-0.06
-0.04
-0.02
0
0.02
0.04
0.06
0.08
0.1
%% Part 3
function Lab5part3
m = 1; % mass [kg]
k = 4; % spring constant [N/m]
c = 1; % friction coefficient [Ns/m]
omega0 = sqrt(k/m); p = c/(2*m);
y0 = 0.1; v0 = 0; % initial conditions
[t,Y]=ode45(@f,[0,10],[y0,v0],[],omega0,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
legend('y(t)','v(t)=y’(t)')
grid on
for i=1:length(y)
m(i)=max(abs(y(i:end)));
end
i = find(m<0.01); i = i(1);
disp(['|y|<0.01 for t>t1 with ' num2str(t(i-1)) '<t1<' num2str(t(i))])
function dYdt= f(t,Y,omega0,c)
y = Y(1); v= Y(2);
dYdt = [v; -c*v-omega0^2*y ]; % fill-in dv/dt
!
% a)
% |y|<0.01 for t>t1 with 3.7807<t1<3.8711
% b)
% The maximum velocity is 0.142 m/s, and is reached after
% 0.7 seconds.
0 1 2 3 4 5 6 7 8 9 10
-0.15
-0.1
-0.05
0
0.05
0.1
0.15
y(t)
v(t)=y’(t)
% c)
% A larger c value, or dampening factor can reduce the speed and number
% of oscillations that the mass undergoes.
0.5 0.6 0.7 0.8 0.9 1
-0.15
-0.148
-0.146
-0.144
-0.142
-0.14
-0.138
-0.136
-0.134
-0.132 y(t)
v(t)=y’(t)
%%Part 4
% a)
% Energy is not conserved because the graph decreases with time.
% b)
% for c > 0: dE/dt < 0. -c*v-omega0^2*y < 0
% c is always positive, so the value, -c*v, will always be positive.
% A negative minus a number will always be less than 0: dE/dt < 0.
% for c < 0: dE/dt > 0. -c*v-omega0^2*y > 0
% c is always negative, so the value, -c*v, will always be negative.
% A positive number is greater than 0: dE/dt < 0.
% c)
% The curve spirals into the origin. It loses energy because of the
% dampening factor.
0 1 2 3 4 5 6 7 8 9 10
-0.15
-0.1
-0.05
0
0.05
0.1
0.15
c=2
y(t)
v(t)=y’(t)
0 1 2 3 4 5 6 7 8 9 10
-0.08
-0.06
-0.04
-0.02
0
0.02
0.04
0.06
0.08
0.1
c=4
y(t)
v(t)=y’(t)
0 1 2 3 4 5 6 7 8 9 10
-0.06
-0.04
-0.02
0
0.02
0.04
0.06
0.08
0.1
c=6
y(t)
v(t)=y’(t)
0 1 2 3 4 5 6 7 8 9 10
-0.05
0
0.05
0.1
c=8
y(t)
v(t)=y’(t)