Interpolation points for determining values in between known data points

In this function, i want to use interp1 to generate a linear, cubic, and spline interpolation among points in the above sequence. The x values to be interpolated are x2interp = 1:0.1:10. I want to assign the resulting arrays of linear cubic, and spline interpolated values to y1, y2, and y3, respectively. Then plot the 3 dierent interpolations along with the original data on a single gure. (for the graph I am providing a legend to indicate the type of interpolation that each curve represents. blue circles to represent original data, red dashed line for linear interpolation, green dotted line for cubic interpolation and black solid line for spline interpolation.)
The set of data is :
x 1 2 3 4 5 6 7 8 9 10
y -2 3 -4 6 -7 10 -17 25 -26 30
I think I know the basic idea on how to write a interpolation function, but how do I incorporate the above data into my function?
function [y1, y2, y3] = MyInterpolator()
interp1 = interp1(X,Y,Xi)
yI = interp1(X,Y,Xi,'linear')
Y2 = interp1(X,Y,Xi,'cubic')
Y3 = interp1(X,Y,Xi,'spline')
PP = spline(X,Y)
x2interp = 1:0.1:10
ylabel('y')
xlabel('x')
legend('original data','linar','cubic', 'spline')
lines = {'original data','linar','cubic', 'spline'}
legend(lines)

 채택된 답변

Star Strider
Star Strider 2012년 10월 26일
편집: Star Strider 2012년 10월 26일
You're almost there! If you want to pass the x, y, and x2interp to your MyInterpolator function, I suggest changing the first line to:
function [y1, y2, y3] = MyInterpolator(X, Y, Xi)
then call it as:
[y1, y2, y3] = MyInterpolator(x, y, x2interp)
I also mention that you need to correct some typos so that your code reads:
y1 = interp1(X,Y,Xi,'linear')
y2 = interp1(X,Y,Xi,'cubic')
y3 = interp1(X,Y,Xi,'spline')
MATLAB is case-sensitive, so Y1 is not the same as y1.
Please eliminate this line:
interp1 = interp1(X,Y,Xi)
You redefine interp1 with it as a variable rather than a function and the following three lines will throw an error because of that.

댓글 수: 4

so, in my test case, I am given
>> [y1, y2, y3] = MyInterpolator();
>> y1(2)
and I originally defined my function like you had, but I was told to get rid of the input variables, and leave the inputs in the function declaration blank so the test case would work. How is it possible to have a function without any inputs?
Yes, you can have a function without inputs. In your particular situation, you simply have to put your variables in your function, so that now it should begin with the following lines:
function [y1, y2, y3] = MyInterpolator()
x = [1 2 3 4 5 6 7 8 9 10];
y = [-2 3 -4 6 -7 10 -17 25 -26 30];
x2interp = 1:0.1:10;
so that the variables appear first. Then you either need to add:
Xi = x2interp;
or change Xi in your interp1 function calls to x2interp. You also need to change X and Y to x and y, or define:
X = x;
Y = y;
to be sure the variables and arguments all match. I strongly suggest changing the arguments to your functions, for example:
y1 = interp1(x,y,x2interp,'linear')
The MATLAB editor has some shortcuts for doing this. If it offers you the option of using Shift+Enter to change all the affected variables in your function at once (for instance Y to y), I reccommend it.
Not all functions have to have inputs if they don't do anything with them. See beep for example. It can take command line inputs and can return an output but doesn't need any input arguments to function. There are probably other examples; beep comes quickly to mind.
okay, i completely forgot that you can still put in variables after you declare the function! that clears things up so much. thank you!!
My pleasure! I'm glad it worked!

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 26일
편집: Azzi Abdelmalek 2012년 10월 26일
function [y1, y2, y3] = MyInterpolator()
x =[ 1 2 3 4 5 6 7 8 9 10]
y=[ -2 3 -4 6 -7 10 -17 25 -26 30]
plot(x,y,'ob')
xi=1:0.1:10
method=char('linear','cubic','spline')
col={'--r',':g','-k'}
for k=1:3
yi{k}=interp1(x,y,xi,method(k,:))
hold on;plot(xi,yi{k},col{k})
end
y1=yi{1}
y2=yi{2}
y3=yi{3}

카테고리

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

질문:

Ben
2012년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by