Hi, I have a data containing X and Y coordinates which I'd like to plot them over fixed time intervals. I tried to use linear interpolation but it doesn't work. Can anyone help me with this please.
Xp = [1:0.25:50693];
yp =interp1(X,Y,Xp,'linear');
figure
plot(X,Y,'o');
hold on
plot(Xp,yp,'r-');
xlabel('X');
ylabel('Y');
title('X and Y positions')
grid on

댓글 수: 3

Dyuman Joshi
Dyuman Joshi 2022년 7월 31일
What exactly is not working? Do you not get the intended result or are you getting an error?
sahar daraei
sahar daraei 2022년 7월 31일
I get an error. Also It says the sample must be unique
Are there any repeating values in your data? That causes the error
X=[1 1 2 3 4 5 5]; %repeating values
Y=[2 3 4 5 6 7 8];
%you can check with repeating values in Y as well, it will give the same error
Xp=1:0.25:2.5;
Yp =interp1(X,Y,Xp,'linear')
Error using matlab.internal.math.interp1
Sample points must be unique.

Error in interp1 (line 188)
VqLite = matlab.internal.math.interp1(X,V,method,method,Xqcol);

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2022년 7월 31일
편집: Star Strider 2022년 7월 31일

0 개 추천

The data were not supplied, however it quite probably works correctly. You are asking it to do a linear interpolation, probably to a finer ‘x’ resolution, and it does exactly that.
The plots would appear to be the same because the method chosen is 'linear'. If you chose a different interpolation method, the plots might appear to be different.
EDIT — (31 Jul 2022 at 17:30)
To make the sample points unique:
X = sort(randi(50,100,1))
X = 100×1
1 1 3 3 4 4 4 4 5 5
Y = randi(90, 100, 1)
Y = 100×1
81 84 32 46 61 9 42 78 32 81
[Ux,i1] = unique(X)
Ux = 43×1
1 3 4 5 6 7 8 9 10 11
i1 = 43×1
1 3 5 9 12 14 15 16 18 19
X = Ux;
Y = Y(i1);
Xp = [1:0.25:50693];
yp =interp1(X,Y,Xp,'linear');
figure
plot(X,Y,'o');
hold on
plot(Xp,yp,'r-');
xlabel('X');
ylabel('Y');
title('X and Y positions')
grid on
It would help to have the actual data, however this illustrates the concept.
.

카테고리

도움말 센터File Exchange에서 Interpolation에 대해 자세히 알아보기

제품

릴리스

R2022a

태그

질문:

2022년 7월 31일

편집:

2022년 7월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by