Exercise 2.1: Fibonacci 1 (p.14)
For this exercise, we were told to enter the expression for the Fibonacci number in MATLAB and compute the 10th Fibonacci number. We were told that the correct answer should be 55.
The expression for the nth Fibonacci number was given as the following:
In order to make things look a little nicer, Christina and I decided to set the expressions inside the parenthesis (including the exponent) as variables a and b. We also set n as 10 as told in the problem.
Then we set the expression for Fn as ans so the console would print the Fibonacci number.
Here's a screenshot of our script:
and the command window:
As seen in the above picture, the Fibonacci number for n=10 is calculated as 55, which is the correct answer.
This exercise was pretty self-explanatory and was a good way to practice setting variables in MATLAB.
Exercise 2.3: Car Update (p.20-21)
Here's a quick recap of the given information:
We initially have 150 cars in both Albany and Boston. Each week, 5% of the cars in Albany are dropped off in Boston and 3% of the cars in Boston are dropped off in Albany.
We started out the script by defining variable a as the initial number of cars in Albany and variable b as the initial number of cars in Boston.
Then, we defined new variables anew and bnew to reflect the number of cars in the next week.
For Albany (anew), the equation would be the initial number of cars minus 5% of the initial number of cars plus 3% of the initial number of cars in Boston.
For Boston (bnew), the equation is the initial number of cars in Boston minus 3% of the initial number of cars plus 5% of the initial number of cars in Albany.
We used the 'round' command to round up the numbers to whole numbers (we are assuming that there are no fractional amount of cars)
Here's the command window after running the script shown above:
The first a and b is simply the initial number of cars defined in the beginning of the script, and the second a and b is the number of cars in Albany and Boston after the first week.
Now if we ran the same script over and over again, it would just give the same results because every time we re-run the script, we define the initial a and b as 150.
So if we get rid of the a=150 and b=150 after running the script once:
the a would be defined as the number of cars in Albany after the first week and b would we defined as the number of cars in Boston after the first week.
Now if we run the script again, the 'new' a and b values is used to calculate the anew and bnew for the second week. Running the script repeatedly will give us the number of cars in Albany and Boston week after week.
However, this process is very manual, and we would need to keep track of the amount of times the script was run in order to figure out which week the numbers represent.
The first set of a and b shown in the command window above is the number of cars after 1 week. The second set of a and b is after week 2, and the third set of a and b is after week 3.
To answer the question asked in the exercise, we thought the number of cars in Albany and Boston will eventually reach an equilibrium, where 5% of cars in Albany and 3% of cars in Boston is approximately equal.
We could prove this by repeatedly clicking the run button and looking at the results in the command window.
After multiple (couldn't keep track of the exact number) runs, the numbers reached equilibrium at a=118 and b=182.
Exercise 3.1: Car Loop (p.26)
For this exercise, we were finally allowed to use the for loop and asked to calculate the number of cars in Albany and Boston after 52 weeks (as continuation of the previous exercise). Using a for loop makes our lives much easier because we wouldn't have to manually run our script 52 times.
In the script, we simply defined the initial number of cars in Albany and Boston (as variables a and b) before the for loop.
Then, we let the for loop run from i=1 (after 1 week) until i=52 (after 52 weeks) and ran the car update script (the commented out parts under the car_updatescript shows what the car update script is)
After running the script, the command window showed a long list of all the 52 a and b values.
Just to make the a and b values a little easier to view, I made a new script using vectors:
Here, i=1 is defined as the index for the initial number of cars (150) in both Albany and Boston. So when i=2, it actually displays number of cars after the first week and when i=53, after 52 weeks.
The vectors 'Albany' and 'Boston' displays all of the a(i) and b(i) values from i=1 to i=53:
As predicted in the previous exercise, the number of cars reach equilibrium after about 21 weeks.
Exercise 3.2 Car Loop Plotting (p.27)
In order to make a plot of number of cars vs i, we modified the script from Exercise 3.1 so that it would plot a point every time it goes through the loop.
We simply added a plot function within the for loop so that it would plot i vs a as a red circle and i vs b as a blue diamond.
The resulting graph definitely made it easier to see that the numbers reach equilibrium. The equilibrium values displayed in the graph (a~120, b~180) agrees with the values obtained from the previous exercise (a=118, b=182) and we also see that i~20 when numbers reach equilibrium.
We also plotted a graph for initial values a & b=10000
As the graph shows, the equilibrium is reached at a slower rate. The numbers seem to reach equilibrium around i=50, and in order to verify this, I ran the loop from i=1:100 and plotted the results:
This new graph clearly shows that equilibrium is reached after about 50 weeks. The approximate equilibrium values are a=12,500 and b=7,500.
Also, the graph is smoother and resemble exponential functions.
I was curious to see what the plot looked like for low initial values, so I decided to make a plot with initial values of 50.
As the graph shows, the number of cars change linearly with i until it reaches equilibrium around i=7 (7 weeks).
Exercise 3.5: Fibonacci 2 (p.30-31)
The assignment asks us to create a loop that computes the first 10 elements of the Fibonacci sequence and assign the final answer as the 10th Fibonacci number.
We started out by defining the first and second Fibonacci numbers as 'prev1' and 'prev2'. Since we already have the first and second numbers, the loop was run from i=3 to i=10 in order to compute the next 8 numbers.
Inside the loop, the Fibonacci number was defined as F_i and was set equal to prev1 + prev2. This is simply a restatement of the definition given in the text.
Then, the value for prev2 was re-defined as prev1 because for the next Fibonacci number, the value of F(i-1) becomes F(i-2). Prev1 was re-defined as the Fibonacci number because the Fibonacci number becomes F(i-1) for the next loop.
For example, in order to compute F(3), we need F(2) and F(1). In order to compute F(4), we need F(3), which is simply the Fibonacci number computed previously and F(2), which is F(i-1) in the previous computation but F(i-2) in this computation.
The command window displays 55 as the 10th Fibonacci number, which is what we expected to get.
To make the script more general, we simply defined a variable n that represents the order of the Fibonacci number that we want to calculate. Then, we replaced 10 with n in the for loop.
In order to test the script, I set n=29 and compared to answer to the number that I Googled.
The command window tells me that the 29 Fibonacci number is 514229, which agrees with the Googled value.
Exercise 4.6: Plotting Fibonacci Ratios (p.46)
To start off, the Fibonacci sequence was re-written as vectors and we were able to display a beautiful vector of the first 10 Fibonacci numbers.Using vectors to define the Fibonacci sequence is a lot less confusing and easier to understand. We simply define F(i) as F(i-2) + F(i-1) and run the for loop. Since the first two Fibonacci numbers are given, we can start the loop from i=3. I defined 'sequence' as the vector F so that it would display all the values of F.
Now, it's time to calculate the Fibonacci ratios.
At the very beginning of the script, I defined a variable n, which is equal to the number of Fibonacci numbers we are calculating. The number of ratios being calculated is equal to n-1.
The first for loop simply calculates the Fibonacci numbers until the nth number.
The second for loop calculates the Fibonacci ratios. Since we are defining the Fibonacci ratio as F(i+1)/F(i), we need to run the loop from i=1 to i=n-1 because F(n) is the maximum Fibonacci number calculated from the first loop and F(i+1) would be undefined if i=n.
We could also have defined the ratio as F(i)/F(i-1) and set i=2:n.
The script tells the console to display 2 vectors defined as 'sequence' and 'ratio'. These are vectors of the Fibonacci numbers and Fibonacci ratios.
In order to plot the ratios, I define a new vector 'N' that goes from 1 to (n-1). So when N=1, the ratio is F(2)/F(1), and when N=n-1, the ratio is F(n)/F(n-1).
We first started out by plotting the ratios when n=10:
The graph shows that the numbers converge, but we wanted to get a better graph by having more data points.
So we set n=20.
When n=20, we can clearly see that the ratio converges around 1.6.
Looking at the vector for the ratio, we could get a more accurate value at which the ratios converge:
As seen in the above picture, the ratios converge at 1.6180.
Exercise: Baseball Trajectory
(Not drawn to scale) |
We basically need to find the initial batting angle that maximizes the range of the trajectory.
After a lot of thought, we came up with a plan.
1. First, we calculate the trajectory and the range (which is just the final x value in the already given while loop) for angles 0 to 90 in 1 degree intervals.
2. Then, we would make a vector of all the ranges at different angles and use a command to find the maximum range and the angle corresponding to that range
3. Lastly, we plot trajectories at 10 degree intervals because plotting trajectories of all angles from 0 to 90 would just look very very messy
(*I later decided to re-plot the maximum range trajectory with a different color so that it would stand out)
Since the index for the for loop can only start from 1, the i ranges from 1 to 91, where angle(1) is defined as 0 degrees and angle(91) is defined as 90 degrees.
After defining the angle(i) as i-1, the rest of the script (until the end of the while loop) is the same as the script given in the assignment with the exception of the iteration index for the while loop.
Since i is already being used as a iteration index for the for loop, I defined j as an iteration index for the while loop.
The while loop calculates the x and y positions of the ball in intervals of 0.1 seconds. I realized that the definition for the y position was missing a term (1/2)gt^2 and decided to add it just for accurate physics. Even without drag force, there is acceleration due to gravity in the y-direction. (I ran the final script both with and without this term and they give the same angles)
After the while loop, I defined the i-th range (range(i)) as x(j), which is the final x value calculated from the while loop.
Then I ended the for loop and looked up commands in MATLAB that would allow me to print the largest element in an array.
According to the MathWorks website (http://www.mathworks.com/help/matlab/ref/max.html), the command [C,I]=max(A) finds the maximum value of array A and assigns it to the variable C and the index of the maximum value to the variable I.
This was incredibly helpful because it was exactly what I needed to do in order to find the maximum range and the angle corresponding to the maximum range.
Using the command [maxRange index] = max(range), I was able to find the maximum value in the array 'range' and made the console display both the range vector and the maxRange value.
Angle corresponding to the maxRange value can be printed just by typing 'angle(index)' and since angle(i)=i-1, the angle is going to be 1 less than the index value.
Here's a picture of the command window that displays all of the values:
The maximum range is 258.96m and the corresponding angle is 44 degrees.
Now that we've knocked #1 and #2 off the list, it's time to work on #3 and plot the trajectories.
Since 44 degrees is the angle that maximizes the range, I decided to plot the trajectories at angles 4, 14, 24, 34, 44, 54, 64, 74, and 84.
In order to do this, I copied the exact for-loop from the previous part and changed the indices from i=1:91 to i=5:10:85.
This will run the loop at intervals of 10 from i=5 to i=85, which corresponds to angles 4-84 degrees.
After the while loop, I added a plot function and plotted the trajectory (x and y positions) from the initial j value until the final j value. I connected the points with a blue line.
I wanted to do something special for the trajectory at 44 degrees since it gives the maximum range.
So this time I took out the for loop and defined the radians using the angle(index) (which is the angle that gives the maximum range)
This would allow the while loop to run just for this particular angle.
I plotted the trajectory at 44 degrees and connected it with a magenta line.
I also wanted to label the axis of the plot and define the minimum and maximum values, so I used the commands title(), xlabel(), ylabel(), axis([]).
(http://www.mathworks.com/help/matlab/titles-and-labels.html) (http://www.mathworks.com/help/matlab/ref/axis.html)
Here's a picture of our graph:
I tried to label each trajectory with the corresponding angles, but could not figure out how to do that in MATLAB so I thought it would be easier to just label it using Word.
The graph looks a little chaotic but it clearly shows that 44 degrees gives the maximum range.
Since the y-axis is so zoomed out, it actually looks like the starting y position is at zero, but if you zoom into it, it clearly starts out at y=1
We can actually see a very nice trajectory of angle=4 degrees in this picture (Even though it is definitely not to scale) |
For this particular problem (with no air drag), it is easy to just calculate the angle by hand, but I can see how using MATLAB would be useful in cases where we need to consider drag forces, because it would be very painful to try to solve complex differential equations.
No comments:
Post a Comment