Assignment 3 Solution
Assignment 3 Solution
clc;
clear;
abc=11;
%if abc=11 then we will solve for part (a) otherwise part (b) will be solved
%for question number 2.
%Initializing
b(1:N)=1;
d=0;
%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
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
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
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
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
for i=1:n-2
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
%%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
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
for i=1:n-2
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;
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;
Y=yf;
%%End of Program
Obtained result
>> Cubic_spline
Y =
1.9488