MATLAB
We started our new unit today on MATLAB. I am working with my new partner Sabrina on this unit (Here's her blog:
sabrinaattemptsengineering.blogspot.com) We will also be working on the final project together.
We started by reading the first four chapters of Allen Downey's
Physical Modeling in MATLAB and doing some of the exercises to familiarize ourselves with programming in MATLAB.
Exercise 2.1 - Fibonacci Sequence
The fibonacci numbers are a sequence of numbers where the next number is the sum of the previous two: 1, 1, 2, 3, 5, 8, 13, 21, . . . The following is our first exercise to learn simple coding in MATLAB, and to make sure we got the order of operations correct.
In our code, we decided to split the expression inside the brackets into two elements. By doing it this way, we reduce the number of parentheses we must use and we don't get so confused. We name these elements by creating variables called first_element and second_element, and then use those variables in the next expression. In the command window below, you can see that when we set n = 10, the answer is 55, which is the 10th term in the Fibonacci sequence.
Exercise 2.3 - Car Rental Update
Now things got a little more complicated. We had the following problem to solve and code. Our first step was actually understanding the problem and the math behind it. We did some diagrams and created equations to work it out.
At first, we had our equations as you see in the following code. To update the number of cars for a, we take away 5% of a and add 3% of b. The equations are correct, but we realized we were missing a step when the numbers did not come out correctly. The problem is the following: a and b both start at 150. Then a updates. Then b updates, but when b is updating, it is using the value for a that has already been updated, not the original 150. We fixed this problem in the second code below. We update the values using anew and bnew and then set a and b to the new values. After several weeks we see that the number of cars reaches an equilibrium.
Car Update (Almost Correct!)
Car Update (Correct!)
Exercise 3.1 - Car Update with Loop
For the next exercise, we kept the same problem and the same code, but just added a for loop to the code.
Here, we take our code from the last example and add a
for loop. Inside the loop, we have our previous function that updates the number of cars. The loop runs 52 times, so that the last values will show the number of cars in each location at the end of 52 weeks (1 year).
So at the end of a year, we see that the number of cars in each location reach an equilibrium. To visualize this, we will learn in the next exercise that we can plot these points over time.
Exercise 3.2 - Car Update Loop with Plot
Next in our reading we learned how to make plots from our code. We took our previous code for the Car Update Loop and added code to plot the points. This way we could see the equilibrium that each city reached.
We added three lines to our code. "Hold on" tells the plot to keep the previous points during the loop. Then we plot the values of a versus i with 'ro' red circles. Then we plot the values of b versus i with 'bd' blue diamonds.
|
Y-axis: Number of cars. X-axis: Time in number of weeks.
Initial number of cars in Albany and Boston is 150.
Albany (a) is represented by red circles and Boston (b) is represented by blue diamonds. |
Exercise 3.5 - Fibonacci Sequence with Loop
Now, back to the Fibonacci sequence. This time, we will add a loop that can compute any number in the sequence.
First, we have to tell it the first two terms: 1 and 1. Here we are using a function notation, where F is the function and the number in the parentheses is the term number. Hence F(1) means the first term and F(2) is the second term. So F(n) means that we can set n to be something and compute the nth term. The for loop will run from 3 to n, and inside the loop all we have is the function specifying the Fibonacci sequence. The next term is the sum of the previous two terms.
To test our code, we set n to be 10, and the result was 55, which is correct. We tested out 15 and the 15th term of the Fibonacci is 610!
Exercise 4.6 - Fibonacci Ratios with Plot
This last exercise was the most confusing of the afternoon. It took us a while to actually understand the question. Basically, take the Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, . . . and divide the next term by the previous term, so 1/1 = 1; 2/1 = 2; 3/2 = 1.5; 5/3 = 1.667. These are the ratios we want to compare. Over time these ratios converge, or get close to one number. If I list out the ratios, you can see that they start to converge somewhere around 1.618: 1, 2, 1.5, 1.667, 1.600, 1.625, 1.615, 1.619, 1.618, 1.618. You can see these ratios in the command window below.
In our code, we have the loop from last time and the only thing we added was the second for loop/ For this loop we have it going from 2 to n, and the function we have is R(n) for ratios. It take the nth term and divides is by the n-1 term. For example if I were computing R(2), it would be R(2) = F(2)/F(1), which is 1/1 and so R(2) = 1. Then, we plot ratio versus n with blue circles.
In the following plot of the Fibonacci ratios, we can see the convergence behavior. For the first 10 or so terms, the ratios are drastically different and after that, the ratios only differ by small decimals so they seem to converge on one value.
Final Thoughts
That is it for our first day of MATLAB! I found coding in MATLAB to be pretty similar to the other coding languages I already know, just a few minor syntax differences. It was great to see that we can apply this to useful situations and I can see why MATLAB is used to model so many things in the sciences. Next class, we will using MATLAB to model thermal systems, heating and cooling things down. Also, since we were given our partner assignments today, we will be working on our project during the next few classes in addition to finishing up MATLAB, so look for those updates as well!