May 22, 2011

Matlab Simulations

So, as our course description involves modeling in Matlab, our class spent two days doing various exercises in Matlab.

First task: fibonacci. [Aside: one of my other professors, Randy,  has this joke that goes something like this: if somebody were to guess the major problems in computer science by just observing exercises given, they would that they are fibonacci numbers, factorials, and the Towers of Hanoi numbers.]
Our fib program
This calculates the nth fibonacci number given the mathematical equation derived for the fibonacci numbers. A design decision that we made is we calculated the square root of five once, stored it as a variable, and then used it the three times in the equations that we needed it. We could have calculated the square root of five each of the times that we needed, but that would be less efficient. We also let the variable n be defined on the interaction panel so it was easier to test, since we could pick any arbitrary n without changing our code.

Second task: Car Update.
Our car update program
This program had some question that went something like this: a car rental company has two locations, A and B. Three percent of cars leaving B end up at A and five percent of cars leaving A end up at B after a week. So, this program changes the value of 'a' and 'b' which are the number of cars at each respective location. This program works for arbitrarily large or smFourtall values of 'a' and 'b' despite our professor's anxiety over dropping cars.

Third task: Car Loop
This program uses the car update script and calls it 52 times, simulating a year at the car rental company. This also plots the values on a graph.

Fourth task: Simple DC motor
DC motor code

Plot that this generates
This program simulated a DC motor as described on the page outlining the assignment. Here is a drawing stolen from that website:
Diagram of a Motor
Physics scares me, so it was nice that my partner, Bryn, was extremely comfortable and skilled at physics. She interpreted all of the scary symbols and equations, so the combination of our skills resulted in us being the first ones done with this painful excersize.

Fifth task: Develop a proportionally controlled motor.
Code for our proportionally controlled motor
It was an easy add-in to just add proportional controls to our code from the previous excersize. All we did was changed Vt to be (90 - alpha). [We also made our code after an amount of time, because we wanted to capture the oscillating tail of the motor.]

Sixth task: Develop a derivative contolled motor.
Derivative Motor code
So this code is also not very different, even though it looks much longer. This is because we also added in a variable "last bad" which measures the last time that the change in angle was greater than .1 degrees. The derivative control part is we changed our angle to be some constant times 90 minus alpha plus some other constant times the change in angle. We found that that we minimized last bad to be .0430 when the first constant was 15.2 and the second was 1.3.
Plot of Motor using derivative controls - optimal