#   1. DEMO -  Damped Oscillation

# Here we model the motion of a body hanging on a spring, 
# after giving an initial velocity to it and supposing friction. 
# You can change the parameters and see how changes
# the graph of the motion. 

func x,v; # displacement and velocity
const D,m,k; # constant of the spring, mass, coefficient of friction
aux F,a;   # force, acceleration

x'=v; # the velocity is the derivative of the displacement

F=-D*x-k*v; # force of the spring supposing friction
a=F/m; # Newton's second Law

v'=a; # the acceleration is the derivative of the velocity

endmodel;



x=0;                 # initial	displacement
v=6.283185307;       # initial velocity (2*phi)

D=10;  # the constant of the spring [N/m]  
m=1;  # mass [kg]  
k=1; # coefficient of friction

rerr(x,0.00005);   # error bounds of the solution
rerr(v,0.00005);

run(0,5,0.1,x,v,a);  # interval of the modelling

print(table,0,5,0.1,x,v,a); #  table

plot(graph,0,5,0.04,x=x(-2,2),v=v(-7,7),a=a(-20,20)); # graph

