Lab 5: Intro to physical simulation
Today we are going to break from things we've been talking about in class and look at simulating physical processes. In particular, we are going to write functions which simulate simple Newtonian physics. In particular, we are going to think about launching an object and simulating its trajectory. First, if we want to model the flight of on object, what things do we need to represent (what are our variables going to be?). Second, how are these initialized? Third, how are they updated? and Fourth, how can we visualize them? The lab today will help work through those questions, but in the future i'll continue to ask you to do more and more of this yourself.
  1. First, if we want to model the flight of on object, what things do we need to represent (what are our variables going to be?). We need to model the position of the object, y. In order to update this, we will need to use the velocity v, and in order to update the velocity, we will need to use the accelaration a. We will suppose that a is constant.
  2. Second, how are these initialized? We will let the user specify starting conditions for all of these.
  3. Third, how are they updated? In the first step, we chose to maintain the position of the object "y". Even if we only represent the "current" position y, we can think about the variable y(t): Thinking back to physics, y(t) is a time varying function. The most common approach to understanding how a function varies is to take the "first order taylor series expansion". This says that near a time point t:
    y(t + dt) = y(t) + dy/dt * dt + higher order terms
    we commonly choose to make our simulation run at a time step where all the higher order terms are close to zero, so we ignore those terms. the other term (dy/dt) is the velocity, that we are representing with the variable v. So, in a small time step (from t to (t+dt)), how does y change?
    yNew = y + v * dt.
    Then, from one time step to the next, the velocity also changes.
    v(t + dt) = v(t) + dv/dt * dt + higher order terms.
    Since dv/dt = a, we can use:
    aNew = v + a * dt.
    and we can assume that a is a constant.
  4. and eventually we will plot the value of y as a function of time.

Task 1: Write a function to update the position and velocity of a particle. Your function should have the form:

function [yNew, vNew] = update(y,v,a,dt)
and take values of y,v,a and a time step dt, and return new values for y and v.

You should create for yourself 2 sample inputs and compute by hand the expected output, then verify that your program gives this output.

Show your calculations and your function as you check in with the TA.

Task 2:

Write a function that simulates the behavior of this over time, as long as the "y" position is positive. Your function should have the form:

function simulate(origY,origV,a,dt)

that takes initial values for y,v, and a, dt and continually calls your update function until y becomes less than zero. Your function should include a while loop.

Add to your function a call that plots the current value of (y,n) where n how many times you've called the update function.

Check in with a TA and show the plot.

Task 3:

Finally, I'd like to simulate the particle bouncing when it hits the ground. When the position becomes negative, flip the velocity from negative to positive. You could do this by modifying either the "simulate" function or the "update" function. Choose one function to modify and write one or two sentences about how/why you which to make the modification in that function.

Check in with a TA, show your justification, and show the resulting plot.