%numerically solve for displacement of a mass-spring system in 2D from the
%origin, the time-axis on the plots are scaled to make the graph better
%deltaT = 0.1 and there are 100 points
%spring constant = k, mass = m, initial displacement = ix, angle of
%displacement from origin = theta
%Author: Ankur Kumar (ak364)

function fig1 = springmass(k,m,ix,theta)

clf
%x-dim
%assume initial velocity to be 0
v=0;
T=0.1; %delta T
dx = []; %displacement
%x-dim displacement
x = ix*cos(theta);
%use F=ma=-kx (Hooke) and Newton
for n=1:100
    a = -k*x/m;
    v=v+a*T;
    x=x+v*T;
    dx(n) = x; %the displacement
end
fig1 = figure(1);
subplot(2,2,1);
tempx = max(dx)/100; %scale for t-axis
plot([0:99].*tempx,dx);

%y-dim
%assume initial velocity to be 0
v=0;
T=0.1; %delta T
dy = []; %displacement
%y-dim displacement
y = ix*sin(theta);
%use F=ma=-kx (Hooke) and Newton
for n=1:100
    a = -k*y/m;
    v=v+a*T;
    y=y+v*T;
    dy(n) = y; %the displacement
end

subplot(2,2,2);
tempy = max(dy)/100; %scale for t-axis
plot([0:99].*tempy,dy);
 
%the two dimensions are displacement and time
%parallel coordinates
map = winter(100);
subplot(2,2,3);
hold on
ndim = 2;
%the axes for parallel coords
axisx = [1;ndim];
axisy = [0;0];
label = [];
for i = 1:ndim
    axisx = [axisx,[i;i]];
    axisy = [axisy,[0;1]];
end
label = [{'t'},{'x'}];
line(axisx, axisy, 'color', 'b');           %draw the axes
set(gca, 'xtick',[1:1:ndim]);
set(gca, 'xticklabel', label);
vx=vertcat([0:99].*tempx,dx)'; %data points x-dim
for i=1:100
    plot(vx(i,:),'color',map(i,:));
end

subplot(2,2,4);
hold on
ndim = 2;
%the axes for parallel coords
axisx = [1;ndim];
axisy = [0;0];
label = [];
for i = 1:ndim
    axisx = [axisx,[i;i]];
    axisy = [axisy,[0;1]];
end
label = [{'t'},{'y'}];
line(axisx, axisy, 'color', 'b');           %draw the axes
set(gca, 'xtick',[1:1:ndim]);
set(gca, 'xticklabel', label);
vy=vertcat([0:99].*tempy,dy)'; %data points y-dim
for i=1:100
    plot(vy(i,:),'color',map(i,:));
end
hold off
