0% found this document useful (0 votes)
11 views4 pages

ES341

Uploaded by

Omar Amin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

ES341

Uploaded by

Omar Amin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment Report for Numerical Analysis (ES341)

Construct the cubic spines for the collected data points to replace them
with continuous and smooth curves using MATLAB

FACULTY OF ENGINEERING SCIENCES

GHULAM ISHAQ KHAN INSTITUTE OF ENGINEERING SCIENCES


AND TECHNOLOGY

Submitted to:
Prof. Dr. Sirajul Haq

Submitted by:
Muhammad Omar Amin (2022411)

Page 1 of 4
I. MATLAB Code

% ES341 Assignment
% Cubic Spline Interpolation
% Muhammad Omar Amin (2022411)

% Hardcoded 100 data points


N = 100;
X = linspace(1, 100, N); % Evenly spaced x-values
F = [3.053, 4.823, 5.796, 6.549, 6.770, 7.390, 7.743, 8.142, 8.940, 9.203,
9.557, 10.000, 10.309, 11.018, 11.151, 11.726, 12.124, 12.169, 12.923,
13.098, 13.142, 13.761, 13.982, 14.071, 14.230, 14.470, 14.630, 14.870,
14.950, 15.320, 15.460, 15.830, 15.910, 16.339, 16.697, 17.013, 17.143,
17.412, 17.589, 17.856, 17.858, 18.705, 18.840, 18.928, 19.020, 19.286,
19.289, 19.464, 19.956, 20.090, 20.491, 20.895, 21.163, 21.343, 21.397,
21.402, 21.579, 21.587, 21.677, 21.732, 21.789, 21.852, 21.922, 21.944,
21.963, 21.987, 22.035, 22.061, 22.341, 22.832, 22.843, 22.857, 23.097,
23.099, 23.634, 23.895, 24.159, 24.968, 25.221, 25.488, 25.492, 25.758,
25.774, 26.018, 26.019, 26.283, 26.289, 26.550, 27.081, 27.345, 27.350,
27.366, 27.611, 27.612, 27.876, 27.881, 28.143, 28.408, 29.204, 29.470];

% Pre-allocate arrays for efficiency


h = zeros(N-1, 1); % Step sizes
A = zeros(N, N); % Tridiagonal matrix
B = zeros(N, 1); % RHS vector
Alpha = F'; % Column vector of f(x)

% Calculate step sizes


for j = 1:N-1
h(j) = X(j+1) - X(j);
end

% Construct matrix A for cubic spline system


A(1, 1) = 1; % Natural spline boundary condition
A(N, N) = 1; % Natural spline boundary condition
for i = 2:N-1
A(i, i-1:i+1) = [h(i-1), 2*(h(i-1) + h(i)), h(i)];
end

% Construct vector B
for i = 2:N-1
B(i) = (3/h(i)) * (Alpha(i+1) - Alpha(i)) - (3/h(i-1)) * (Alpha(i) -
Alpha(i-1));
end

% Solve for second derivatives (gamma)


gamma = A\B;

% Calculate coefficients for cubic spline


Beta = zeros(N-1, 1);
Delta = zeros(N-1, 1);
for i = 1:N-1
Beta(i) = (1/h(i)) * (Alpha(i+1) - Alpha(i)) - (h(i)/3) * (2*gamma(i) +
gamma(i+1));
Delta(i) = (1/(3*h(i))) * (gamma(i+1) - gamma(i));
end

% Plot original data points

Page 2 of 4
figure;
scatter(X, F, 40, 'r', 'filled', 'DisplayName', 'Data Points'); % Pretty
data points
hold on;

% Color gradient for spline segments


colors = parula(N-1);

% Plot cubic spline segments


for i = 1:N-1
% Generate fine grid for smooth spline plot
Y = linspace(X(i), X(i+1), 50);
S = Alpha(i) + Beta(i)*(Y - X(i)) + gamma(i)*(Y - X(i)).^2 +
Delta(i)*(Y - X(i)).^3;
plot(Y, S, 'Color', colors(i, :), 'LineWidth', 2); % Gradient color
end

% Finalize plot
title('Cubic Spline Interpolation', 'FontSize', 14, 'FontWeight', 'bold');
xlabel('X-axis', 'FontSize', 12);
ylabel('f(X)', 'FontSize', 12);
grid on;
set(gca, 'FontSize', 10, 'GridColor', [0.8, 0.8, 0.8], 'GridAlpha', 0.5);
legend('Data Points', 'Location', 'northwest');

hold off;

II. MATLAB Graph

Page 3 of 4
III. Conclusion

The cubic spline interpolation implemented in this assignment demonstrates a robust


numerical technique for constructing smooth and continuous curves through given data
points. The MATLAB code efficiently handles 100 data points, generating x -values evenly
spaced between 1 and 100, while ensuring that the corresponding f (x)values are random but
monotonically ordered. The algorithm constructs natural cubic splines, employing tridiagonal
matrix equations to compute the second derivatives and subsequently derive the coefficients
for each polynomial segment.

The generated cubic splines effectively approximate the data set with continuous and
differentiable curves, demonstrating the utility of this method in handling numerical problems
involving interpolation. The results confirm that the implemented code is accurate and
efficient, with the graph validating smoothness and continuity across all segments. This
analysis underscores the importance of careful algorithm implementation and aesthetic
presentation in numerical analysis tasks.

Page 4 of 4

You might also like