Index exceeds number of array elements (1)?

This is the code on derivada.m:
function [Hd] = derivada(valores, h)
i = 2;
while i < length(valores)
Hd(i) = (valores(i+1) - valores(i-1))/(2 * h);
i = i+1;
end
Hd(1) = ((valores(2) - valores(1))/h);
Hd(length(valores)) = (valores(length(valores)) - valores(length(valores)-1))/h;
end
And this is the main:
valores = y;
h = 0.1;%t(2) - t(1);
Hd = derivada(valores, h);
This is the error:
Index exceeds number of array elements (1)

Answers (1)

It would appear valores only has a single value, yet you are trying to index a second.
You might find the explanation I gave in this post helpful.

Answered:

on 9 Jan 2021

Community Treasure Hunt

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

Start Hunting!