Friday, April 1, 2016

MatLab

This week we learned MatLab, which I was very excited about because my graphing calculator just broke.

Exercise 2.1 Our first exercise was to write a program that could compute the nth Fibonacci number, and then find the 10th Fibonacci number. This was as simple as inputting the given equation (being careful with placement of parentheses) and setting n=10 before running the program. Here is the code.


When we set n=10, the answer was 55, just like it should be!

Exercise 2.3 Imagine that you are the owner of a car rental company with two locations, Albany and Boston. Some of your customers do “one-way rentals,” picking up a car in Albany and returning it in Boston, or the other way around. Over time, you have observed that each week 5% of the cars in Albany are dropped off in Boston, and 3% of the cars in Boston get dropped off in Albany. At the beginning of the year, there are 150 cars at each location. Write a script called car update that updates the number of cars in each location from one week to the next. The precondition is that the variables a and b contain the number of cars in each location at the beginning of the week. The postcondition is that a and b have been modified to reflect the number of cars that moved.



Here is my attempt at solving this problem. After one week, A had 147 cars and B had 153 cars. However, I found out that this solution is wrong because it violates the "conservation of cars." A updates in the first line, so then the second line uses the new A to update B instead of the old A. After a while, this throws off the total number of cars so that it's less than 150, which doesn't make sense. This is a good lesson that in MatLab, you need to try several different calculations with your program (in this case, checking the number of cars after several weeks instead of just after the first) to make sure everything makes sense. 


This code works because it uses the old a and b to update the new a and b. Round is used because otherwise you get fractions of cars, which just doesn't make sense. If you check a few weeks, you can see that the number of cars always equals 300.

Exercise 3.1 Create a script named car loop that uses a for loop to run car update 52 times. Remember that before you run car update, you have to assign values to a and b. For this exercise, start with the values a = 150 and b = 150.


In order to see the number of cars in each location at the end of the year, I used a for loop that updates a and b 52 times, since there are 52 weeks in a year. At the end of the year, there were 118 in Boston and 182 in Albany. Since this code ran 52 times, it displayed a lot of numbers on the screen, which was overwhelming. A graph would be useful for interpreting this data.

Exercise 3.2 Modify car loop so that each time through the loop it plots the value of a versus the value of i.



Here is the code that plots a vs. i and its corresponding plot. You can see that at week 21, the number of cars in Albany becomes stable at 118. You can also see that when you don't specify a color for your circles, MatLab automatically alternates colors.

b. Once you get that working, modify it so it plots the values of a with red circles and the values of b with blue diamonds.



Here is the code that plots the values of a and b vs. i and its corresponding plot. You can see that at week 21, equilibrium is reached. There are 118 cars in Albany and 182 in Boston. One thing you have to be careful about in MatLab is clearing your workspace. At one point my a vs. i graph showed equilibrium occurring at week 17, whereas my a and b vs. i graph showed equilibrium at week 21. This is because I hadn't cleared my workspace before running the first program and it was starting with initial a and b values other than 150.


I also tried making a plot with initial a and b values of 10000. It looks parabolic!

Exercise 3.5 We have already seen the Fibonacci sequence, F, which is defined recurrently as Fi = Fi−1 + Fi−2. In order to get started, you have to specify the first two elements, but once you have those, you can compute the rest. The most common Fibonacci sequence starts with F1 = 1 and F2 = 1. Write a script called fibonacci2 that uses a for loop to compute the first 10 elements of this Fibonacci sequence. As a postcondition, your script should assign the 10th element to ans.


It's been a while since I thought about recursive sequences, let alone coding with them, so this one took a few tries. I checked that this code worked by googling the Fibonacci sequence and comparing it against my answers. The 10th Fibonacci element calculated was 55.

b. Now generalize your script so that it computes the nth element for any value of n, with the precondition that you have to set n before you run the script.


Exercise 4.6 The ratio of consecutive Fibonacci numbers, Fn+1/Fn, converges to a constant value as n increases. Write a script that computes a vector with the first n elements of a Fibonacci sequence (assuming that the variable n is defined), and then computes a new vector that contains the ratios of consecutive Fibonacci numbers. Plot this vector to see if it seems to converge. What value does it converge on?





