Practice:
The rand function in MATLAB returns a randomly selected number between 0 and 1. Let's write a "coin flip" simulation script.
First use rand to pick a random number x: x = rand()
Then use an if statement to determine whether x is greater than or less than 0.5. If x is greater than 0.5 have the program print "HEADS". If x is less than 0.5 have the program print "TAILS".
HINT: Use fprintf to print the result, e.g. fprintf("HEADS\n")
Assignment:
The equation of motion of a pendulum is given by:
where A is the angle of the pendulum from the vertical and l
is the length of the pendulum.
In the limit that the angle, A, is small then A itself is a good
approximation to sin(A) and we may write the equation of motion:
This equation has a well known solution. If we start the pendulum from
rest at an initial angle A0 at an initial time t=0,
then the solution is just:
Write a script to evaluate the above equation for times t=0, 1, 2, 3, ... 10 and where
A0 = 1; g = 9.8; l = 4.
You must use a for loop to evaluate the value of A for
each value in the t array.
HINT: The cosine function in MATLAB is called cos
and it takes radians as its argument. The square root function is
called sqrt.
The name of the program file should be something like exercise2.m
so that it can be distinguished from other submissions.
Please write your name in a comment field
near the beginning of the script.
Copy your completed program into the directory ~schloerb/ph281/username, where username is your user name.
See this link for examples of how to
copy a file to this directory.