How do you graph a function that refers to the function in its value?

3 ビュー (過去 30 日間)
Nitishsai Nandineni
Nitishsai Nandineni 2019 年 10 月 23 日
回答済み: Shubham Gupta 2019 年 10 月 23 日
How do you graph a function that refers to the function in its value?
For example,
Function:
y(p)=[y(p-2)+y(p-1)]/2
y(1)=0
y(2)=[y(0)+y(1)]/2
How would you graph this, when p is equal to 1-20?

採用された回答

galaxy
galaxy 2019 年 10 月 23 日
what is y(0)?
please refer following example.
y(1) = 0;
y(2) = 1;
for i = 3:20
y(i) = (y(i-1)+y(i-2))/2;
end
plot(y)

その他の回答 (1 件)

Shubham Gupta
Shubham Gupta 2019 年 10 月 23 日
MATLAB doesn't allow 0 as an index. Infact, index must be a natural number.
y(0) % is an invalid index because 0
y(0.5) % is an invalid index because fraction
y(-1) % is an invalid index because negative
Now, to solve the problem for indexing that starts with 0, you can shift indeces by 1 unit forward. So, now your code will become.
y(0+1) = 0; % equivalent to y0
y(1+1) = 1; % equivalent to y1
for i = 2:20
y(p+1) = (y(p-2+1) + y(p-1+1))/2; % will calculate y(3), y(4),......, y(21) equivalent to y2, y3,....., y20
end
Now, once you have stored the data in y variable, you can plot it using the 'plot' function
plot(y)
Let me know if you have any doubts

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by