How to store a Fibonacci Sequence in an array using a for or a while loop?

14 visualizzazioni (ultimi 30 giorni)
So I'm attempting to create a code that will allow the user to input any values they desire to find a fibonacci sequence with the user's desired number of elements for the sequence. I was able to get the sequence to produce values and work for any input. I'm now trying to take the outputs for the sequence and store it in an array using a for or a while loop, then I must display the Array. After displaying the array, I need to plot the results on a polar graph where the element number is the angle and the value of the element in the sequence is the radius. I'm running into trouble getting the output of the elements into an array and I would also like to display and plot it as stated above.
Here is what I have so far: x=input('Pick the first number of the sequence\n');
y=input('Pick the second number for the sequence\n');
k=input('Enter the total number of elements desired\n');
fib=zeros(1,x);
fib(1)=x;
fib(2)=y;
s=3;
while s<=k
fib(s)=fib(s-2)+fib(s-1);
s=s+1;
end
fprintf('The Fibonacci Sequence to %d terms is \n',k);
fprintf('%g ',fib);
fprintf('\n');
valueOffib=zeros(1,x);
for s=1:k
fib=fib(s);
valueOffib(s)=fib;
end
Here is the result and error that is occurring when I attempt to run the script:
Pick the first number of the sequence
1
Pick the second number for the sequence
2
Enter the total number of elements desired
5
The Fibonacci Sequence to 5 terms is
1 2 3 5 8
Attempted to access fib(2); index out of bounds because
numel(fib)=1.

Risposte (2)

Thorsten
Thorsten il 27 Apr 2015
After the first while loop you already have the array with the values, namely fib. A Cartesian plot would be plot(1:k, fit), for a polar plot you can use polar(theta, fib), where theta are the angles in radians
theta = (1:k)/180*pi;
  1 Commento
Daniel Rauch
Daniel Rauch il 27 Apr 2015
Modificato: Daniel Rauch il 27 Apr 2015
Thorsten, Thank you for the feedback. I guess I'm still a little confused as to getting the outputs into an array. What I'm getting from what you said was that the outputs that the script creates will automatically be put into an array, then a simple polar graph will graph the outputs?
I was under the assumption that I needed to put code in in order to place these values into an array. I am also attempting to display the array which I'm not certain if the code is already taking care of this or not.
Any further help would be great.
Thank you
EDIT: If I wasn't clear about this above, the values have to be stored into an array using the for or while loop. Still uncertain if the coding I provided already does this.

Accedi per commentare.


James Tursa
James Tursa il 27 Apr 2015
You have a typo in your pre-allocation. zeros(1,x) should be zeros(1,k). I.e.,
fib=zeros(1,x);
should be
fib=zeros(1,k);
The for-loop doesn't make any sense because you already have the fibonacci sequence from the while-loop.
Also, it is not clear what you intend to plot. There is no "angle" associated with a single number. Did you mean to plot the angle associated with a point produced by grouping two successive numbers? And the "radius" maybe the ratio of two successive numbers? I.e., going after the Golden Ratio?
  1 Commento
Daniel Rauch
Daniel Rauch il 27 Apr 2015
Thank you for the correction to the pre-allocation. I guess I got a little confuse in regards to the goal. I think what I want to do is just use the while loop for the Fibonacci sequence to create an array using those given values.
As for the plot, I need to plot the values using a polar graph, where the element number(k) is the angle and the value of the element (the number produced by the fibonacci sequence) as the radius. I figured out how to get the plot graphed so maybe that means that all my problems have been solved, including displaying the fibonacci sequence, storing it as an array, and plotting it as a polar graph.
Thank you for your help. I hope I made a little more sense this time around.

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by