Thursday, April 9, 2015

MATLAB Introduction

Today, we started our section on MATLAB. This program can be used for a variety of calculations and functions. Using Arduino and having a bit of programming experience made starting the MATLAB programming an easier transition. 

We started by introducing ourselves to MATLAB by reading Physical Modeling in MATLAB by Allen B. Downey. In chapters one through four, he writes about basic functions of the program, such as writing loops, and functions, and vectors.

As we read the chapters, we also did some of the exercises.

Fibonaci Sequence

The first exercise was to write a script to compute the nth term of the Fibonaci sequence, which can be calculated using the following equation. 

We translated the equation into something that MATLAB could read. Each portion of the equation was set as a separate variable, and then put together in one line of code at the end. We also set n as 10, so that the program computed the 10th term of the sequence. 

We ran the program and got the 10th term as 55, which was what we expected.

Car Update

The next exercise was to create a script that updates the number of cars at two rental car companies, one in Albany and one in Boston. 5% of the Albany cars get dropped off in Boston  and 3% from Boston to Albany each week. Each location started off with 150 cars, so we set the initial a and b as 150 in the command window. Then we set A and B as the number of cars in each location after one week. Then, we set a as A and b as B so that the new initial number of cars would be changed. That way, if we run the function once, it displays the number of cars in each location after one week; if we run it 10 times, it shows the cars for after 10 week, and so on.
The round function is to round the number displayed to a whole number, as one cannot have half a car.



Car Loop

Next, we were to create a loop for the previous script so that the function automatically runs a specified number of times. In this case, we set the loop to run 52 times to simulate the number of cars in each location in one year. The initial conditions for a and b were set as 150.


When we ran the loop, we found that after a while, the values of A and B stopped changing. A stopped at 118 cars while B stopped at 182 cars.

Car Loop Plot

Next, we created a script that would also plot the values of the car loop script from above. Again, the initial values were 150. The hold on ensure that the graph doesn't automatically clear the points each time. The cars in Albany were plotted by red circles, while the Boston cars were plotted in blue diamonds.


As you can see from the above graph, the values eventually stop changing. 

We also tried using initial values of 10,000 cars. The result was that the graph resembled a better parabola shape. 



Fibonaci 2

The next exercise was to write a script to compute the elements of the Fibonaci sequence using a loop. The Fibonaci sequence is calculated using the equation 

Fi = F(i-1) + F(i-2)

In order to display the first 10 terms of the sequence, F1 and F2 are set as 1. Then, inside the loop, the nth term of the sequence is the sum of the previous two terms. Then, the previous two terms are updated to the newer two terms.

Then, we generalized the loop so that any number of terms could be calculated. The value of n just needs to be specified before the script is run.

Fibonaci Ratios

The last exercise was to write a script to first calculate the vector of the nth number of terms in the Fibonaci sequence, and then calculate the vector of the ratios of F(i+1)/F(i) -- that is the ratio of two consecutive terms. Then the ratios were plotted on a graph to see if the values do indeed converge. 

The ratios were plotted versus X, which was the value of n of that specific ratio.






2 comments:

  1. I like how you cut out the code part only so we can focus in on the code. Everything was very easy to read and your short descriptions helped!

    ReplyDelete
  2. Great organization of the blog post, very easy to follow and nicely explained. The Car Update section was a bit confusing though, maybe a possible rewording would be better.

    ReplyDelete