[問題] Fibonacci數.用while迴圈寫(幫檢查)已回收

看板MATLAB作者 (阿里八八與四十大盜)時間15年前 (2010/04/25 13:01), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串1/1
題目:第n個Fibonacci數,可由下列遞迴關係式推導而出 f(1)=1 f(2)=2 f(n)=f(n-1)+f(n-2) 因此,f(3)=f(2)+f(1)=3,並依此類推後面的數字,對n>2而言, 其中n由使用者輸入,請寫出一個M檔案,並計算出Fibonacci數 ,請使用WHILE迴圈執行計算。 我寫的: clc clear close all n = input('輸入一個大於2的整數:'); f(1)=1; f(2)=2; i=3; while (i<=n); f(i) = f(i-1)+f(i-2); i = i + 1; fprint('Fibonacci數 = %d \n',f(i)); end 可是跑出來說 Index exceeds matrix dimensions. 不知道錯在哪 可以幫我看看嘛 謝謝 初學者還請見諒... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.171.123.143

04/25 13:11, , 1F
i=i+1; 放在 fprintf(...); 後面
04/25 13:11, 1F

04/25 13:18, , 2F
謝謝樓上!!!^^
04/25 13:18, 2F
文章代碼(AID): #1BqyoofE (MATLAB)