Ask your own question, for FREE!
Mathematics 20 Online
OpenStudy (raffle_snaffle):

matlab help

OpenStudy (raffle_snaffle):

How would I make a function for a Fibonacci sequence?

OpenStudy (raffle_snaffle):

@phi

OpenStudy (raffle_snaffle):

What is going on here? I am trying to understand the fib(1) = 1 and fib(2) = 2

OpenStudy (phi):

what is the definition of fibonacci sequence ?

OpenStudy (raffle_snaffle):

is equal to the sum of the preceding two numbers

OpenStudy (phi):

so fib(1) and fib(2) are required as "startup values" , right ?

OpenStudy (raffle_snaffle):

Yes, I would agree.

OpenStudy (raffle_snaffle):

% asks user to enter a number N = input('Pick a number: '); fib = zeros(1, N); fib(1) = 1; fib(2) = 2; k = 3; while k <= N fib(k)= fib(k-1) + fib(k-2); k = k + 1; end fprintf('The Fibonacci sequence to %d term is \n', N); fprintf('%g', fib);

OpenStudy (phi):

often, people use recursion. you define a function fib(n) n= fix(n) % this makes sure you have an integer if n<1 return 0; (this is not allowed, but you want the function to do something reasonable) if the start values are fib(1) = 1 and fib(2)=2 , then the next statement could be if (n<=2) return n; this would return 1 or 2 , depending on n % otherwise, you recurse return fib(n-1)+fib(n-2)

OpenStudy (raffle_snaffle):

Okay how about this part? fib(k)= fib(k-1) + fib(k-2)

OpenStudy (phi):

that code is sequentially filling in the numbers you start with k=3 the first time through the "while loop" it calculates fib(3) = fib(2) + fib(1) it then increments k to 4 and does fib(4) = fib(3) + fib(2) and so on until k reaches N

OpenStudy (phi):

fib is an array, which starts out with 1 and 2 in the first two "slots" and the code fills in the next entries, using the two previous values in the array

OpenStudy (raffle_snaffle):

When you say fib(1) = 1 the 1 inside the para thesis is an index? Are we saying fib(1) = 1, so index 1 is assigned a number 1?

OpenStudy (phi):

yes, that is assignment you can create a small array fib= zeros(1,10) for example, and then do fib(1)=1 and see what it is doing

OpenStudy (raffle_snaffle):

So for this little bit of code Lets say N = 5 what happens? k = 3 which is less than 5 so does k = 3 run through the loop? while k <= N fib(k)= fib(k-1) + fib(k-2); k = k + 1; end

OpenStudy (raffle_snaffle):

I am doing this out on paper.

OpenStudy (phi):

if you define fib= zeros(1,5); then you can cut and paste your code into matlab if you leave off the semi-colon on the line fib(k)= fib(k-1) + fib(k-2) <- no ; it will print out what it's doing if you put in a statement pause, then it will stop until you press a key

OpenStudy (phi):

yes, for k=3 it executes the loop. the logic is: top: test if k is bigger than 5. if so, quit otherwise: do the statement fib(k)= fib(k-1) + fib(k-2) incitement k go to the top

OpenStudy (phi):

*increment k

OpenStudy (raffle_snaffle):

Is my logic correct?

OpenStudy (phi):

almost. k=4 not 5 in your last iteration (fib(4) is 5, but fib is not k)

OpenStudy (raffle_snaffle):

so fib(4) = 5 let me look at the math. but does fib(k) = k ? k = k + 1, the k on the right side is equal to k above

OpenStudy (raffle_snaffle):

so k = 3 get throw into fib(k) after you enter the Nth number?

OpenStudy (raffle_snaffle):

thrown*

OpenStudy (raffle_snaffle):

beginning number for the loop. The loop doesn't start with k = 1 or 2

OpenStudy (phi):

variables can be thought of as locations in memory, or mail boxes in a post office when we say fib(k) = fib(k-1) + fib(k-2) we first go to location (or mailbox) k, find the value stuffed in there, and get it. we get a 4 we then do fib(4) = fib(4-1) + fib(4-2) fib(4-1) or fib(3) is a location: 3 entry in the fib array. get the number. it's is 3 ditto for fib(2), we get a 2 fib(4) = 3+2= 5 this means "put a 5 in location "4th entry in fib array" next we do k= k+1 this says " get value in k" ... we get a 4 4+1 is 5 k= 5 which means "put a 5 in location k"

OpenStudy (phi):

so variables on the right-hand side mean "read or get the value in that location" variables on the left side of the =, mean "set that location" to the value of the right-hand side

OpenStudy (raffle_snaffle):

Okay I see. That is much better. Basically a k value isn't a number you will see on the output end. The k value is a place holder/and index.

OpenStudy (raffle_snaffle):

an*

OpenStudy (phi):

all variables are place holders (for values) some variables you only need for "temporary use" others you want to "return" to the calling procedure or print out on the screen

OpenStudy (raffle_snaffle):

So k = k +1 increments the kth value which is an index and the fib(k) = fib(k-1) + fib(k-2) function find the fib numbers

OpenStudy (phi):

yes. But to be clear, k is a variable, and we use it as an index (into the fib array) we could also use k in calculations if we wanted or had to.

OpenStudy (raffle_snaffle):

Okay that makes a lot more sense now. Thanks.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!