index must be a positive integer or logical

for i=1:1:7
P(1)=1000;
P(7)=500;
f(i)=P(i-1)^2-P(i-1)*P(i)-P(i)^2+P(i)*P(i+1);
disp f(i)
end
All my declared variables here are integers. How do I resolve this error?

Respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 22 de Mzo. de 2016
Editada: Azzi Abdelmalek el 22 de Mzo. de 2016

0 votos

when you write p(i), make sure that i is a positive integer, not equal to 0. in your case, you have p(i-1), if i is equal to 1, then i-1 will be 0, which is not allowed by Matlab as an index

1 comentario

Amey
Amey el 22 de Mzo. de 2016
Yes that worked! thank you.

La pregunta está cerrada.

Etiquetas

Preguntada:

el 22 de Mzo. de 2016

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by