Friday, April 1, 2016

MATLAB Introduction

Day 17 (3/29/16)

Today we started learning how to program in Matlab. By reading through chapters 1-4 of "Physical Modeling in MATLAB," by Allen B. Downey and going through some of the exercises, we were able to get a good understanding of MATLAB and some or its major functions.

The exercises are outlined below with screenshots of the code and the answers/graphs they produced if applicable.


Exercise 2.1: fibonacci1 script

The first exercise we did was come up with a script to determine the nth number in the fibonacci sequence.

Here is what the code looked like:


If you wanted to solve for the 10th value, you would input 10 when the command window prompts you for the value of n.



Exercise 2.3: car_update script

The second exercise we did was to create a script to update the number of cars in both Albany and Boston week to week. The conditions were that they both started with 150 cars and 5% of the cars from Albany go to Boston each week and 3% of the cars from Boston go to Albany each week.

Here is what the script looked like:


After running the script repeatedly, the number of cars finally reached an equilibrium after 24 weeks with 116 cars in Albany and 184 cars in Boston.

Exercise 3.1: car_loop script

The next task we did was update the car script to run it in a loop, rather than having to run the code for each week, we can specify the number of weeks we want to run the code for.

Here is what the new script looked like:


And here is what the loop returned after 52 weeks:




Exercise 3.2: car_loop script with plotting. Also try initial values of a & b = 10000

The next exercise we did was build on top of the car loop so that our data could be plotted to see the pattern. We did this by just adding a plot function in to the loop and specifying the shape and color of all of the points.

Here is what the script looked like:



Here is what the plot looked like for number of cars in Albany with an initial values of 150 at each location and a time span of 52 weeks:


And here is what the plot looked like for number of cars in Albany with an initial values of 1000 at each location and a time span of 52 weeks:


Exercise 3.5: fibonacci2 sequence script

The next exercise we did was to create a script using a loop to determine the nth element of the fibonacci sequence.

Here is what the code looked like:


For the 10th number in the sequence, the code returned a value of 144:



Exercise 4.6: plotting fibonacci ratios

The last exercise we had to do out of the book was to create a vector for both the fibonacci sequence and the fibonacci ratio (Fn+1/Fn). And then we had to plot the ratio.

Here is what my code looked like:


And here is what the plot of the fibonacci ratio vector looked like:


Unfortunately, I did not have time to get to the baseball problem, but I will take a look at it and try to work it out when I get the chance.

No comments:

Post a Comment