In: Electrical Engineering
Q1. Interpolationv (MATLAB PROGRAM ) :
Suppose that x = 0:10;?
y = [1.5, 1.0, 0.9, 2.6, 2.2, 0.03];?
a)Use both linear interpolation and cubic spline interpolation function to determine the value of y when x= 3.5
b) Plot both functions y and the interpolated version of y.
PLEASE SOLVE THE QUESTION USING MATLAB PROGRAM .
x=0:2:10;
y=[1.5, 1.0, 0.9, 2.6,2.2,0.03];
% a). calculating the values of y(3.5) for linear and cubic
spline
% interpolations
xq=3.5;
y1=interp1(x,y,xq,'linear') % y(3.5) for linear interpolation
method
y2=interp1(x,y,xq,'spline') % y(3.5) for cubic spline
interpolation
% b). calculating the values of y for linear and cubic
spline
% interpolations and plotting the same along with y
xf=0:0.5:10;
y3=interp1(x,y,xf,'linear'); % y values for linear interpolation
method
y4=interp1(x,y,xf,'spline'); % y values for cubic spline
interpolation
figure;
plot(x,y,'-bo','linewidth',4);hold on;
plot(xf,y3,':*r','linewidth',2);hold on;
plot(xf,y4,':sr','linewidth',2);grid on; grid minor;
legend('y','y_{linear}','y_{cubic spline}');
xlabel('x');ylabel('y');
title('linear and cubic spline interpolation of y(x)');