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

조회 수: 1 (최근 30일)
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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by