How to fix this error in interp1?
41 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I am trying to interpolate X values from Y values using a set of imported data.
The problem is that when I set both x and y as unique values, they do not have the same length, and I get the following error message:
Error using interp1>reshapeAndSortXandV (line 424)
X and V must be of the same length.
Error in interp1 (line 93)
[X,V,orig_size_v] = reshapeAndSortXandV(varargin{1},varargin{2});
Error in FindMax (line 18)
x2 = interp1(y3, x3, y2, 'linear');
The code:
clear; clc;
y=load ('X.txt');
x=load ('Y.txt');
plot(x(:,2),-y(:,2));
y1=-y(:,2);
x1=x(:,2);
y3=unique(y1);
x3=unique(x1);
a= size(y3)
b= size(x3)
y2 = [100.0 10.0 95.0];
x2 = interp1(y3, x3, y2, 'linear');
figure(1)
plot(x1, y1, '-g')
hold on
plot(x2, y2, 'bp')
hold off
grid
legend('Data', 'Interpolated Points', 'Location', 'NW')
0 commentaires
Réponses (1)
Walter Roberson
le 4 Avr 2019
You cannot fix that. Your x has duplicate values, as it goes left and then back towards the right again. The number of unique values in y is not going to match the number of unique values in x.
perhaps:
[y3, yidx] = sort(y3);
x3 = x1(yidx);
x2 = interp2(y3, x3, y2, 'linear');
However, the maximum y3 value is -1.17e-9 . You cannot justify linear interpolation to the y2=10 or y2=100 range.
4 commentaires
Walter Roberson
le 8 Avr 2019
In my code, I sorted y3 and I took the sorted y3 as the first parameter to interp1()
In your code, you unique x1 producing x3, and you take x3 as the second parameter to interp1()
You have to be doing the sort or unique with respect to the parameter that you are going to use first in interp1()
Your x1 are already unique, and it is your y1 that are not unique, but you are treating y1 as your independent variable and x1 as your dependent variable.
Consider for example y1 = 3008570 . When asked to interpolate at that value, do you want to return x1 = 16 (array index 74), or do you want to return x1 just slightly different than 544.75 (near array index 2189, which is y1 = 3008750)? If asked to interpolate at y1 = 3008750 then should the result be just slightly different than x1 = 16 (array index 74), or should it be x1 = 544.75 (array index 2189) ? That is, should the choice of whether to interpolate based upon the rise or the descent be based upon which happens to be the closer match to the interpolating location, rather than trying to pick consistently from the rise or the descent ?
Voir également
Catégories
En savoir plus sur Interpolation dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!