(1/(5^0.5))*((((1+(5^0.5))/2)^n)-(((1-(5^0.5))/2)^n))
This seems like a lot of parenthesis for just this line of code. If there's a more efficient way to write this, I would be glad to hear comments about it. I could alternatively write this through variables like:
a = (1/(5^0.5));
b(((1+(5^0.5))/2)^n;
c((1-(5^0.5))/2)^n;
a*(b-c)
But it doesn't seem as efficient either.
Exercise 2.3: car_update script
For this, I initially assigned a and b to be equal to 150 and stored the values through the prompt. My script is as follows:
For this, I initially assigned a and b to be equal to 150 and stored the values through the prompt. My script is as follows:
%car_update updates the number of cars in each location from
%one week to the next. Precondition: variables aStor and bStor contain
%the number of cars in each location at the beginning of the week
%Postcondition: a and b have been modified to reflect the number of cars
%that have moved
aStor=(b*0.03)+a - (a*0.05)
bStor=(a*0.05)+b - (b*0.03)
a = aStor
b = bStor
a represented the number of cars in Albany while b represented the numbers of cars in Boston. Since there is a 2% difference between the transfer of cars in each city, Boston ended up with the majority of cars after a few weeks.
Exercise 3.1: car_loop script
%car_loop updates the number of cars in each location via a for loop from
%one week to the next. Precondition: variables aStor and bStor contain
%the number of cars in each location at the beginning of the week
%Postcondition: a and b have been modified to reflect the number of cars
%that have moved
a = 150
b = 150
for i = 1:52
aStor=(b*0.03)+a - (a*0.05)
bStor=(a*0.05)+b - (b*0.03)
a = aStor
b = bStor
end
I borrowed the code that I used from exercise 2.3 but I've always preferred for loops over recursion.
Exercise 3.2: car_loop script with plotting
a = 10000 % or 150
b = 10000 % or 150
for i = 1:52
aStor=(b*0.03)+a - (a*0.05)
bStor=(a*0.05)+b - (b*0.03)
a = aStor
b = bStor
hold on
plot (i, a, 'o')
plot (i, a, 'ro')
plot (i, b, 'bd')
end
Exercise 3.5: fibonacci2 sequence script
%creates the Fibonacci sequence using for loops instead of recursion.
%Precondition: n represents the level that the sequence is on. a, b, and
%c represent sections of the Fibonacci sequence while Fx represents the
%entire equation. Postcondition: the 10th element is assigned to the
%answer.
for i = 3:10
n = i
a = (1/(5^0.5));
b = (((1+(5^0.5))/2)^n);
c = (((1-(5^0.5))/2)^n);
Fx = a*(b-c);
end
Fx = a*(b-c)
I later adjusted the code to allow users to compute the nth element for any value of n. However, users must set the n before the script is run.
n = 3 %user can change this number
for i = n
a = (1/(5^0.5));
b = (((1+(5^0.5))/2)^n);
c = (((1-(5^0.5))/2)^n);
Fx = a*(b-c);
end
Fx = a*(b-c)


I like how you assigned everything its own letter, rather than a letter and a sub letter. I feel like it makes you code easier to follow because there are no similarities between different variables and values. You've minimized reader confusion.
ReplyDelete