# Matlab Fibonacci sequence?

#### THCynical

Joined May 5, 2014
26
I'm a bit stuck with the task below. i don't expect someone to do it for me, but i don't know how to even begin to express a Fib sequence using a for loop.

"A Fibonacci sequence is composed of elements created by adding the two previous elements. Using a
for loop or a while loop, create a program that will find the find the first 20 elements of the sequence
starting with two numbers input by the user. The average value of the ratio of consecutive elements
in the sequence must also be calculated."

This is my wonderful attempt...

f1= input('f1:')
f2= input('f2:')
help for

Any help much appreciated.

#### Papabravo

Joined Feb 24, 2006
19,872
In order to come up with an algorithm for doing something you should be able to do it by hand. If I tell you what the first two numbers are then you can compute the third member of the sequence by adding those first two numbers together. Let's write an expression describing this operation:
Code:
3rd = 1st + 2nd
Now if I ask you to write an expression for finding the 4th member of the sequence, what would it be?
Now if I ask you to write an expression for finding the Nth member of the sequence, what would it be?

#### THCynical

Joined May 5, 2014
26
Cheers, Sounds like a good approach

3rd = 1st + 2nd
4th = 2nd + 3rd
Nth =... sum of previous two?

Its late where i am, ill look at it with fresh eyes in the modge

#### Papabravo

Joined Feb 24, 2006
19,872
Yes, but the expression I was looking for was:
Nth = (N-1)st + (N-2)nd

[Tongin in cheek] If I'd a wanted words, I'd a asked for words. #### THCynical

Joined May 5, 2014
26
ha yeah, well that's fair enough, but i was hoping 'sum of previous two' was an actual function.

Nth = (N-1)st + (N-2)nd

I don't know how input that as anything other than, a variable, N, -1.
I get what your saying, but how do you refer to a number by its "place" in the sequence, it being the 4th or 5th number?

for v1=1
v2=2;
fprintf('%d %d %d\n',v1,v2,v+v2)
end

#### djsfantasi

Joined Apr 11, 2010
8,792
So, let's say (N-1)th is the last number and (N-2)th is the number before that. This is the same as your second and first numbers. Right?

After you calculate the Nth, and print it or save it, what does it become? Why, the last number! You need to shuffle the numbers to their new places.

Can you show the code (or pseudocode) to shuffle the numbers (variable contents)?

#### Papabravo

Joined Feb 24, 2006
19,872
In most programming languages there is the concept of a linear array of objects where each of the objects is the same type. For example an array of integers. The elements are ordered and each element has an index number that you can use to refer to it's value. This index value is often called a subscript an is usually also an integer. Let's say I have the following declaration:

array a contains 20 integers ;

Let us assume that in our fictional programming language the first element of the array can be referred to as a(0). The next element in the array would be a(1). Pop quiz: "How would you refer to the last element in the array?

We could start the generate the standard Fibonacci sequence as follows:

a(0) = 1 ;
a(1) = 1 ;
a(2) = a(1) + a(0) ;
.
.
.
For 20 elements you could write this out explicitly, but I think you wanted to use a looping construct. Is that correct?

#### WBahn

Joined Mar 31, 2012
28,186
What you are seeing is two different approaches to the problem. Djsfantasi is working toward a solution that only keeps the information needed to proceed while Papabravo is working toward a solution that keeps all the information. Both have advantages and disadvantages and both approaches are good to understand. But it is helpful to think of them at two, largely incompatible, approaches so that you don't get confused trying to combine them. Pick one and push it through to the end, then go back and do the same with the other.

Since you are doing this in Matlab, I would recommend taking the array-based approach of Papabravo since Matlab is about vectors, arrays and matrices and also because, probably, the next assignment you will be given will be to vectorize the solution.

• djsfantasi

#### djsfantasi

Joined Apr 11, 2010
8,792
Exactly, WBahn. The advantages to my approach are speed and size of working memory. It's disadvantage is it's loss of historical information. Note that even the average of neighboring pairs ratio can be calculated with my technique.

But as PapaBravo is working toward a vector solution and WBahn recognizes that as a potential next step, I'll bow out. It's programming is interesting as well and perhaps easier to understand.

#### Papabravo

Joined Feb 24, 2006
19,872
Actually I think the problem is twofold. The TS/OP is not sufficiently fluent in Matlab to express even simple concepts. On top of that he has little experience in breaking problems into small chunks and applying the divide and conquer approach. Working on both simultaneously is always difficult. As I explained in another thread I no longer have access to a licensed copy. I did have a \$99.00 student copy when I went back to graduate school more than a decade ago. I can't really help him with the Matlab formulation; the best I can do is trigger his desire to read the help files and just try things.

#### WBahn

Joined Mar 31, 2012
28,186
@OP: Are you comfortable with any programming languages? If so, then if can often help to solve the problem in a language you are comfortable with so that you can focus on the logic involved and then port that to Matlab so that you can focus on the syntax involved.

#### THCynical

Joined May 5, 2014
26
I have no programming skills other than basic excel, these assignments are a precursor the the computer programming lectures that start after Christmas. I really don't no how to approach matlab

Papabravo's [a=array;] [a(1)=] is how to specify the first [a(2)=] the second and so on, helped me to get it working.

clc
clear

N= input('Enter Sequence Lenth:');

fib=zeros(1,N);
mag=zeros(1,N-1);

fib(1)= input('Enter 1st number:');
fib(2)= input('Enter 2nd number:');

k=3;
while k <= N
fib(k)=fib(k-2)+fib(k-1);
k=k+1;
mag=fib(k-1)/fib(k-2);
end

fprintf('The Fibonacci sequence to %d terms is\n',N);
fprintf('%g ',fib);
fprintf('\n');
fprintf('\n');
fprintf('The Average Ratio of the Terms is');
fprintf(' %g ',mag);
fprintf('\n');

#### WBahn

Joined Mar 31, 2012
28,186
Your code indicates that 'mag' is an average of something. But look at how you calculate it. You overwrite whatever it was with the latest value of it. Is that what you want?

#### djsfantasi

Joined Apr 11, 2010
8,792
Your calculation of 'mag' gives you something that the problem needs, the ratio of subsequent values. But it asks for the average over the range of numbers. So what more do you need to do? As a hint, you'll need the total of ratios in order to compute the average.

You can calculate the total as your go along, or create another list containing the ratio of the numbers.

#### THCynical

Joined May 5, 2014
26
Yup, that's correct, think i have it now though. ratio of each, average of ratios.

while k <= N
fib(k)=fib(k-2)+fib(k-1);
k=k+1;

mag(k-3)=fib(k-1)/fib(k-2);

magt=mean(mag)

end