Friday, April 1, 2016

MATLAB Introduction

 Matlab is a computing program that is widely used by engineers for modeling. Or as Allen Downey likes to call it: "a glorified calculator.
First exercise:
Exercise 1   The Fibonacci sequence, denoted F, is described by the equations F1 = 1, F2 = 1, and for i ≥ 3, Fi = Fi−1 + Fi−2. The elements of this sequence occur naturally in many plants, particularly those with petals or scales arranged in the form of a logarithmic spiral.
The following expression computes the nth Fibonacci number:
F_n=(phi^n-(-phi)^(-n))/(sqrt(5))
(1)
=((1+sqrt(5))^n-(1-sqrt(5))^n)/(2^nsqrt(5)),
We accomplished this task by putting the equation into matlab and checking it by plugging numbers into the program.




Exercise 2
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.

Our first inclination was to use an exponential function, which in theory works but in practice we realized that it did not update, so we made it instead to have:
anew = a - 0.05*a + 0.03*b; % albany cars after certain number of weeks
bnew = b + 0.05*a - 0.03*b; % boston cars after certain number of weeks



Exercise 3
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.
If everything goes smoothly, your script will display a long stream of numbers on the screen. But it is probably too long to fit, and even if it fit, it would be hard to interpret. A graph would be much better!

Exercise 4
Modify car_loop so that each time through the loop it plots the value of aversus the value of i.
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.

Exercise 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.
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. To keep things simple for now, you can assume that n is greater than 0.

Exercise 6
The ratio of consecutive Fibonacci numbers, Fn+1/Fn, converges to a constant value as nincreases. 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?



3 comments:

  1. I really like all of the detail you used in your blog. It would help if you just include screen shots of the code itself, because I couldn't read any of the scripts.

    ReplyDelete
  2. I really like how you included the description of each task and gave a brief summary of what you did.

    ReplyDelete
  3. nice graphs! Graphing in different colors really does help visualize the differences!

    ReplyDelete