Question

In: Advanced Math

MATLAB question! 4. (a) Modify the code, to compute and plot the quantity E = 1/2mv^2...

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? Is the 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 the behavior 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 for 0<t<15
y = Y(:,1); v = Y(:,2); % retrieve y, v from Y
figure(1); plot(t,y,'ro-',t,v,'b+-');% time series for y and v
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

-----------------------------------------------------------------------------------------

Solutions

Expert Solution


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 for 0<t<15
y = Y(:,1); v = Y(:,2); % retrieve y, v from Y
figure(1); plot(t,y,'ro-',t,v,'b+-');% time series for y and v
title('y(t) and v(t) plot')
xlabel('time (t)')
ylabel('y(t) and v(t) plot')
grid on; axis tight;

%Answering question 4a.
E=((1/2).*m.*v.^2)+((1/2).*k.*y.^2);
figure(2); plot(t,E,'ro-');% time series for E
title(sprintf('E(t) vs. time plot for c=%d',c))
xlabel('time (t)')
ylabel('E(t) plot')
grid on; axis tight;

%Answering question 4a.

figure(5); plot(y,v,'ro-');% time series for E
title(sprintf('Phase space plot for c=%d',c))
xlabel('y(t)')
ylabel('v(t)')
grid on; axis tight;


%Answering question 4b.
E=((1/2).*m.*v.^2)+((1/2).*k.*y.^2);
figure(3); plot(t,E,'ro-');% time series for E
title('E(t) vs. time plot for c>0')
xlabel('time (t)')
ylabel('E(t) plot')

grid on; axis tight;
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 for 0<t<15
y = Y(:,1); v = Y(:,2); % retrieve y, v from Y

E=((1/2).*m.*v.^2)+((1/2).*k.*y.^2);
figure(4); plot(t,E,'bo-');% time series for E
title('E(t) vs. time plot for c<0')
xlabel('time (t)')
ylabel('E(t) plot')
grid on; axis tight;

fprintf('\nFor question 4a)\n')
fprintf('\tFrom the energy plot it can be shown that the energy is decreasing gradually and E conserved.\n')
fprintf('Due to friction energy is dissipated.\n')

fprintf('\nFor question 4b)\n')
fprintf('\tFor C>0 Energy gradually decreases so dE/dt<0.\n')
fprintf('\tFor C<0 Energy gradually decreases so dE/dt>0.\n')

fprintf('\nFor question 4b)\n')
fprintf('\tFrom phase plot, graph get close to origin\n')
fprintf('As velocity decreases and energy dissipates it gets close to origin.\n\n')
%---------------------------------------------------
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


%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%


Related Solutions

Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){ localSum+=...
This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
use a matlab built-in function to numerically solve: dy/dx= -x^2+((x^3*e^-y)/4) for 1<=x<=5 with y(1)=1 plot the...
use a matlab built-in function to numerically solve: dy/dx= -x^2+((x^3*e^-y)/4) for 1<=x<=5 with y(1)=1 plot the solution
MATLAB CODE: A particular 3x3 linear transformation A has eigenvalues of -1, 2, and 4, and...
MATLAB CODE: A particular 3x3 linear transformation A has eigenvalues of -1, 2, and 4, and corresponding eigenvectors with directions of [1;1;0], [0;1;l], and [1;1;1], respectively. Because eigenvectors must have unit length, you will need to normalize the given vectors by the length of each array to find the actual eigenvectors. Given this information, and using the eigen decomposition, find the linear transformation A. Hint: you can use the diag() function to form the D matrix from the provided eigenvalues....
Question 5. Use MATLAB to solve for and plot the response of the following models for...
Question 5. Use MATLAB to solve for and plot the response of the following models for 0≤t ≤1.5, where the input is f (t) =5t and the initial conditions are zero a. 3¨ x +21˙ x +30x = f (t) b. 5¨ A (Turn in the MATLAB script and answers from MATLAB, .m file, screen shots if needed) B (Turn in the MATLAB plot with t being time in SI units) C Comment on the response the analytical solution compared...
Practice for Matlab. You can create own f(x). a. Write code to find and plot a...
Practice for Matlab. You can create own f(x). a. Write code to find and plot a root using the modified secant method b. Find the roots using the roots command - identify and plot root of interest c. Use polyder and roots to find and plot the minimum value d. use fminbnd to find and plot minimum value Thank you!
Please show MATLAB code to plot below function from x = −3 to x = 12:...
Please show MATLAB code to plot below function from x = −3 to x = 12: function f = piecewise(x) % implements piecewise function using if statements if x < 0     f = -x^3 - 2*x^2 + 3*x; elseif x <= 8     f = (12/pi) * sin(pi*x/4); else     f = (600*exp(x-8))/(7*(14 + 6*exp(x-8))) -30/7; end
using matlab, compute and plot y [n] = x [n]* h [n], where a. x [n]...
using matlab, compute and plot y [n] = x [n]* h [n], where a. x [n] = h [n] = a^n (0 <=n <=40) & a = 0.5 b. x [n] = cos [n]; h [n] = u [n]; n = 0:4:360 c. x [n] = sin [n] ; h [n] = a^n; n:4:360; a = 0.9
what is the code to solve and graph the following function in matlab dv/dt=80.5(1-e^(.4t))
what is the code to solve and graph the following function in matlab dv/dt=80.5(1-e^(.4t))
4. Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify...
4. Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify the code to create a function named ‘decryptECB(key, ciphertext)’ that accepts key and ciphertext and returns a plaintext. from base64 import b64decode from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Util.Padding import unpad # We assume that the password was securely shared beforehand password = input ("Enter the password to decrypt the message: ") key= pad(password.encode(), 16) ciphertext = input ("Paste the cipher...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT