You are on page 1of 2

clear all;

dt = 0.1; %Time step

M=[1 0; 0 2];
K=[8 -3; -3 4];
R=[10 0]';
invM=inv(M);

%ICs
T(1)=-dt; %at time '-dt'
u1(1)=5*dt^2;
u2(1)=0;
T(2)=0; %@t=0
u1(2)=0;
u2(2)=0;

%Step by Step Loop

nStep = 4/dt; %Time step to 4

for i=1: nStep


curU(1)=u1(i+1);
curU(2)=u2(i+1);
lastU(1)=u1(i);
lastU(2)=u2(i);
nextU = dt^2*invM*(R- K*curU' + 2/dt^2*M*curU' - 1/dt^2*M*lastU');
u1(i+2)=nextU(1);
u2(i+2)=nextU(2);
T(i+2)= T(i+1)+dt;
end

figure(11);
axes('fontsize',18);
plot(T, u1,'ro-', T, u2,'b.-');
xlabel('time');
ylabel('u_1 and u_2');
xlim([0, 4]);
grid on;

%Analytical solutions for comparison


hold on;
num=length (T);
for n=1: num
time=T(i);
a1(n)=2.2847*(1-cos(1.1514*time))*(0.3029) + 1.0987*(1-
cos(2.9452*time))*(0.9530);
a2(n)=2.2847*(1-cos(1.1514*time))*(0.6739) + 1.0987*(1-
cos(2.9452*time))*(-0.2142);
end

plot(T, a1, 'g');


plot(T, a2, 'k');

You might also like