Consider the generic homogeneous spring-mass system
function dummy=ex_with_2eqs()
clc;
clear all;
t0 =0; tf = 4;
x0=0.1;
y0=0;
u0 = [x0;y0];% initial condtion
gama=0;
[t,y] = ode45(@f,[t0 tf],u0,[],gama);
u1 = y(:,1);
gama1=0.1;
[t,y1] = ode45(@f,[t0 tf],u0,[],gama1);
u2 = y1(:,1);
gama2=1;
[t,y2] = ode45(@f,[t0 tf],u0,[],gama2);
u3 = y2(:,1);
gama3=3;
[t,y3] = ode45(@f,[t0 tf],u0,[],gama3);
u4 = y3(:,1);
%%% Solution Plot
figure(1);
plot(t,u1,'b');
hold on
plot(t,u2,'r');
hold on
plot(t,u3,'m');
hold on
plot(t,u4,'k');
xlabel('t')
ylabel('y')
legend('\gamma=0','\gamma=0.1','\gamma=1','\gamma=3')
end
%----------------------------------------------------------------------
function dydt = f(t,y,gama)
k=1;
m=1;
%%%Here u is y(1), v is y(2),
u = y(1); v = y(2);
dydt = [v ;-(k/m)*u-(gama/m)*v];
end
Get Answers For Free
Most questions answered within 1 hours.