# Lab 6

## Tips / Reminders

To format (line-up) if statements/for loops/ while loops, press CTRL-A (Highlight everything), CTRL-I (Format selection).

FUNCTIONS

If I have a function with the following first line:

function [out1, out2] = myFunction(in1, in2, in3)

Then:

This line will call the function with 0 outputs, and 1 input:

myFunction(1)

And this line will call the function with 1 output, and 1 input:

[a] = myFunction(1);

And this line will call the function with 2 outputs, and 1 input:

[a,b] = myFunction(1);

And this line will call the function with 2 outputs, and 2 inputs:

[a,b] = myFunction(1,2);

Etc...

Look at lab5 Q4 and Q5 for more help.

## Simulation

In this lab, we will build a hockey table.

We'll start by learning how to manipulate graphic objects in MATLAB. The function plot, as you know, generates a visual representation of some data. If we call, for example, plot(0,0,'o'), a blue circle will appear at (0,0). Several things happened here: a new figure window was created, a new system of axes was created in this figure, and a new line was created in these axes. In this case, the "line" has only one data point. MATLAB allows us to manipulate the properties of all these objects - you might have used the function set(gca,...) in Lab 3 to manipulate your graphs. The function gca or Get Current Axes gets a handle for the axes and the set function sets the desired property. Just like physical handles allow you manipulate physical objects, handles in MATLAB allow you to manipulate MATLAB objects.

In this example, we'll manipulate the properties of the "line" object. If we call h=plot(0,0,'o'), then the variable h will be our handle to the circle created by the plot command. We can now alter the properties of this line object through its handle. For example, we can change the circle to a star by saying set(h,'Marker','p').

 lab6_1.m: Create a plot with a single blue circle and change the circle to a red star. Hint: If h is a handle, then get(h) shows all of the properties for that handle. Hint: See help plot for shapes and colors.

We also want to move our point around. The position in the plot was originally determined by our input to the plot command (in this case, x=0 and y=0). This input was stored in the properties of the line called 'XData' and 'YData'. (In the general case, these are vectors containing the x-coordinates and y-coordinates of the data, respectively.)

 lab6_2.m: Re-locate the red star to (1,1). Hint: If you update the properties and the figure doesn't change, try calling the function drawnow.

We are ready for some animation. In order to create an animation we must use a loop to repeatedly update the plot. The two most common loops are for loops and while loops. Here are two examples that sum the numbers from 1 to 10.

```sum = 0;                    % Initialize our sum to 0
counter = 1;                % Initialize our counter
while (counter <= 10)
sum = sum + counter;    % Add to the sum.
counter = counter + 1;  % Increment our counter.
end ```
```sum = 0;                    % Initialize our sum to 0
for counter = 1:10
sum = sum + counter;    % Add to the sum.
end ```
 lab6_3.m: Create a script that uses a loop to create an animation of a filled green circle moving across the screen. Don't just call plot in a loop again and again! Instead, update the position of the circle in the existing plot using a handle. To make sure that the intermediate steps of your animation appear, use the drawnow command after each update. To prevent your animation from running too fast, use the pause command, e.g., pause(0.01). If you don't use the axis command to lock the axes, your circle will always be plotted in the middle and the axes will change to compensate.

Now, let's talk about basic mechanics. A point object in a 2D world has a position which we can represent with a 2D vector (the first entry is the X-coordinate, and the second entry is the Y-coordinate). A point object also has a velocity which has both magnitude and direction We can represent velocity as a 2D vector (the first entry is the velocity in the X dimension - the projection of the velocity onto that axis; the second entry is the Y-component of the velocity). Recall that veclocity is measured in units of distance per unit of time.

Suppose we are given an initial position and velocity at time 0 and we want to know where the object will be at time t=0.3. If there is no acceleration and the velocity is constant, and we can simply calculate the new position by pos_new=pos_init+0.3*v_init. However, in the case of a hockey table, the puck cannot go outside the boundaries. How should we deal with this? First of all, we take small time steps and check at each time step whether the puck is outside the boundaries. As long as the puck stays away from the walls, there's no problem. If the puck's new position puts it outside of the wall, we need to correct it. Let's assume that the collision of the puck with the boundary is totally ellastic (i.e. no velocity is lost, but its direction changes). If the puck crosses a vertical boundary we need to "reflect" its position with respect to this boundary. Assume that the boundary is at the line x = 0. If pos(1) < 0, then pos_new(1) = -1*pos(1), which by definition is again within the boundaries. We need to remember to change the velocity. In the Y-direction, it will stay the same, and in the X-direction, it will change sign (reverse direction). The math of reflecting the puck with respect to the boundaries that are not x = 0 or y = 0 is slightly more tricky.

 Q1: Using "pen and paper," what are the required change if the puck hits a boundary at y = 2 or x = -2. lab6_4.m: Create a simulation of a 3 by 5 hockey table. The puck starts in the center of the table and should bounce off three distinct walls in a reasonable number of steps. Create a function that takes in the current position, velocity of the puck, and time step, and returns the new position. Define an initial position and velocity and create a loop that updates the position of the puck using your function and shows the result. Write a function that takes the new position, velocity and boundaries and returns a corrected position and velocity. Each boundary should be stored in a variable so you can change the boundaries of the table by changing these variables. Your code should have this structure: ```initialize position initialize velocity plot the initial positions of the pucks and store the handle ... start loop update the positions check the position and velocity of puck "idx" update the plot using the handle ... end loop ```

An example:

 Demonstrate Q1 and lab6_1.m, lab6_2.m, lab6_3.m, lab6_4.m and your two functions to the TA or instructor.