MATLAB: Problem to find inflection point

index exceeds array boundsinflection point

Hi, i previously follow the guidelines and tutorial provided by Stryker to solve the inflection points problem. At first, i used 19 values in an array and it come out fine. However, when i have 20 values inside my array, i got an error message: "Index exceeds array bounds". Can someone help me? i really appreciate any help.
My array with 20 values inside:
xNew = [-433.08032 -422.60339 -435.8176 -447.13495 -458.09866 -469.62628 -476.03198 -478.79868 -482.30139 -481.55859 -479.44727 -474.50156 -472.0987 -463.40927 -456.15408 -449.5589 -442.93631 -442.58139 -445.34409 -447.53372];
yNew = [1443.96497 1399.23254 1369.25476 1351.823 1329.43958 1299.13647 1268.26343 1234.25989 1201.34021 1174.68372 1145.37134 1118.427 1089.32715 1061.75366 1034.36926 1008.59052 982.44653 956.20862 933.88422 905.15338];

Best Answer

Note that with your data, in order to calculate the inflection points correctly, you need to reverse ‘x’ and ‘y’ in my code:
x = yNew;
y = xNew;
The index range worked for the original example (How to find inflection point, PLEASE HELP!), however it needs to be modified in the event that it over-writes the end of the vector.
Try this:
dydx = gradient(y) ./ gradient(x); % Derivative Of Unevenly-Sampled Data
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
zxidx = zci(dydx); % Approximate Indices Where ‘dydx=0’
for k1 = 1:numel(zxidx) % Loop Finds ‘x’ & ‘y’ For ‘dydx=0’
ixrng = max(zxidx(k1)-2,1):min(zxidx(k1)+2,numel(x));
inflptx(k1) = interp1(dydx(ixrng), x(ixrng), 0, 'linear');
inflpty(k1) = interp1(x(ixrng), y(ixrng), inflptx(k1), 'linear');
end
figure(1)
plot(x, y)
hold on
plot(x, dydx*10)
plot(inflptx, inflpty, 'pg', 'MarkerFaceColor','g')
hold off
grid
legend('Data', 'Derivative', 'Inflection Points', 'Location','best')
inflpts = sprintfc('(%5.3f,%5.3f)', [inflptx; inflpty].');
text(inflptx, inflpty, inflpts,'FontSize',8, 'HorizontalAlignment','center', 'VerticalAlignment','bottom')
I edited my original post to include this change (‘ixrng’).
.