Matlab challenges

So my entire thesis has basically been done in matlab....plus I took CS112 which is a matlab course...so LOTS of matlab. I must say at this point I am not that worried about coding. It was nice to do some modular coding after my large gargantuan codes for my thesis (I haven't had time to make the code nice....not so good coding I've used). Here are the codes I made for the homework -- nothing was hard or weird...I did fib2 as recursive (woot).

function f = fibonnacci1( n )
%basic fibbonacci script for homework EXTD 160 ACO 4/13/2010 where n is how
%far you want to run the fibonnacci sequence. Returns the vector f with the
%sequence of numbers

f=zeros(n);
for i=1:n
    f(i)=(1/sqrt(5))*(((1+sqrt(5))/2).^i - ((1-sqrt(5))/2).^i);
end
end

function [ n2 ] = fibonnacci2(n)
%recursive fibonnaci script
if n<2
    n2=1;
else
    n2=fibonnacci2(n-1)+fibonnacci2(n-2);
end
end

function [ vec1 vec2 ] = fibplotratio(n)
%plot ratio of consecutive fibonacci sequences

vec1=zeros(n);
vec2=zeros(n-1);

vec1=fibonnacci1(n);

for i=2:n-1
    vec2(i)=vec1(i)/vec1(i-1);
end

plot(vec2)
%it converges to the golden ratio!
end



This is all of my code for the matlab scripts.


function [a2 b2] = car_update(a, b)
%based on execise 2.3 matlab script for EXTD 160 ACO 4/14/2010 where a, b
%are the number of cars in each location at the start of week, and a2, b2
%are the number of cars in each location at the end of the week

atemp=.03*b;
btemp=.05*a;
a2=a-btemp+atemp;
b2=b-atemp+btemp;

a2=round(a2);
b2=round(b2);

end

function [total] = car_loop(a,b,n )
%Calls the function car_update and checks to see what happens to car
%inventory over time given a starting car stock for each location and hte
%number of weeks, n, you want to run it for. EXTD 160 ACO 4/14/2010
aold=a;
bold=b;
a2=[];
b2=[];
total=zeros(n,2);
for i=1:n
    [a2 b2]=car_update(a,b);
    total(i,1)=a2;
    total(i,2)=b2;
    a=a2;
    b=b2;
end

%modified bit below for 3.2
figure
hold on
a=aold;
b=bold;
for i=1:n
    [a2 b2]=car_update(a,b);
    plot(i,a2,'ro')
    plot(i,b2,'bd')
%     if i==10:10:n
%         clf
%         hold on
%     end
    a=a2;
    b=b2;
end
end

Here is my plot of car_loop over 1 year of work.

Comments

Popular posts from this blog

Hopper Last

Thermal Work - Matlab -- Write up

Hopper Finale