Exercise 2.1: Fibonacci1 Script
The goal of this exercise was to calculate the nth term in the Fibonacci sequence that has the form, F(n) = F(n-1) + F(n-2) where F(1) = 1, F(2) = 1. In other words, every term (starting from the third) in this sequence is the sum of the two terms before it. We used the following expression to find the nth term. We wrote the program such that when we run the code, it asks us to enter a value for n and then prints the value of Fn.
Code and Results:
This exercise helped train us write mathematical expressions the way MATLAB understands them. As you can see, n = 10, for example gives us the correct Fn value of 55.
Exercise 2.3: car_update script
In this exercise, we pretended we had a car rental company with a branch in Boston and another in Albany. Customers can pick a car from either location and drop it off at either location. We are told that initially there are 150 cars in each location and 5% of the cars from Albany end up Boston while 3% of the cars from Boston end up in Albany every week. We wrote a script that updates the number of cars in each location every week.
We represented total number of cars left in Albany with the letter a and those in Boston with the letter b.
Script and results:
Exercise 3.1 and 3.2: car_loop script with plotting
(also trying values of a and b = 10000)
The previous code required us to manually enter each value of a and b to get the next pair. This is a bit tedious especially when dealing with a large amount of data. So this time, we created a loop that runs the above functions of a and b 52 times and helps us see if the values increase, decrease, oscillate or approach a constant.
Here are the first few values the loop produced:
We also plotted it to see this pattern.
For initial values a = 150 and b = 150For initial values of a = 10000 and b = 10000
It is interesting that the 10000-cars curve is smoother at the bend towards its constant value.
For starting values of a = 150 and b = 150, we get constant values a = 116 and b = 184 after the 24th week. The number of cars left in Albany decreases and that in Boston increases until they reach these constant values.
Exercise 3.5: fibonacci2 sequence script
Here we used a for loop to make MATLAB give us the 10th and later the more general nth number.
Scrips and results:
• Exercise 4.6: plotting fibonacci ratios
After learning about vectors and plotting techniques in Chapter 4, this exercise challenged us to write the Fibonacci sequence as vector, another vector with the ration of the consecutive numbers in the Fibonacci sequence and plot the latter to see if the ratio converges.
Scripts and results:
Firstly, I really like the pattern in the background of your blog. I also really like that you included so many pictures and screen shots of your code and the plots.
ReplyDeleteNice supporting graphs and screenshots of the calculated ratios.
ReplyDelete