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."

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 (Text):

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?

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?

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.

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.

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.

@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.

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.