PLease use matlab to solve this Question. Consider an oscillating spring: The mo
ID: 2079932 • Letter: P
Question
PLease use matlab to solve this Question.
Consider an oscillating spring: The movement of the mass is described by: F = -kx; F = ma = ma= d^x/dt^2; m d^2x/dt^2 + kx = 0 The solution for the differential equation is: x(t) = A.cos omega t omega = squareroot k/m x: displacement at time t (sec) A: maximum displacement (m) omega: angular frequency (rad/sec) f: frequency (sec^-1) f = f = omega/2 pi T: time period (sec) T = 1/f Write a program to compute the position of the mass for time values from 0 to r (increments of 0.1 seconds). Your program asks for values of A, m, k and r as inputs. The output of the program is a print of values of x, one second apart, along with their time values. The precision is 0.01 (meaning 2 digits after the decimal point). Plot x versus time. Test your program with values of A = 4cm, m = 0.1kg, k = 0.158N/m, and r = 12sec. Convert the program to a function position(A, m, k, r, inc), that outputs the vector x for time values between 0 and r. Test the function by a short program that inputs the test values of (a), calls the function, and print and plot x values as in (a). Write a short program that calls the function "position" 3 times to compute x for A = 4cm, k = 0.158N/m, r = 12sec, and three different value of m for each calling of "position", m1 =0.1 kg, m2 = 0.2kg, m3 = 0.3kg. The program plots the three outputs on one single plot x-label, y-label, title, and legend.Explanation / Answer
a)
%START OF QUESTION a
clc;
close all;
clear all;
format long
A = input(' Enter the value of A:');
k = input(' Enter the value of k:');
m = input(' Enter the value of m:');
r = input(' Enter the value of r (time range):');
t = 0:0.01:r;
w = sqrt(k/m);
xt = A*cos(w*t);
j = 0;
for i = 0+1:0.01:r+1;
j = j+1;
if rem(i,1) == 0
fprintf('value of xt is %0.2f value of t is %0.2f ',xt(j),t(j));
end
end
figure(1);
plot(t,xt);
%END OF QUESTION a
b)
%START OF QUESTION b FUNCTION
function [out1,t] = position(A,m,k,r,inc);
format long
t = 0:inc:r;
w = sqrt(k/m);
out1 = A*cos(w*t);
j = 0;
for i = 0+1:0.01:r+1;
j = j+1;
if rem(i,1) == 0
fprintf('value of xt is %0.2f value of t is %0.2f ',out1(j),t(j));
end
end
%plot(t,out1);
end
%END OF QUESTION b FUNCTION
Save it as function with name 'position'
%START OF QUESTION b program
clc;
close all;
clear all;
%QUESTION b
format long
A = input(' Enter the value of A:');
k = input(' Enter the value of k:');
m = input(' Enter the value of m:');
r = input(' Enter the value of r (time range):');
[out,tj] = position(A,m,k,r,0.01);
figure(1);
plot(tj,out);
%END OF QUESTION b program
c)
%START OF QUESTION c
clc;
close all;
clear all;
%QUESTION c
format long
A = 4E-2;
k = 0.158;
r = 12;
m1 = 0.1;
[out1,t1] = position(A,m1,k,r,0.01);
m2 = 0.2;
[out2,t2] = position(A,m2,k,r,0.01);
m3 = 0.3;
[out3,t3] = position(A,m3,k,r,0.01);
figure(1);
%subplot(3,1,1);
plot(t1,out1,'r',t2,out2,'b',t3,out3,'m');
xlabel('time');
ylabel('x(t)');
title('displacement ');
legend('x(t) for m1=0.1kg','x(t) for m1=0.2kg','x(t) for m1=0.3kg');
%END OF QUESTION c
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.