Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

When I run this code my fibonacci sequence is missing a beginning 1. Can someone please tell why it is doing this and how I can fix it?

조회 수: 1 (최근 30일)
The output I get when inserting 1 for the fist number, 1 for the second number, and 10 for the total number of elements is the following: 1 2 3 5 8 13 21 34 55 89. There should be another 1 at the beginning of this output.

답변 (1개)

Rajiv Ghosh-Roy
Rajiv Ghosh-Roy 2014년 2월 28일
You initialize a(1) to 1 and a(2) to 1, but then on line 7 you run
a = 1:z;
This changes a(1) to 1 and a(2) to 2. You can remove the line 7 (which makes a grow on each loop iteration), or you can put a=1:z before line 5. Even better, initialize it to a = zeros(1, z), again before line 5.

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by