Lab 8: Back to physical simulations

Several labs ago, we simulated a block on a spring attached to a wall, and we saw how that block acted if we pulled. This week we are going to model an elastic string as a large collection of stones, attached to each other with springs, and see what shapes this becomes. First, some physics. Imaging a stone hanging on springs connected to (immovable) anchors on two walls:


It will eventually come to rest at the position (x,y) where:

Fleft + Fright + Fgravity = 0

We will figure out what Fgravity should be later, but Fleft and Fright will be calculated using Hooke's law:

F = -k * displacement,

where x is the displacement (how stretched the spring is), and the force x is now a vector pointing towards the other endpoint of the spring.

At the moment, let us assume that the spring constant "k" is 1, and that the "unstretched" length of the string is 0.5, so the displacement = (distance between two endpoints of string - 0.5)

Task 1:
Simulate the above physical system.

First, compute Fleft and Fright for a stone at position (x,y). The below lines of code compute the force to the left. Test them, then put the into a function called computeFLeft (what should this function return?). This function should take in parameters (x,y,k)

dL = sqrt(x.^2 + y.^2);  % distance to left anchor
F = -k * (dL-0.5);       % compute the force.

vx = (x)./dL;
vy = (y)./dL;            % unit vector in direction towards left anchor

Fx = F * vx;             
Fy = F * vy;             % make the force vector. 

Make a similar function computeFRight that computes the force to the right anchor. When you get these right, they should give the following results:

[Fx Fy] = computeFRight(0.5,-0.1,1)
Fx =
    0.0097
Fy =
    0.0019
>> [Fx Fy] = computeFLeft(0.5,-0.1,1)
Fx =
   -0.0097
Fy =
    0.0019

Now, to complete this task, we are going to simulate the motion of the stone, assuming that it starts with 0 velocity at position (0.3,0), and is acted on by the forces to the left, to the right, and gravity. We will use a value of 4 for "k". To do this, write a script which does the following:

% initialize position and velocity of the stone, and the value of "k"


% initialize a time step dt


% For loop, for 1000 steps:   

  % Compute Fleft for the current position


  % Compute Fright for the current position


  % Compute Fgravity for the current position (assume the stone has
  %   weight of 1 and use 9.8 m/s/s  (so Fgravity will be 9.8).


  % Sum these vectors to get the total force.


  % Compute the total accelaration vector (F = ma, so, a = F/m);


  % use that acceleration to update the velocity (make sure to get
  % both the x and y components)


  % use that velocty to update the stone position


  % make a plot which shows the anchor positions and the stone (and
  %   the springs)

% Hint, do your plot commands, then use " axis([0 1 -2 1]) " before
% your drawnow command to give it a good window to plot 

Now run your simulation, does it do what you expect?

The reason that it doesn't ever "calm" down is that there is no friction in the system so it will continually vary. We can add friction in a cheap way with the lines:

vx = vx .* 0.99;
vy = vy .* 0.99;
Experiment with different values of "dt" until you find one where the system is approximately stable after 1000 steps.

Show this simulation to a TA for the first check-off

Task 2
Now, i'd like to run the same simulation, but with 8 stones between the two end walls. It is probably easiest to consider the anchors as "stones that don't move". This means that you need to modify your script in several ways:
  1. you need to initialize the position and velocity for all 10 stones.
  2. you need to change the computeFLeft and computeFRight commands
    1. to accept a vector of x,y positions.
    2. to compute the distance to the "next" stone rather than to the anchors
    3. to return the forces for each stone to the left/right
    4. to use an "expected" spring distance of 0.05 (instead of 0.5)
    5. to a "k" value (for the spring constant) of 60
  3. you need to change the rest of your updates to work on all the stones at once, and update your display code.

    Experiment again with different parameters for dt and when you find one that lets the set of springs settle down in about 1000 steps, show this simulation to a TA for the final check-off