Wednesday, April 8, 2015

MATLAB Introduction

Today we began our exploration of the MATLAB program. Allen Downey describes MATLAB as a “glorified calculator.” Indeed, as I was experimenting with the extreme basics, I found MATLAB to be similar to how I would use my TI-84 graphing calculator, both in syntax and logical use.

We began reading Downey’s book which explained the basics of MATLAB. We learned how to assign values to variables and how to compute expressions. We learned about the importance of syntax and how parentheses and other punctuation made a big impact. After getting a basic understanding of the Arduino program, I found MATLAB to be challenging in a different way, yet also slightly easier because of my knowledge from the Arduino program.

Our first task was to have MATLAB compute the nth element of the quintessential 1, 1, 2, 3, 5… Fibonacci sequence. We were given the equation and had to use proper syntax to create the equation. 
We had to implement a precondition – that n equaled ten. This told the program to find the 10th element of the sequence. “n” could be designated as any value. 





Next, we were posed with the challenge of updating the inputs used in the written program. 

We had to consider how the number of cars in Albany and Boston changed over a year given fixed rates of travel. We pre-conditioned each variable (A and B) to begin at the value of 150 cars, then wrote a code that would account for one week passing. This code found the total number of cars remaining at one city plus the arrival of cars from the other city, using the A and B variables. We were introduced to the command “round” which rounds the values to the nearest integer. Once the number was rounded, we set the A and B variables equal to the new total of cars in each city. This updated the number of cars in each place and the values were used when the program ran again, generating new values. (SIDE PIC CAR)  
To find the number of cars ending in Albany and the number at Boston at the end of the year would require us to hit the “run [program]” button 52 times.

However, we learned that if we could write a loop, MATLAB would do it for us. 

We kept the same program as above, but to put it into a loop, we used a “for” command. This command created a loop, and as i (representative of iterations) was declared to be 1:52, the loop was told to run again and again from 1 time to the 52nd time. We noted that for the loop to conclude, we had to command “end.”  We found that the number of cars exchanged between Boston and Albany reached equilibrium when there was 118 cars in Albany and 182 cars in Boston. 

We then modified the car loop program to plot the number of cars at each location after each week. 

We kept the previous program and added modifiers that created a plot for each set of data and maintained it for 52 data sets. The “plot” command alerts MATLAB that it must create a plot. In our code, the first plot command tells MATLAB to create a plot where the iteration (week) is on the x axis and the corresponding number of cars in Albany is on the y axis. The point is shown in red, ‘r,’ and as a circle, ‘o.’ The two letters can be combined into ‘ro.’ The second plot shows the cars in Boston and uses a blue diamond ‘bd.’ Now, for each time the loop runs, it plots a value. However, without the command “hold on,” each time the loop begins again, the previous data set is erased. Therefore, we added “hold on” to the beginning of the code. 

Using a 150 car original value, our plot looked like: 

Using a 10000 car original value: 






The plot is more smoothly curved with greater original values.










Next, we began to look at sequences. The Fibonacci numbers used above are an example of a sequence. We were prompted to recursively define the nth term in the Fibonacci sequence. 

Screen Shot 2015-04-08 at 11.23.50 AM.png












I began by introducing the first four numbers (1, 1, 2, &3) to use in my program. I then told MATLAB to run the program to find the 5th through the nth elements (n is preconditioned). I created functions prev(n), prev(n-1), and prev(n-2) to keep track of elements’ position relative to the most recently calculated element (prev(n) value). For each, I assigned variables A1- A4. These began as defined values before being updated in the next steps. As the sequence produces a number, the next step is to make that number a contributor to the next number produced. This shifts that most recently found value to the left and now it is the number preceding the term being produced. As the term being produced is now prev(n), the one that moved to the left becomes prev(n-1). I then updated the variables A2-A4 accordingly. A1 was used only in the first iteration. 


The last task was to plot the ratios of Fibonacci elements to their preceding element. We were asked to determine whether these ratios converged.

The inherent purpose of this code was to calculate a vector given specific inputs (the first values of the Fibonacci sequence). The vector updates and then compares the ratios of successive elements.

I defined the first three elements (temporarily) and introduced “hold on” to make sure the plot continuously graphed and did not erase data points every loop. I then told MATLAB to run the vector computing loop for the 4th through the nth element of the sequence, and defined my vector A(i) as equivalent to the vectors A(i-1) + A(i-2). Then I created another loop that told MATLAB to run from the first element to the second to last element (not the last element because it is the numerator of the final fraction).  In this loop, I created another vector B(i) that took the A(i+1) and divided it by A(i). This was dividing one element by its predecessor. I then assigned X as the element number, going from element 1 to the nth element in the sequence. Finally, I told MATLAB to plot B(i) as a function of X with red circles. I predefined n to be 50.







The ratio begins at two (its maximum) and then jumps to 1.5 (its minimum). The successive jumps decrease in magnitude until the ratio converges to 1.6180.




The Golden Ratio! Coincidence?

No comments:

Post a Comment