dt = .01; //Make this smaller for better accuracy k1 = 10; k2 = 30; //First give the variables some initial values x1=0; //initial positions of trolleys x2=50; vx1=0; //initial velocities vx2=0; running=true; StepModel(); //Start the simulation running
// Mass 2 is accelerated by force in spring 2 vx2 += dt * (k2 * (x1-x2)); //Mass 1 is accelerated by forces in springs 1 and 2 vx1 += dt * (-k1*x1 + k2* (x2-x1)); //Update the positions x1 += dt * vx1; x2 += dt * vx2; MoveTrolleys(x1,x2); if(running){setTimeout('StepModel()',10);}