E5 Lab 5

Upon completion of the lab you should be able to use MATLAB to

At the end of the lab you will turn in a MATLAB script (.m file) with code in several cells (either 5 or 6).  Each cell will be the solution to a different part of the lab (marked by magenta arrows: ).  See the end of this web page for directions on what, precisely, needs to be turned in.

Feel free to consult with each other but don't ask for (or provide) completed solutions.


Getting extra help:

If you find that you would like extra help, the following resources are available (in no particular order):


Matrix Applications

Solving systems of linear equations.

Matrices can be used to solve multiple simultaneous equations.  For example, consider the problem of solving 3 simultaneous equations:

This can be written in a matrix form as

where

Verify for yourself that the preceding statement is true (i.e., show that Ax=b represents the set of equations)..

You can solve these equations by various substitution methods, or we can use matrices where A-1 indicates the inverse of the matrix A

Recall:

 To do this using MATLAB, do the following (the "inv" function calculates the inverse of a matrix).

>> A=[1 2 3; 1 -1 2; 3 1 -1];
>> b=[0 -2 5]';
>> x=inv(A)*b
x =
    1.0000
    1.0000
   -1.0000

which is the correct solution.

MATLAB Cell 1:  Go to the MATLAB command window and choose File→New→Script.  In this script write code in a cell that uses MATLAB to determine x1, x2, x3, and x4 for the following set of equations. 

Recall that the beginning of a "cell" is a line of code that starts with "%% " (two percent marks and a space - the space is important). You might want to add code at the top of the cell like the following

disp(' ');  disp('Cell 1');

This will print a line in the MATLAB command window identifying the cell. This will be useful later when you run the code when it has multiple cells.

Static Equilibrium

A picture hangs on the wall from two strings as shown.

The picture has a mass of two kilograms.  The forces T1 and T2 are the tensions in the string, and act as vectors.  The horizontal force in T1 is equatl to T1·cos(30°) to the right, and the horizontal force due to T2 is T2·cos(60°) to the left.  These forces must sum to zero; this is the top equation.  Likewise we can sum the forces acting in the vertical direction (including gravity) to get the second equation.

MATLAB Cell 2:  Formulate the problem as a matrix problem and find T1 and T2, using MATLAB. 

Recall that the beginning of a "cell" is a line of code that starts with "%% " (two percent marks and a space - the space is important).

