Monday, March 31, 2014

MATLAB Introduction: The Puzzle of Physical Modeling

Today in class, we were introduced to the wonderful world of MATLAB. The program is a way of modeling events mathematically. Although we've only just barely begun to scratch the surface of the program's potential, we did manage to complete some sample exercises.

We started by simply making a Fibonacci Script. We had to translate the Fibonacci expression into MATLAB, set n=10 and see if the answer (Fn=ans) was 55 to check our work.


Our answer was, indeed, 55. 




Our next challenge was to calculate the number of cars a dealership had in each of its two location each week. Each week, 3% of the cars in B move to Lot A, and 5% of the cars in lot A move to Lot B. Each Lot starts the year with 150 cars 




We achieved this by adding and subtracting the desired number of cars each week. In addition, we also had to remember that the amount of cars in each lot changed per week, so 5% of the cars in Lot A one week would be a different from 5% in any other week.

Once we had completed this, we moved on to leaning about loops in MATLAB. Our challenge was to create a loop for out car calculating program and make it run 52 times, so that we could see what each car lot would look like at the end of one year. We set our initial A and B values to be 150.




We achieved a long stream of random numbers on our screen, but it was clear that the program was working because each week the number of cars in each lot changed. Some of our numbers were decimals, however, and because we knew a lot could not contain 'half a car', we used the round command to make our cars whole again. 

To make our data easier to interpret, we learned how to plot a graph. 




The graph allowed us to visually understand exactly how our numerous results were trending over the 52 week period. We then compared Lot A to Lot B to see how the entire stock of 300 cars was trending over the year.




In general, the number of cars in Lot A decreased at a decreasing rate and the number of cars in Lot B increased at a decreasing rate. In other words, Lot A always lost cars, but how many cars it lost each week became less over time. Conversely, Lot B always gained cars, but the number of cars it gained dropped lower and lower each week. The result was the same, even when Lot A and Lot B initially began with 10000 cars each.


For our next challenge, we left the car rentals behind and returned to our Fibonacci sequences. Our challenge was to compute the first 10 numbers in the sequence (we would have to specify the first two, but after that everything else was computable). 



Using our previous code and our knew knowledge of forever loops, we managed to complete the task. By checking online to see what the actual values of the first ten numbers in the sequence were, we were able to confirm our results. Next, we had to adjust the code such that it could calculate the sequence to any number (n) beyond (or even below) 10, but not less than 2. 


We achieved this by defining i as a variable able to be changed by the user. The starting point, 3, would remain the same, but the user could change the end point to anything.

From here, we went one step farther in our Fibonacci fun. Using vectors, we determined the ratio to which that F(n+1)/Fn converges, as well as graphed it. 


 


By using the vector to store of Fibonacci numbers, we were able to graph the ratio and see that the sequence converges to approximately 1.62 as the value of 'n' increases.

Finally, we were charged with maximizing the range of a baseball. Given a script, we had to alter it to determine the launch angle needed to produce the maximum range. Using the equation,

d = ((v^2)*(sin2x))/g,

we graphed distance vs angle on a separate script to see what angle would produce the greatest range. 




 
The optimal angle ended up being 45 degrees. Our code has the pre-condition that the user run our fabricated script first to acquire the needed angle before running the code. Conversely, our script can also be used to find an angle for a different distance, or to determine the angle based off a known distance.




No comments:

Post a Comment