MATLAB question!
4. (a) Modify the code, to compute and plot the quantity E =1/2mv^2 + 1/2ky^2 as a function of time. What do you observe? Isthe energy conserved in this case?
(b) Show analytically that dE/dt < 0 for c > 0 while dE/dt> 0 for c < 0.
(c) Modify the code to plot v vs y (phase plot). Comment on thebehavior of the curve in the context of the motion of the spring.Does the graph ever get close to the origin? Why or why not?
given code
---------------------------------------------------------------
clear all;
m = 4; % mass [kg]
k = 9; % spring constant [N/m]
c = 4; % friction coefficient [Ns/m]
omega0 = sqrt(k/m); p = c/(2*m);
y0 =-0.8; v0 = 0.3; % initial conditions
[t,Y] = ode45(@f,[0,15],[y0,v0],[],omega0, p); % solve for0y = Y(:,1); v = Y(:,2); % retrieve y, v from Y
figure(1); plot(t,y,'ro-',t,v,'b+-');% time series for y andv
grid on; axis tight;
%---------------------------------------------------
function dYdt = f(t,Y,omega0,p); % function defining the DE
y = Y(1); v = Y(2);
dYdt=[ v ; -omega0^2*y-2*p*v]; % fill-in dv/dt
end
-----------------------------------------------------------------------------------------