(note: you'll have to move the "mg" term to the right hand side of the second equation).   You can use MATLAB to find the sine and cosine, but first you must either convert the angle to radians:

>> cos(30*pi/180)
ans =     0.8660
 
>> sin(30*pi/180)
ans =    0.5000

or use the "cosd" and "sind" forms

>> cosd(30)
ans =
    0.8660
 
>> sind(30)
ans =
    0.5000

or you could use the fact that cos(30°)=(√3)/2=0.866.or, with  (using MATLAB's SQuare RooT function - "sqrt").

>> sqrt(3)/2
ans =
    0.8660

Iterating in MATLAB

To solve the next problem that we will consider using MATLAB, we will need to use a loop.  Go to the MATLAB command window and choose File→New→Script.  (or use the script that you already have open). Type the following lines into the window that appears:

Save the script (File→Save from the Editor Window), and make sure to give it a unique name.  Now go to Debug→Run (also in Editor Window).  If you go back to the MATLAB command window, it looks like this:

x =
     0
x =
     2
x =
     6
x =
    12
Final x
x =
    12

The for loop iterates the variable i from 0 to 6 with an increment of 2. The first value of "x" is 0.  The next is incremented by 2, then 4, then 6 (as the variable "i" goes from 0 to 2 to 4 to 6).   The syntax of the for loop is

 for variable=start:increment:finish,
   ... MATLAB commands ...
end

Fibonacci's sequence

There is a sequence of numbers that comes up frequently in mathematics called Fibonacci's sequence.  The first two numbers in the sequence are 0 and 1.  After that we define the nth number in the sequence as the sum of the previous two:

We can use this expression to find the third and fourth elements in the sequence

MATLAB Cell 3:  Write MATLAB code that uses a loop to calculate and display the third through tenth Fibonacci numbers (you may also display the first two elements if you wish).  Think about this, it doesn't take much code.  If you are having trouble, try explaining verbally how you would solve the problem.

Make sure that the code for this cell goes into the same script file that you used previousley for "Cell 1" and "Cell 2."

Simulating Physical Systems in MATLAB

Consider the following code (available in the file xy_vel.m). 

If you put the code into a script (or download xy_vel.m) and run it you'll see a simulation of an object moving with a specified velocity in the x-y plane; there is no friction or gravity.    You can see that the velocity in the y direction is faster than the velocity in the x direction because the slope is greater than 45° (45° is the slope you would expect if the velocity was equal in the two directions).

 

Your job is to add code so that the simulation adds gravity in the y direction.  It might be helpful to refer to the code from class that is available in the pdf of the last lecture that is on the moodle page.

MATLAB Cell 4:  Write MATLAB code that shows the trajectory of the object from the example above in the presence of gravity (i.e., an acceleration in the y direction of 9.8 m/sec² downwards).  Make sure the code is well commented.

In Physics class you either have already proven, or will prove, that maximum range of a projectile is achieved when the initial trajectory is at 45°. 

Assume the object in the code has an initial speed of 60 m/sec.  Given the initial angle of the trajectory it is possible to use this to find the initial velocities in the x and y direction using trigonometry.  Add a for...loop to your code that displays the trajectory for angles of 30°, 45°, and 60°

MATLAB Cell 5:  Add a second for...loop something like either one (but only one) of the examples below to your code so that the three trajectories are plotted and demonstrate that an initial angle of 45° is better than either 30° or 60°.    Your code need not look precisely like either examples - they are just to show two possible methods of accomplishing the task.

% example 1
for theta=30:15:45
   ...MATLAB commands...
end

% ... or example 2
theta_vector=[30,35,60]
for i=1:3
   theta=theta_vector;
   ...MATLAB commands...
end

You  should have two nested four loops in your final code.  The animation should show one complete trajectory (e.g. at 30°) from beginning to end followed by another...  However, at the end of the animation all three trajectories should be shown on the plot.  The graph should look something like the one below (it need not be exactly the same).   If you want you can use different markers for the three trajectories (as was done in class and below) but this is not necessary.

You may stop here, but see the end of this web page for directions on what, precisely, needs to be turned in.

Challenge Problem - the trajectory of an object with friction.

You only need to do this if you have time and are looking for an extra challenge.  It is quite a bit more difficult than the problems above.

Consider a falling object (no horizontal velocity).  If the object is in a vacuum, the acceleration of the object is equal to the gravitational constant, g (9.8 m/s2).  But in the real world there is also a retarding force on the object due to air friction.  This force is called drag.  To a first approximation, let us assume that drag is proportional to the velocity of the object such that Fdrag=bv.  In this equation "Fdrag" is the drag force, "v" is the velocity and "b" is the proportionality constant between velocity and drag.  So the object now has two forces on it, gravity (Fg) and drag.

The sum of forces on a body is equal to mass times acceleration, so we get

(where the acceleration is positive in the downward direction).   We can now solve for acceleration.

There is a 1 kg mass projectile, moving in two dimensions, with initial velocities

vx(0)=50 m/sec, to the right, and
 vy(0)=50 m/sec upward

and drag coefficient, b=0.1.

MATLAB Cell 6 (optional): Plot the trajectory of the object from launch until it comes close to  hitting the ground.  Make sure your code is well commented.  You'll have to use vx(t) and vy(t) to find x(t) and y(t), then plot y(t) vs x(t).

This isn't quite as easy as it seems at first.  The force due to the drag is proportional to the total velocity of the projectile, and is in a direction opposed to the motion of the projectile.  So first you'll have to find the magnitude of the velocity, and then use that to find the magnitude of the drag force.  Then you can resolve the drag force into its x and y components, and use that information to find the acceleration in the x and y directions (use that to find velocities, and use that to find position).

To do this you'll need the square root function, "sqrt()".

Hints/techniques to consider while debugging your script:

To turn in

(via moodle)

Turn in a single MATLAB script with multiple cells.  Each cell is devoted to one of the assigned tasks (magenta arrows, ).  There is no need to turn in a "published" Word document.  Grading of each cell is on a ten point scale: