%%MATLAB code to solve Ques#2
clc;
clear;
% Performing interpolation (using Lagrangian polynomials) for the highest
order possible
abc=11;
%if abc=11 then we will solve for part (a) otherwise part (b) will be solved
%for question number 2.
if abc==11 %solving for part (a)
x=[0.6 1.1 1.3 2.2 3.3 4.4 5.1]; %x values
y=[0.0265 0.1918 0.2906 0.6093 0.4593 0.1921 0.0924]; %corresponding y
values
u=1.75; %value of x where y needs to be determined
N=numel(x); %Number of data points
%Initializing
b(1:N)=1;
d=0;
% Performing for loop to obtain the function value
for i=1:N
for j=1:N
if i==j
a(j)=1;
b(i)=b(i)*a(j);
else
a(j)=((u-x(j))/(x(i)-x(j)));
b(i)=b(i)*a(j);
end
end
c=y(i)*b(i); %this step corresponds to the multiplication term
corresponds to Li in the class notes
d=c+d; %this step correpond to the summation term and at the end
of the loop will result in the solution
end
else %part (b) will be solved.
x=[1 1.5 2 2.5 3 3.5 4 4.5 5]; %x values
y=[0.1469 0.3905 0.5758 0.6149 0.5355 0.4050 0.2763 0.1742 0.1033];
%corresponding y values
u=1.75; %value of x where y needs to be determined
N=numel(x); %Number of data points
%Initializing
b(1:N)=1;
d=0;
% Performing for loop to obtain the function value
for i=1:N
for j=1:N
if i==j
a(j)=1;
b(i)=b(i)*a(j);
else
a(j)=((u-x(j))/(x(i)-x(j)));
b(i)=b(i)*a(j);
end
end
c=y(i)*b(i); %this step corresponds to the multiplication term
corresponds to Li in the class notes
d=c+d; %this step correpond to the summation term and at the end
of the loop will result in the solution
end
end
Solution (the value of ‘d’ obtained for the corresponding value of ‘u’)
2(a) 0.4945
2(b) 0.4995
Solution 3
Sol 3(a): MATLAB code to obtain the linear spline interpolation
%% To Evaluate the value of y at x = 2.326 by using linear splines
%%Given data points and corresponding values
x= [-1.5;1;3;4];
y= [0.8325;0.37;3.33;5.92];
%%Number of data points
n=length(x);
%%Point to obtain the value
xf = 2.326;
%%Initializing the matrices
P=zeros(2*(n-1),2*(n-1));
R=zeros(2*(n-1),1);
%%Performing for loop to obtain the coefficients of 2(n-1) equations
for i=1:n-1
P(2*i-1,2*i-1) = x(i);
P(2*i-1,2*i) = 1;
P(2*i,2*i-1) = x(i+1);
P(2*i,2*i) = 1;
R(2*i-1) = y(i);
R(2*i) = y(i+1);
end
%%To obtain the unknown coefficients
Q=P\R;
%%Performing for loop to obtain the point value
for i=1:n-1
if (x(i)<xf) && (xf<x(i+1));
yf=Q(2*i-1,1)*xf + Q(2*i,1);
end
end
Y=yf;
Y
%%End of Program
Obtained result
>> Linear_spline
Y =
2.3325
Sol 3(b): MATLAB code to obtain the quadratic spline interpolation
%% To Evaluate the value of y at x = 2.326 by using quadratic splines
%%Given data points and corresponding values
x= [-1.5;1;3;4];
y= [0.8325;0.37;3.33;5.92];
%%Number of data points
n=length(x);
%%Point to obtain the value
xf = 2.326;
%%Initializing the matrices
P=zeros(3*(n-1),3*(n-1));
R=zeros(3*(n-1),1);
%%Performing for loop to obtain the coefficients of 2(n-1) equations
for i=1:n-1
P(2*i-1,3*i-2) = x(i)^2;
P(2*i-1,3*i-1) = x(i);
P(2*i-1,3*i) = 1;
P(2*i,3*i-2) = x(i+1)^2;
P(2*i,3*i-1) = x(i+1);
P(2*i,3*i) = 1;
R(2*i-1,1) = y(i);
R(2*i,1) = y(i+1);
end
%%Performing for loop to obtain the coefficients of (n-2) equations and
for i=1:n-2
%%First derrivative of interior points are equal
P(2*(n-1)+i,3*i-2) = 2*x(i+1);
P(2*(n-1)+i,3*i-1) = 1;
P(2*(n-1)+i,3*i+1) = -2*x(i+1);
P(2*(n-1)+i,3*i+2) = -1;
end
%%First derrivative of first and last point 1s 0 (1 equation)
P(3*(n-1),1) = 1;
%%To obtain the unknown coefficients
Q=P\R;
%%Performing for loop to obtain the point value
for i=1:n-1
if (x(i)<xf) && (xf<x(i+1));
yf=Q(3*i-2,1)*xf^2 + Q(3*i-1,1)*xf + Q(3*i,1);
end
end
Y=yf;
%%End of Program
Obtained result
>> Quadratic_spline
Y =
1.5885
Sol 3(c): MATLAB code to obtain the cubic spline interpolation
%% To Evaluate the value of y at x = 2.326 by using cubic splines
%%Given data points and corresponding values
x= [-1.5;1;3;4];
y= [0.8325;0.37;3.33;5.92];
%%Number of data points
n=length(x);
%%Point to obtain the value
xf = 2.326;
%%Initializing the matrices
P=zeros(4*(n-1),4*(n-1));
R=zeros(4*(n-1),1);
%%Performing for loop to obtain the coefficients of 2(n-1) equations
for i=1:n-1
P(2*i-1,4*i-3) = x(i)^3;
P(2*i-1,4*i-2) = x(i)^2;
P(2*i-1,4*i-1) = x(i);
P(2*i-1,4*i) = 1;
P(2*i,4*i-3) = x(i+1)^3;
P(2*i,4*i-2) = x(i+1)^2;
P(2*i,4*i-1) = x(i+1);
P(2*i,4*i) = 1;
R(2*i-1,1) = y(i);
R(2*i,1) = y(i+1);
end
%%Performing for loop to obtain the coefficients
for i=1:n-2
%%first derrivative of interior points are equal (n-2) equations
P(2*(n-1)+i,4*i-3) = 3*x(i+1)^2;
P(2*(n-1)+i,4*i-2) = 2*x(i+1);
P(2*(n-1)+i,4*i-1) = 1;
P(2*(n-1)+i,4*i+1) = -3*x(i+1)^2;
P(2*(n-1)+i,4*i+2) = -2*x(i+1);
P(2*(n-1)+i,4*i+3) = -1;
%%Second derrivative of interior points are equal (n-2) equations
P(3*n-4+i,4*i-3) = 3*x(i+1);
P(3*n-4+i,4*i-2) = 1;
P(3*n-4+i,4*i+1) = -3*x(i+1);
P(3*n-4+i,4*i+2) = -1;
end
%%Second derrivative of first and last point 1s zero (1 equation)
P(4*n-5,1) = 3*x(1);
P(4*n-5,2) = 1;
P(4*n-4,4*n-7) = 3*x(n);
P(4*n-4,4*n-6) = 1;
%%To obtain the unknown coefficients
Q=P\R;
%%Performing for loop to obtain the point value
for i=1:n-1
if (x(i)<xf) && (xf<x(i+1));
yf=Q(4*i-3,1)*xf^3 + Q(4*i-2,1)*xf^2 + Q(4*i-1,1)*xf + Q(4*i,1);
end
end
Y=yf;
%%End of Program
Obtained result
>> Cubic_spline
Y =
1.9488