Tuesday, April 14, 2015

MATLAB exercises

This past week we have been learning the basics or MATLAB, and were asked to complete a series of exercises in order to get to know the program better, which I will detail below.

Exercise 2.1

To begin, we were asked to write a code that would compute the nth term of the fibonacci sequence. The fibonacci sequence defines the first two terms as 1, and then each following term is the sum of it's two preceding terms. This is the code we wrote:


Note that we defined n=10 as a precondition. This successfully computed the nth term.

Exercise 2.3

Next, we were asked to write a script that had variables that were dependent on each other. In this case, there are two locations that each start out with 150 cars. Each week, five percent of the cars in location "a" leave to go to location "b", and 3 percent of the cars in location "b" move to location "a". our first task was to write a script that shows the final amount of cars after one week.

Note that our preconditions were to define "a" and "b" to be 150, to set a start value. You can see that after running the program (and rounding the numbers to the nearest whole integer) the adjusted values were produced.

Exercise 3.1

Now, we had to implement a loop into the function from 2.3 to account for 52 weeks. For this we used the integer "i" to count up to the number of weeks we wanted it to run for.


The final values look the same as the ones in 2.3 because I rounded the numbers to integers to begin with and after a certain point (number of weeks) the answers begin to stabilize until the number of cars in each location are the same from week to week.

Exercise 3.2

Now we added a plot to our script to see what the car transfers look like over time. Here is a photo of our sketch implementing the plot command and of our plot.


"ro" means to plot the "a vs. i" values in red circles. "bd" means to plot "b vs. i" as blue diamonds. 



You can see the values are more erratic at the bottom and then quickly level out. With initial values of "a" and "b" at 10000, it was easier to see the values change at the beginning. However since the function involves percentages it took the same amount of time fr the values to reach a stable output.

Exercise 3.5

For the next exercise, we wanted to compute the first ten numbers of the fibonacci sequence. We ended up printing the first 12 because we were printing "F", which is going up to te "i + 2th" term. We should have either said "i= 1:8" so that it would print the first ten numbers or we could have written "Print F(i-2)" to solve the problem. Either way we still found the 10th term of the sequence.


Exercise 4.6

For the last exercise we had to write a script that computes a vector for the "n+1th" term of the fibonacci sequence divided by the nth term. We would like to see if and where this converges. We had a lot of difficulties defining the "n+1th" term at first, but eventually we found a simple way to implement what we wanted. Then we plotted a graph.



You can see that it converges right above 1.6. 

This completes all of the MATLAB exercises, and soon I will post about thermal systems and more feedback & control.



1 comment: