How to resolve this error message in interpolate1?

Hi, I am using the following code for interpolation:
clear; clc;
y=load ('xx11.0.txt');
x=load ('yy11.0.txt');
plot(x(:,2),-sum(y(:,2:11),2))
xlabel('X')
ylabel('Y')
y1=-sum(y(:,2:11),2);
x1=x(:,2);
y3=unique(y1);
x3=unique(x1);
y2 = [9080.954]*1000;
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')
But I keep getting the following error message:
Error using interp1>reshapeAndSortXandV (line 423)
X and V must be of the same length.
Error in interp1 (line 92)
[X,V,orig_size_v] = reshapeAndSortXandV(varargin{1},varargin{2});
Error in Interpolate (line 15)
x2 = interp1(y3, x3, y2, 'linear');

11 Comments

Ismail,
The error is telling you that the dimensions of y3 and x3 are different. I ran the code and found that y3 is 5503x1, whereas x3 is 5501x1.
Hi Paolo,
Many thanks for your reply. That is actually strange. I am not sure why the dimensions are different. I have plotted them manually, and could not find that mismatch in dimensions.
For testing purposes I removed the last two points from y3 as follows:
y3= y3(1:end-2);
The code ran successfully. The graphical output:
Thank you very much Paolo for your help. It works now.
Because you used
y3=unique(y1);
x3=unique(x1);
to get the x,y values for the interpolant and it turns out there are different numbers that are unique for the two.
...
x2 = interp1(y3, x3, y2, 'linear');
By doing that independently, you also may have rearranged the x,y relationship somewhat in the area(s) in which there are duplicated values.
You might want to use the 'rows' option to make both x and y disappear where they aren't unique combinations.
"Working" doesn't necessarily imply "correct"; whether you got the right answer or not depends on where the UNIQUE() values were and what happened to the x,y relationship from that point forward.
Simply removing the two "extra" points from the longer vector may make no difference for the specific point, may make only an insignificant difference, or could have a profound effect on the result; all depends on just what the data are and how the values were rearranged owing to the independent removal.
@dpb that's correct, I did state that I just removed the two points for testing purposes only. I just pointed out the nature of the error, Ismail will have to determine why the data he is using right results in incorrect values when using unique().
Ismail (the OP) indicated it "was working"; response is to that comment.
As for the cause, I outlined the "why" of the differing lengths above as well as the manner in which it could be corrected.
Thank you very much bpb and Paolo for your explanation. I have tried the idea of Paolo and it gave me reasonable estimate, which is sufficient at this stage of my problem.
Very risky move... :)
Thanks dpb for your feedback. I am actually aware of it. I just need a rough estimate of the data at this stage. I shall get back to you when I need to be more accurate. Thank you very much again for your great help.

Sign in to comment.

Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Asked:

on 27 May 2018

Commented:

on 27 May 2018

Community Treasure Hunt

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

Start Hunting!