matlab help
How would I make a function for a Fibonacci sequence?
@phi
What is going on here? I am trying to understand the fib(1) = 1 and fib(2) = 2
what is the definition of fibonacci sequence ?
is equal to the sum of the preceding two numbers
so fib(1) and fib(2) are required as "startup values" , right ?
Yes, I would agree.
% 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);
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)
Okay how about this part? fib(k)= fib(k-1) + fib(k-2)
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
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
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?
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
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
I am doing this out on paper.
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
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
*increment k
Is my logic correct?
almost. k=4 not 5 in your last iteration (fib(4) is 5, but fib is not k)
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
so k = 3 get throw into fib(k) after you enter the Nth number?
thrown*
beginning number for the loop. The loop doesn't start with k = 1 or 2
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"
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
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.
an*
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
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
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.
Okay that makes a lot more sense now. Thanks.
Join our real-time social learning platform and learn together with your friends!