Research of inflexion point - Index exceeds the number of array elements.

2 次查看(过去 30 天)
Hello
As part of a data analysis project, I've written a function to calculate the inflection points of a curve using its derivatives.
The program consists of two functions and a main code. A first basic function calculates the derivatives :
function F1 = derivee(x1, x2, y1, y2)
F1 = (y2-y1)/(x2-x1);
end
A second function calculates inflection points using the signs of left, center and right derivatives:
function [outputArray] = inflexion_point(x, y)
dg = [];
dd = [];
dc = [];
n = length(x);
dd(1) = derivee(x(2), x(1), y(2), y(1));
dg(1) = derivee(x(1), x(2), y(1), y(2));
dc(1) = derivee(x(1), x(3), y(1), y(3));
for i = 2:(n-1)
dd(i) = derivee(x(i), x(i+1), y(i), y(i+1));
dg(i) = derivee(x(i-1), x(i+1), y(i-1), y(i+1));
dc(i) = derivee(x(i-1), x(i+1), y(i-1), y(i+1));
end
dg(n) = derivee(x(n-1), x(n), y(n-1), y(n));
inflex = [];
plat = [];
for i = 2:(n-1)
if dg(i) * dg(i+1) == 0 && y(i) == y(i+1)
j = i + 1;
while j < n && dg(j) == 0 && y(j) == y(j+1)
plat = [plat j];
j = j + 1;
end
elseif dg(i) * dg(i+1) < 0 || dd(i) * dd(i+1) < 0 || dc(i) * dc(i+1) < 0
inflex = [inflex i];
end
end
xinflex = x(inflex);
yinflex = y(inflex);
dg %This six lines are just to try to figure out where the problem is
dc
dd
a=length(dd)
b=length(dc)
c=length(dd)
outputArray = [xinflex', yinflex'];
end
I made a first test with a basic data set in my main file:
x=[]
y = [10,9,8,7,6,5,5,6,8,8,7,5,4,3,4,5,6,6,5,5,4,3,2,1,1,1,1,1];
for i = 1 : length(y)
x = [x i]; %ici, pour construire les x
end
[outputArray] = inflexion_point(x,y);
xinflex = outputArray(:,1);
yinflex = outputArray(:,2);
figure(1)
plot(x,y)
hold on
scatter(xinflex, yinflex)
In the real case, my data are data vectors (X,Y) of length 199, so I created a random vector to generate data for a more consistent test. And then, when I compile, I get the error
Index exceeds the number of array elements. Index must not exceed 198.
lowerBound = 0;
upperBound = 800;
y = (upperBound - lowerBound) * rand(1, 199) + lowerBound;
for i = 1 : length(y)
x = [x i]; %ici, pour construire les x
end
[outputArray] = inflexion_point(x,y);
xinflex = outputArray(:,1);
yinflex = outputArray(:,2);
figure(1)
plot(x,y)
hold on
scatter(xinflex, yinflex)
is displayed. In the inflexion_point function, I tried to modify the end indices of the for and while loops by stopping at the previous point, but this didn't change anything and the error "adapted" by becoming :
Index exceeds the number of array elements. Index must not exceed 197.
I don't understand why this code works for one lambda data set and not for another.
Is there a step I've missed or a glaring error in my code?
Thank you in advance for your help!

采纳的回答

Hélène
Hélène 2023-6-21
Ok I found what the problem was, this line
dg(n) = derivee(x(n-1), x(n), y(n-1), y(n));
was adding an extra term to dg, so dc dd and dg were not the same length. Moreover, in this for loop :
for i = 2:(n-1)
if dg(i) * dg(i+1) == 0 && y(i) == y(i+1)
...
end
the loop went to index n-1, but the if condition was looking for the n-th term of dg which didn't exist. I solved the problem by modifiying the last index of the for loop :
for i = 2:(n-2)

更多回答(1 个)

Torsten
Torsten 2023-6-20
编辑:Torsten 2023-6-20
You forgot to initialize x as
x = []
in the second example.
  1 个评论
Hélène
Hélène 2023-6-21
Hello,
Thanks for your reply but no, it's initialized, I just forgot this line in the copy/paste of the code I put here.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by