Interpolation points for determining values in between known data points
Afficher commentaires plus anciens
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)
Réponse acceptée
Plus de réponses (1)
Azzi Abdelmalek
le 26 Oct 2012
Modifié(e) : Azzi Abdelmalek
le 26 Oct 2012
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}
Catégories
En savoir plus sur Spline Postprocessing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!