Help with Padovan Sequence

2 ビュー (過去 30 日間)
jacob Mitch
jacob Mitch 2019 年 10 月 1 日
コメント済み: David K. 2019 年 10 月 1 日
I have a question that says
P(0)=P(1)=P(2)=1 , P(n)=P(n-2)+P(n-3)
use for loops to get a script for p(n)
I have written
function y=padovan(n)
P(1)=1; P(2)=1; P(3)=1;
for v=4:n
P(v)=P(v-2)+P(v-3);
end
y=P(n);
As you can see I have had to started at P(1)=P(2)=P(3)=1 instead of P(0)=P(1)=P(2)=1 as I get an error when trying to define P(0)=1, how would I change my script to include and start at P(0) instead of P(1) as I have done.

採用された回答

David K.
David K. 2019 年 10 月 1 日
You cannot start at P(0) because matlab does not let you index at 0. However, you can make it so that it works as expected. Everywhere that you have n, replace it with n+1.
  2 件のコメント
jacob Mitch
jacob Mitch 2019 年 10 月 1 日
Thanks david the script works as intended with n+1, however, I dont understand why P(0) would be included in the question and its relevancy.
David K.
David K. 2019 年 10 月 1 日
I expect that is because it is part of the definition of the Padovan Sequence. Padovan of 0 still needs to be 1 even if matlab cannot nicely index at 0.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by