interp1 monotonic increasing error

hi guys i got two arrays to interpolate (interp1). but i got an error :
>>Ct_related=interp1(J_00_B,J_00_Ct,10)
Error using griddedInterpolant
The grid vectors are not strictly
monotonic increasing.
Error in interp1 (line 191)
F =
griddedInterpolant(X,V,method);
how can i solve this ? any help?

 Accepted Answer

If you run this code:
difB = find([NaN; diff(J_00_B)] <= 0);
Q = [[difB-2:difB+2]' J_00_B(difB-2:difB+2)]
you will see that the values of J_00_B for indices 138 and 139 are the same. The independent variable has to be strictly monotonically increasing, so consecutive duplicate values are not allowed. See ‘Strictly Monotonic’ in the ‘More About’ section of the interp1 documentation. (The NaN in the diff call is to make the index references correct.)
Correcting that by adding a small value to the second duplicate:
J_00_B(difB) = J_00_B(difB)+1E-10;
Ct_related=interp1(J_00_B,J_00_Ct,10)
produces:
Ct_related =
96.1895e-003

5 Comments

Thank you Star Strider, but i got a problem with your code. When i type :
Q = [[difB-2:difB+2]' J_00_B(difB-2:difB+2)]
it gives me an error :
Subscript indices must either be real
positive integers or logicals.
and couldn't interpolate
I forgot to include a couple of lines that I used to be sure the two arrays were aligned and J_00_B was increasing.
Insert these before the difB assignment:
J_00_B = flipud(J_00_B);
J_00_Ct = flipud(J_00_Ct);
It will work then. I was experimenting to get interp1 to work and figure out why it wasn’t working. The J_00_B array is decreasing as posted, so flipping both arrays to make J_00_B increasing was my first step in troubleshooting your code.
Sorry for late reply but i really appriciate your solution.
Thank you very much.
My pleasure!
I apologise for not including the flipud lines the first time. I was interrupted while writing my Answer, and forgot them.
I have used that same trick (i.e. using diff to find duplicated x-data and then adding a very small increment to the duplicates) but yesterday I found a weakness in this approach: if you have more than two identical x-values then adding the increment to all the duplicates (i.e. two or more duplicates) still leaves some of them the same. With my data it was OK just to eliminate duplicates so that is what I did (may not suit all purposes of course), e.g.:
idx = diff([-9.99; x_data]) == 0.0; % Find duplicate x-values
zz = interp1(x_data(~idx), y_data(~idx), x_i); % Only use non-duplicated data

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!