Pages

Friday, April 10, 2015

MATLAB

This past week we learned how to use MATLAB by reading the first few chapters of Physical Modeling in MATLAB by Allen Downey and doing some exercises from the book. A little background, MATLAB functions similarly to a calculator, but allows for much higher processing power. The exercises describe below allowed us to learn how to assign variables, run functions, and obtain graphs from MATLAB. 


Exercise 2.1


The first exercise was to use MATLAB to compute the nth term of the fibonacci sequence. This involved coding a function for the fibonacci sequence which defines the first two terms as 1, and then each following term is the sum of it's two preceding terms. Using the code we came up with (shown below) we set n to equal 10, and were able to get the 10th fibonacci number (F=10)




Exercise 2.3


The next exercise involved working with multiple variables that were dependent on each other. According to the exercise, there are two locations that each start out with 150 cars. Each week, five percent of the cars at location (a) go to location (b), while 3 percent of the cars at location (b) move to location (a). Our job was to use MATLAB to compute how many cars were at each location after one week. For the code below, we preconditioned  (a) and (b) to be 150



Exercise 3.1

 Once we finished the car function, the next step was to insert a loop into it that would allow us to calculate the amount of cars at each location after a certain number of weeks (in this case it was 52). In order to create a loop, we enclosed our original function in a "for" command, setting the number of times the loop should run (i) to 52. While not shown in the image below, we found that the number of cars exchanged between Boston and Albany reached equilibrium when there was 118 cars in Albany and 182 cars in Boston. 





Exercise 3.2


We then added a plot command to our script to get a visual representation of the number of cars at each location week to week. In MATLAB, our plot command creates a graph where the week number is on the y-axis, and the number of cars at that location is on the X axis. In the our graph, location (a) is represented by the red circles, and location (b) is represented by the blue diamonds. 



When we got the graph above, we realized that we had switched our x and y axis. The corrected graph is shown below. One thing to note is that as the initial number of cans increases, the graph lines get smoother.





Exercise 3.5


In this exercise we returned to the fibonacci sequence, this time aiming to use a recurrent equation to compute the first ten numbers. To do this, we used a loop function that would allow us to add the previous sequence value computed to help produce the next one. For the below code, we ended up printing 12  sequence numbers rather than 10 because we didn't account of our code which dictated that we go to the   "i + 2th" term. We should have either said "i= 1:8" so that it would print the first ten numbers or we could have written "Print F(i-2)" to solve the problem. Either way, we achieved the goal of the exercise. 



Exercise 4.6


The last exercise was to write a script that computes a vector for the "n+1th" term of the fibonacci sequence divided by the nth term, and then plot this vector to determine where it converges. From the plot, we determines that the values converged around 1.6.





No comments:

Post a Comment