Linear interpolation using inter1 function in matlab

조회 수: 2 (최근 30일)
Hassan
Hassan 2022년 8월 5일
댓글: Steven Lord 2022년 8월 5일
I have data in this format. I am reading the data using readtable command.
T G
2 6
3 8
4 9
5 0
I am using this code to perform linear interpolation in matlab. Data is read using following command X_t = readtable()
for i = 1:height(X_t)-1
x1(i) = X_t.T(i);
x2(i) = X_t.T(i+1);
y1(i) = X_t.G(i);
y2(i) = X_t.G(i+1);
for j = X_t.T(i) : X_t.T(i+1) -1
y_inter = interp1([x1,x2],[y1,y2]);
end
end
I am not sure if I am doing this correctly or not. Please help me.

답변 (1개)

Bjorn Gustavsson
Bjorn Gustavsson 2022년 8월 5일
편집: Bjorn Gustavsson 2022년 8월 5일
First you should learn to read the help to the function you struggle with. Do that carefully and you will see that the typical use of interp1 is:
Yi = interp1(x,y,xi);
Where you send in a third input argument for the points along the x-direction that you want the interpolated values Yi at. You have to change your call to something like:
x = X_t.T;
y = X_t.G;
Xi = linspace(min(x),max(x),10*numel(x)); % this you might want to modify to get values at your points-of-interest
y_inter = interp1(x,y,Xi);
HTH
  댓글 수: 2
Hassan
Hassan 2022년 8월 5일
Thank you very much for your reply. I tried doing this, but I am getting the error "sample points must be unique)
Xi =0;
Xi(i) = Xi + (X_t.T(i) + X_turb.T(i+1))/2;
y_inter = interp1([x1,x2],[y1,y2],Xi);
Steven Lord
Steven Lord 2022년 8월 5일
That means two or more of your X values are the same. MATLAB can't interpolate the data in that case. Consider a simple example:
X = [0 0.5 0.5 1];
Y = [0 0 1 1];
plot(X, Y, 'o-')
If I were to interpolate this data to try to find the value of Y at X = 0.5, what should that value be? Should it be 0, 1, or a value somewhere inbetween? Since that question is ambiguous, MATLAB throws an error.
interp1(X, Y, 0.5) % Error
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);

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by