We started learning MATLAB by reading the book found at this address:
greenteapress.com/matlab/PhysModMatlab.pdf
Here a few of the exercises we have done in class.
Ex. 2.1
The first is to write a script that would generate the nth Fibonacci number. This program is fairly straight-forward. I actually changed a code a little after I took the screenshot:
first = (1+sqrt(5))/2;
second = (1+sqrt(5))/2;
Fn = 1/sqrt(5)*(first^n - second^n)
This way it is easier to see how Fn is affected by n.
Ex. 2.3
Again, very straight forward, this script calculates the number of cars at each location after each week. Note that there is no n involved in the script, so the calculation is recursive. To reach the nth week (assuming starting at the 0th week), we must run the program for n times. While I was writing the code, I initially put a=150; and b=150; in the script. This became problematic because a and b would reset every time we run the script. As a result, we must initialize a and b in the command window and then run the script.
Ex. 3.1
In the previous exercise, we had to manually click "run" n times to get the result for the nth week. Here, using loop, MATLAB will automatically calculate the result after the nth week. Note that I used 52 here because there are about 52 weeks in a year. The number seems to stabilize at 118 and 182. If we wanted to calculate for any n, simply change the loop to: for i=1:n and then initialize n in the command window before running the program.
Ex. 3.2
This is a follow-up of the previous exercise. Here I used the semi-column to hide numerical results, since we are interested in the plot. Clearly, the graph seems to converge to the numerical value calculated in the previous exercise.
Ex. 3.5
In this exercise, I certainly made the script more complicated than it had to be. I was in the mindset of "car_loop" where two variables are updated after each time the script is run. I would rewrite the code as follow:
F(1) = 1;
F(2) = 1;
for i=3:10
F(i) = F(i-1) + F(i-2)
end
My only concern is if MATLAB would misinterpret F(1) with the parentheses. I would have to try this code again.
Ex. 4.6
The first image shows how to put the Fibonacci numbers into a vector. The second image shows the calculation of the ratio between Fibonacci numbers. Clearly, they converge to 1.6180, which is in fact an estimate of the golden ratio. If I were to use the long format, we could probably see more digits that converge to 1.61803... which is also the value we used to calculate each number in the first exercise.
The first is to write a script that would generate the nth Fibonacci number. This program is fairly straight-forward. I actually changed a code a little after I took the screenshot:
first = (1+sqrt(5))/2;
second = (1+sqrt(5))/2;
Fn = 1/sqrt(5)*(first^n - second^n)
This way it is easier to see how Fn is affected by n.
Ex. 2.3
Again, very straight forward, this script calculates the number of cars at each location after each week. Note that there is no n involved in the script, so the calculation is recursive. To reach the nth week (assuming starting at the 0th week), we must run the program for n times. While I was writing the code, I initially put a=150; and b=150; in the script. This became problematic because a and b would reset every time we run the script. As a result, we must initialize a and b in the command window and then run the script.
Ex. 3.1
In the previous exercise, we had to manually click "run" n times to get the result for the nth week. Here, using loop, MATLAB will automatically calculate the result after the nth week. Note that I used 52 here because there are about 52 weeks in a year. The number seems to stabilize at 118 and 182. If we wanted to calculate for any n, simply change the loop to: for i=1:n and then initialize n in the command window before running the program.
Ex. 3.2
This is a follow-up of the previous exercise. Here I used the semi-column to hide numerical results, since we are interested in the plot. Clearly, the graph seems to converge to the numerical value calculated in the previous exercise.
| When a and b are initialized at 10000 |
In this exercise, I certainly made the script more complicated than it had to be. I was in the mindset of "car_loop" where two variables are updated after each time the script is run. I would rewrite the code as follow:
F(1) = 1;
F(2) = 1;
for i=3:10
F(i) = F(i-1) + F(i-2)
end
My only concern is if MATLAB would misinterpret F(1) with the parentheses. I would have to try this code again.
Ex. 4.6
The first image shows how to put the Fibonacci numbers into a vector. The second image shows the calculation of the ratio between Fibonacci numbers. Clearly, they converge to 1.6180, which is in fact an estimate of the golden ratio. If I were to use the long format, we could probably see more digits that converge to 1.61803... which is also the value we used to calculate each number in the first exercise.
This comment has been removed by the author.
ReplyDeleteWhat did you think was the most challenging part about working with MATLAB?
ReplyDelete