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)
A second function calculates inflection points using the signs of left, center and right derivatives:
function [outputArray] = inflexion_point(x, y)
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));
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));
dg(n) = derivee(x(n-1), x(n), y(n-1), y(n));
if dg(i) * dg(i+1) == 0 && y(i) == y(i+1)
while j < n && dg(j) == 0 && y(j) == y(j+1)
elseif dg(i) * dg(i+1) < 0 || dd(i) * dd(i+1) < 0 || dc(i) * dc(i+1) < 0
outputArray = [xinflex', yinflex'];
I made a first test with a basic data set in my main file:
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];
[outputArray] = inflexion_point(x,y);
xinflex = outputArray(:,1);
yinflex = outputArray(:,2);
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.
y = (upperBound - lowerBound) * rand(1, 199) + lowerBound;
[outputArray] = inflexion_point(x,y);
xinflex = outputArray(:,1);
yinflex = outputArray(:,2);
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!