Friday, April 1, 2016

Matlab

We're learning Matlab!  Matlab is a popular programming lanaguage in academia.  It is a fancy calculator, but it can also program models and parse data other such things.  I might need to use this programming later.

Here are the things that I programmed with it.

Fibonacci1 Script

% this computes the nth fibonacci number through straightforward 
% mathmatical formula
% precondtion: must assign a value to n before running 
% post condition: the result is stored in ans 

(1/sqrt(5))*( ( (1+sqrt(5))/2 )^n - ((1-sqrt(5))/2)^n )
%this is the translation of the expression that computes the nth fibonacci
%number. its pretty much exactly the same as what you would type in a
%graphing calculator.  The only difference is that we included spaces for
%better readability and debugging.  To run, we set n = 10 and then enter 
%fibonnaci1 into the prompt and it runs the script with n = 10 
%
oh look, Matlab looks like a calculator!


car_update Script

% this is to calculate the number of cars in Albany and Boston if 
% 5% of Albany Cars end up in Boston and 3% of Boston Cars are driven 
% to Boston within a week
% precondition: positive integers set for a and for b 
% postcondition: counts of cars in Albany (a) and in Boston (b)

olda = a % this is to save the old values for albany 
oldb = b % this is to save the old values for Boston 
a = olda + round(0.03*oldb) - round(0.05*olda) % recalclating the new Albany value
b = oldb + round(0.05*olda) - round(0.03*oldb) % recalculating the new Boston value %

car_loop Script

%
%car_loop runs car_update 52 times to simulate the car moving activities
% each week for a year 
%preconditions: positive integers for a and for bb
%postcondition: the number of cars left in Albany is stored in a and the
%number of cars in Boston is stored in b for each week (each time through 
%the loop)  and the last result is the final number of cars at each place.   
for i=1:52   % for weeks 1 through 52, repeat below
    car_update    % call car_update to do calculations for you.  Abstraction.
end 
%

car_loop script with plotting

%car_loop_plot runs car_update 52 times to simulate the car moving activities
%for a year.  Then it plots the change in car values,
%preconditions: must assign a positive integer to a and b
%postcondition: the number of cars left in Albany is stored in a and the
%number of cars in Boston is stored in b for each week (each time through 
%the loop) 

hold on     % hold off on plotting until the very end
for i=1:52    % for weeks 1 through 52, repeat below
    car_update        % calls car_update script to get answer from there
    plot(i, a, 'ro')  % set albany to print in red circles
    plot(i, b, 'bd')  % set boston to printin blue diamonds
end %
Setting a= 150 and b = 150:

setting a = 1000 and b = 1000

Fibonacci sequence script
This, unlike the last one, uses loops to calculate the Fibonacci numbers.  This is an introduction to loops.

% this is another way of doing Fibonacci but using loops
% precondition: set n which is the nth element of the fibonacci sequence
% postcondition: will return the fibonacci sequence up to the nth element
% Note: this does not work for n < 3
prev1 = 1;  % this is the previous fib n-1
prev2 = 1;  % fib n-2

F = prev1   % prints out 1st element of fib seq
F = prev2   % prints out 2nd element of fib seq 

for i = 3:n  % for fib from 3 to n 
    F = prev1 + prev2   % calculate current fib from previous fibs
    prev2 = prev1;      % shift old fib(n-2) to be fib(n-1)
    prev1 = F;          % shift old fib(n-1) to be F
end
%

plotting Fibonacci ratios

% this plots the ratio between the previous and current fibonacci numbers
% precondition: set n to be fibonacci sequence up to the nth fibonacci number 
% postcondtion: get a plot of the Fibonacci ratios and the nth fibonacci number
% F is a vector of fib numbers
% R is a vector of fib ratios
F(1) = 1    % first fib number set to be 1 
F(2) = 1    % second fib number set to be 1
hold on     % holds off on printing the graph until the en d
R(2) = 1    % the ratio R(2) is F(2)/F(1)
plot(2, R(2), 'ro-')  % plot the first ratio in red circles
for i=3:n    % for fib fron 3 to n 
    F(i) = F(i-1) + F(i-2)  % calculate fib from prev fibs
    R(i) = F(i)/F(i-1)      % calculate ratios
    plot(i, R(i), 'ro-')    % plot ratio in red circles
end

ans = F(n)   % oh yeah and return answer
%

setting n = 50, getting

setting n = 100

as you can see, the ratio tends towards one number



simulating best trajectory angle:
to find the best angle to get the farthest distance, I cycled through all of the reasonable angles and for the result of each fire at that angle, I decide whether to update my maximizing angle value based on whether the new distnace achieved exceeds the previous farthest distance achieved.


%
    % Model of trajectory of hit baseball. Assumes no air drag.

    % What angle maximizes the distance travelled?
maxing_angle = 0; 
max_dist = 0;
 for a = 0:90    
    
    clf % clear the previous figure

    hold on % allow plotting of multiple figures

    angle = a; % angle baseball hit, in degrees

    velocity = 50 % initial velocity of baseball, in meters/sec

    rads = angle * pi / 180;

    velocity_x(1) = velocity * cos(rads); % horizontal velocity of baseball

    velocity_y(1) = velocity * sin(rads); % vertical velocity of baseball

    x(1) = 0; % x position of batter

    y(1) = 1; % assume baseball hit 1 meter off ground

    dt = 0.1; % time step for simulation, in seconds

    g = 9.8; % gravitational constant, in meters/sec^2

    i = 1 % iteration index

    while y(i) > 0

    x(i+1) = x(i) + velocity_x(i)*dt; % Update x position

    y(i+1) = y(i) + velocity_y(i)*dt; % Update y position

    velocity_y(i+1) = velocity_y(i) - g*dt; % y velocity changes due to gravity

    velocity_x(i+1) = velocity_x(i); % x velocity doesn't change (assume no air drag)

    plot(x(i), y(i), 'r.-'); % display a red dot for each point, and connect them with lines

    i = i + 1; % change index for next iteration

    end

    final_dist = x(i) % Display the final x value.
    if max_dist <= final_dist
        max_dist = final_dist;
        maxing_angle = a;
    end
 end
 ans = maxing_angle
 %



some peculiarities of Matlab
indexing starts at 1 like math and Julia and not like Python or Java or C.
1:5 prints out 1 2 3 4 5, including the 5 and not 1 2 3 4 like in Python.
(4+3)(3-2) does not work because it is missing the *    it should be (4+3)*(3-2)
exponents are simply 3^2 like in math, not exp(3,2) like in Python
I think Julia is going after Matlab.  I want to see Julia become more popular.

Also, I'm so excited that bash is coming to Windows!











1 comment:

  1. your coding is extremely detailed and i like how you wrote about peculiarities at the end

    ReplyDelete