For this plot I used n=100. The plot starts at i=2 (point (2,1) which is the ratio of the second element to the first element) since i=1 would correlate with the ratio of the 1st element to 0th element, and there's no 0th element. The plot definitely converges, and if you check the printout of the ratio vector, you can see that it converges at 1.618. A google search told me that the ratio of Fibonacci numbers indeed converges at 1.618, which is called the golden ratio.  For reference, here is the printout of the vector where the ratios were stored.

R =

  Columns 1 through 12

    1.0000    2.0000    1.5000    1.6667    1.6000    1.6250    1.6154    1.6190    1.6176    1.6182    1.6180    1.6181

  Columns 13 through 24

    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

  Columns 25 through 36

    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

  Columns 37 through 48

    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

  Columns 49 through 60

    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

  Columns 61 through 72

    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

  Columns 73 through 84

    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

  Columns 85 through 96

    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180    1.6180

  Columns 97 through 99

    1.6180    1.6180    1.6180

For our final exercise, we were given a script that calculates and plots the path of a projectile launched at a 30 degree angle with an initial speed of 50 m/s. We were then asked to expand on the code in order to find the angle at which the range was maximized. One option would be to manually run the simulation for all angles from 1 to 90, find the maximum range, and identify the angle that produced it. However, we interpreted the instructions to mean that we should write one script that will return as the answer the angle that produces the maximum range. This turned out to be a time consuming and difficult task, but we got an answer pretty close to 45, which is what we know the answer should be.


This script came up with an answer of 48. Here is a printout of the vector Ranges, which collected the range for each angle.

Ranges =

  Columns 1 through 13

   34.9947   39.9756   44.9383   54.8660   59.7717   69.6165   74.4410   84.1728   93.8304   98.4808  107.9790  117.3777  121.7963

  Columns 14 through 26

  130.9899  140.0592  144.1893  153.0088  161.6796  165.4658  173.8431  182.0482  185.4368  193.3060  196.4123  203.9193  211.2166

  Columns 27 through 39

  213.8416  220.7369  223.0280  229.4967  231.4352  237.4535  239.0211  244.5661  245.7456  250.7953  251.5702  256.1035  256.4582

  Columns 40 through 52

  256.6249  260.3748  260.1007  259.6306  262.5590  261.6295  260.4969  259.1594  260.9609  259.1433  257.1150  254.8748  252.4212

  Columns 53 through 65

  249.7532  246.8698  243.7700  240.4529  236.9180  233.1645  229.1919  225.0000  220.5884  215.9569  211.1056  203.8426  198.6306

  Columns 66 through 78

  193.1999  187.5509  179.8112  173.8085  167.5899  159.5284  152.9634  144.7240  137.8187  129.4095  122.1706  113.6003  106.0350

  Columns 79 through 90

   97.3126   88.5606   79.7816   71.6741   62.7627   53.8322   44.8852   35.9246   27.2147   18.1477    9.0753    0.0000

Based off this data, you can tell that there are several problems. An angle of 45 degrees should produce the maximum range, according to my physics knowledge. This script shows an angle of 45 degrees producing a range of 261.6295. However, 44 degrees produces the largest range, 262.5590. The program picks 48 degrees, with a range of 260.9609, as the range-maximizing angle. I think this may have something to do with the fact that after angle 48, the ranges gradually decrease, but before angle 48, the ranges do not gradually increase. The behavior is like this: ...<R(41)>R(42)>R(43)<R(44)>R(45)>R(46)>R(47)<R(48)>... Obviously, something weird happens with the math in the 40s, and because of the way my program is written, 48 is picked as the range-maximizing angle since the ranges steadily decrease after that.  I'm left with two questions: why is 44 the true range-maximizing angle, and how can I get my script to pick 44 as the range-maximizing angle?

2 comments:

  1. I really like all of the details in your blog, and like that you included other parts of your data in the text of the blog.

    ReplyDelete
  2. Wow, I can really tell you know what is going on with your code and what it computes. I also like your colorful graph.

    ReplyDelete