Friday, April 10, 2015

MATLAB Introduction

The Assignment: 
For this assignment we read through the 1st 4 chapters of the Allen Downey's book Physical Modeling in MATLAB and completed selected exercise contained in those chapters.

Exercise 2.1: fibonacci1 script
For this first exercise we had to transform a Fibonacci sequence equation into a language that could be understood by the program. We also had to make sure that the program updated itself. We separated the equation into a part 1 (P1) and part 2 (P2) to make it easier to read.


Exercise 2.3: car_update script
For this excercise we were given a word problem which was about a car rental company with two locations, one in Boston and the other in Albany. Customers could rent cars one way, which meant that the number of cars at each location would fluctuate over time until it reached an equilibrium. Initially we had trouble making sure that the a (Albany) and b (Boston) values wouldn't update before the program had run through. This made the values only slightly off, and would have been unnoticeable if the round function had been there originally.  We fixed this by creating anew and bnew within the program then updating them at the end.

Exercise 3.1: car_loop script
With the previous script if we wanted it to run 52 times then we would have had to tell it to run over and over again 52 time. By creating a loop the program automatically runs the script 52 times then gives us the answer.

Exercise 3.2: car_loop script with plotting (also try values of a&b = 10000)
Next we plotted the car_update function we created earlier. The first time we wrote the program we wrote car_loop rather than car_update inside the for statement and the function wouldn't work. This was because we created a double loop that didn't reference our original program. Changing it to car_update gave us the correct graph.

Below is what the plot looks like if a&b = 10000 initially. Notice how smooth the curve is compared to the one above.

Exercise 3.5: fibonacci2 sequence script
WE rewrote the fibonacci sequence without all the cumbersome language we used in exercise 2.1. The range i=3:10 starts at 3 becasue if it started at 2 then the program would get stuck since f(i-2) = 0.


Exercise 4.6: plotting fibonacci ratios
We changed he f to an F to conform to the conventions of vector notation which the book talked about. we also moved the ans line out of the loop. We also created a range that has a upper limit equal to n.

Plotting the ratio proved to be very difficult. We got the golden ratio that the ration is supposed to approach, but it wouldn't plot it consecutively instead there would only be a dot in the center of the plot for the answer. Also, if we tried to plot F then it gave us the fibonacci sequence exponential curve. We addressed this problem by creating another function Y to stand for the ration. We then told it to plot Y(n) rather than Y(i) which finally gave us a fully plotted graph.

Reflection:
I found that while reading the book the operations seemed to be logical and easily applicable, however once we started trying to write code for ourselves it became difficult. AS he mentions many times throughout the first 4 chapters, often it is difficult to tell whether or not the code is executing properly or not. With the Fibonacci sequence we found ourselves having to do the math ourselves to check if it was running correctly. Also, the programming language is not terribly intuitive so we need to be especially careful about which signs and symbols we use.

No comments:

Post a Comment