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

1 vue (au cours des 30 derniers jours)
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?

Réponse acceptée

galaxy
galaxy le 23 Oct 2019
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)

Plus de réponses (1)

Shubham Gupta
Shubham Gupta le 23 Oct 2019
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

Catégories

En savoir plus sur Graph and Network Algorithms dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by