Sunday, May 16, 2010

Matlab Challenges

Here are our solutions to the MATLAB exercises:

[Exercise 2.1: fibonacci1]
sq5 = sqrt(5);
n = 10;
ans = (1/sq5) * ((((1 + sq5)/2)^n) - (((1-sq5)/2)^n))

[Exercise 2.3: car_update]
a = round(a - (a*.05) + (b * .03))
b = round(b - (b*.03) + (a*.05))

[Exercise 3.1: car_loop]
for i=1:52
car_update
hold on
plot(i,a,'ro')
plot(i,b,'bd')
end

[Exercise 3.2: car_loop with plotting]
a(1) = 150;
b(1) = 150;

for i = 1:52
a(i) = a(i-1)+ round(.03*b(i-1)) - round(.05*a(i-1));
b(i) = b(i-1)+ round(.05*a(i-1)) - round(.03*b(i-1));
end

x=1:52;

hold on
plot(x, a, 'bd')
plot(x, b, 'ro')

[Exercise 3.5: fibonacci2]
f1 = 1;
f2 = 1;
prev1 = f1;
prev2 = f2;
result = 0;
n=10;

for i=3:n
fnew = prev1 + prev2;
prev1 = prev2;
prev2 = fnew;
end

prev2

[Exercise 4.6: Plotting fibonacci ratios]
F(1) = 1;
F(2) = 1;
n = 10;

for i=3:n
F(i) = F(i-1) + F(i-2);
hold on
plot(i,F(i),'ro')
end

for i=3:(n-1)
ratio = F(i+1)/F(i);
hold on
plot(i,ratio,'bo')
end

ratio

No comments:

Post a Comment