%Prelim 2 prob 2
k1 = 3; k2 = 7; k3 =  9;
m1 = 2; m2 = 5; m3 = 34;
x0 = [1 1 1]';  % initial condition
t  = linspace(0,50, 500);

% Mass matrix
M = diag([m1 m2 m3]);

% Stiffness matrix
K = [ (k1+k2)   -k2      0
       -k2    (k2+k3)   -k3
       0        -k3      k3];

% Find normal modes
[V D] = eig(K,M);

% Find frequencies
omega = sqrt(diag(D));

%n  = 1;      % can use 1,2 or 3
%x0 = V(:,n); % this would give a sine wave
r=   V\x0;    % Amplitudes for given ICs

x1 = V(1,1)*r(1)*cos(omega(1)*t) ...
    +V(1,2)*r(2)*cos(omega(2)*t) ...
    +V(1,3)*r(3)*cos(omega(3)*t);

hold all

plot(t,x1);
