Resources

Spring and Damper Continuous System

/*--------------------------------------------------------------

                    SAS Sample Library

        Name: modex07.sas
 Description: Example program from SAS/ETS User's Guide,
              The MODEL Procedure
       Title: Spring and Damper Continuous System
     Product: SAS/ETS Software
        Keys: nonlinear simultaneous equation models
        PROC: MODEL
       Notes:

--------------------------------------------------------------*/

title1 'Simulation of Spring-Mass-Damper System';

/*- Data to drive the simulation time periods ---*/
data one;
   do n=1 to 100;
      output;
   end;
run;

proc model data=one outmodel=spring;
   var      force -200  disp  10  vel  0  accel -20  time 0;
   control  mass   9.2  c    1.5  dt  .1  k      20;
   force = -k * disp -c * vel;
   disp  = lag(disp) + vel * dt;
   vel   = lag(vel) + accel * dt;
   accel = force / mass;
   time  = lag(time) + dt;
run;

proc model data=one model=spring;
   title2 "Simulation of the model for the base case";
   control run '1';
   solve / out=a;
run;

   title2 "Simulation of the model with twice the initial displacement";
   control run '2';
   var disp 20;
   solve / out=b;
run;

data two;
   do time = 0 to 10 by .2; output;end;
run;

title2 "Simulation of the model using the dert. syntax";
proc model data=two;
   var      force -200  disp  10  vel  0  accel -20  time 0;
   control  mass   9.2  c    1.5  dt  .1  k      20;
   control run '3' ;
   force = -k * disp -c * vel;
   dert.disp  = vel ;
   dert.vel   = accel;
   accel = force / mass;
   solve / out=c;
   id time ;
run;

data p;
   set a b c;
run;

title2 'Overlay Plot of All Three Simulations';
proc sgplot data=p;
   series x=time y=disp / group=run lineattrs=(pattern=1);
   xaxis values=(0 to 10 by 1);
   yaxis values=(-20 to 20 by 10);
run;