Friday, April 1, 2016

MATLAB Introduction

In order to learn a few basic things about MATLAB, we (Thessaly and I) read the first four chapter of Allen B. Downey's book Physical Modeling in MATLAB. MATLAB is essentially a "glorified calculator" (as Downey calls it) that can perform numerous calculations repeatedly and then graph the results.

Exercise #1: Fibonacci
For our first exercise, we were tasked with creating a script that would use the Fibonacci equation to show what the 10th term of the Fibonacci sequence was. We had to be really careful with the order of operations as well as the number of parenthesis we used. We could've simplified the code by splitting it up into much smaller sections but this works too. To use this script, the user is first prompted to give n a value and then type in the name of the scrip fibonacci1 and then the nth term is given as the answer.



Exercise# 2: Update amount of cars
We were then tasked to create a script that updates the number of cars in Albany and Boston. Per week, 5% of Albany's cars would leave and stay in Boston while 3% of Boston's cars would leave and stay in Albany.  Our car_update script gives the number of cars after 1 week where Albany ends with 147 cars and Boston has 153 cars. In order to get say the 10th week, we would have to repeat the script 10 times in the command window.


Exercise #3: Show amount of cars after 1 year in a graph
This script shows the for loop and plotting of car_update. In order to do this, we first wrote a for loop that starts at the 1st term and repeats the car_update script 52 times but for each time in ran through the loop, a red dot for Albany's cars and a blue diamond for Boston's cars would be placed on the graph. And after a year, Albany would have 116 cars while Boston would have 184 cars and the number of cars in each city reached an equilibrium after about 23 weeks.


Exercise #4: Re-write the Fibonacci Sequence script
In the next exercise we were tasked with creating a script that returns the nth term of the Fibonacci sequence using a for loop and the equation that most of use to calculate the Fibonacci sequence F(n) = F(n-1) + F(n-2). The user is first prompted to give n (the number of times the loop repeats/ also the term in the sequence) a value and the script returns the nth value of the Fibonacci sequence. The loop begins at 3 instead of 1 since the first two terms have already been given.


Exercise #5: Plot Fibonacci ratio
To show the ratio of F(n) to F(n-1), we made a new variable called ratio that would be F(n)/F(n-1) and then these ratios would be graphed. The ratios converged to 1.6180.


Exercise #6: Calculate angle with the maximum horizontal displacement
In order to find the angle that would cause the max horizontal displacement of the baseball, we first change angle to be equal to n and placed most of the script in a for loop that would repeat the lines of code provided to us 90 times from an angle of 1 degrees to 90 degrees. We then changed the plot to plot the points of angle vs x displacement instead of x vs y displacement. Then in order to find the maximum x displacement, the code compared the current value of x to the value before it and if it was the biggest it would call it realmax. When realmax was found, the code would then return the index of the realmax which will return the angle where it was found. The code is supposed to show 44 since in the command window we found that the largest x was about 262 which was at an angle of 44 degrees. But the ans is returned as 48 and we don't really know why.

1 comment:

  1. Good job completing the baseball task! I really understand it now.

    ReplyDelete