function test1
% Solving F=ma to find position
clear all
%initial conditions
 t0= 0; x0 = 1;  v0 = 0; 

%%parameters
m = 1;  %value of mass
k = 1;  %value of spring constant
g = 10; %gravity
c = 1;  %drag constant
h =  .0001;   % step size = delta t
t0 = 0;  t1 = 2*pi;  % interval to solve odes


n= (t1-t0)/h;  %number of time steps
n = floor(n);
% initialize
tnew=t0; xnew = x0;  vnew=v0; 

tarray= zeros(1,n+1);xarray=zeros(1,n+1); varray=zeros(1,n+1);
Etotarray  = zeros(1,n+1);
tarray(1)=t0;      xarray(1) = x0;    varray(1) = v0;
Etotarray(1) = (x0^2 + v0^2)/2;
%%%%%%%%
for i = 2:n+1
x = xnew; v = vnew; t = tnew;


F = -k*x; % the system properties are defined here !
%F = m*g - c*v;
F=  rhs(x,v,t);

a = F/m;          % the laws of mechanics
xnew = x + v*h;   % kinematics
vnew = v + a*h;   % kinematics
tnew = t + h;
Etot= (vnew^2 + xnew^2)/2;

tarray(i) = tnew;
xarray(i) = xnew;
varray(i) = vnew;
Etotarray(i) = Etot;
end

figure(1)
plot(tarray, xarray, 'g')
xlabel('time'); ylabel('position');
title('Andy''s plot','fontsize', 20, 'color','b')

figure(2)
plot(tarray, Etotarray, 'b')
xlabel('time'); ylabel('Total Energy');
title('Andy''s 2nd plot','fontsize', 20, 'color','b')

end




function  F=rhs(x,v,t)
F = -x;
end