Write a MATLAB code to solve the following problem.
The forced oscillations of a body of mass m on a spring of modulus k are
governed by the ODE
my" +cy' +ky = r
Find the steady-state solution for y(t), if m = 1 kg, c == 0.05g/sec and k =
25 g/sec. Where,
r(t) = {t + pi/2 -pi <= t <= 0; t - pi/2 0 <= t <= pi }
where, r(t) = r(t + 2pi).
%Main Code
close all,
clear all,
clc,
%{
The forced oscillations of a body of mass m on a spring of modulus k are
governed by the ODE my" +cy' +ky = r
Find the steady-state solution for y(t), if m = 1 kg, c == 0.05g/sec and k =
25 g/sec. Where,
r(t) = {t + pi/2 -pi <= t <= 0; t - pi/2 0 <= t <= pi }
where, r(t) = r(t + 2pi).
%}
m = 1;
c = 0.05;
k = 25;
t = -pi:0.1:pi;
initial_x = 0;
initial_dxdt = 0;
[t,y] = ode45( @rhs, t, [initial_x initial_dxdt] )
plot(t,y(:,1));
xlabel('--- t --->');
ylabel('--- y --->');
grid on,
function dydt=rhs(t,y)
m = 1;
c = 0.05;
k = 25;
r=[];
for n=1:length(t)
if(t(n)<=0), r(n)= t(n)+(pi/2); end
if(t(n)>0) , r(n)= t(n)-(pi/2); end
end
dydt_1 = y(2);
dydt_2 = -(c/m)*y(2) - (k/m)*y(1) + (r/m);
dydt=[dydt_1; dydt_2];
end
%{
The forced oscillations of a body of mass m on a spring of modulus k are
governed by the ODE my" +cy' +ky = r
m*d2y/dt2 +c*dy/dt + ky = r
Find the steady-state solution for y(t), if m = 1 kg, c == 0.05g/sec and k =
25 g/sec. Where,
r(t) = {t + pi/2 -pi <= t <= 0;
t - pi/2 0 <= t <= pi } where, r(t) = r(t + 2pi).
%}
Comments
Leave a comment