0% found this document useful (0 votes)
41 views

Take Home Final Exam (Problem 2)

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

Take Home Final Exam (Problem 2)

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

Take home Final Exam

Numerical Solution of PDEs (MTH 556–01)


Spring 2024
Name: Rana Dey
OPTION 2 (Problem 2, 3, 4)

Question 2

𝜋𝑑
We want to interpolate the function 𝑓(𝑥) = 2𝑥 2 + 𝑥 − 3𝑥 3 , 0 ≤ 𝑥 ≤ 1. Take ℎ = √ with
𝛽𝑁
𝑑 = 𝜋, 𝛽 = 1.
Use Akima interpolation and Sinc interpolation for the given function for N = 10, 20, 40, 80, 160,
320, 640. Plot the graph of the function and for both Akima and Sinc for each N. You should have
seven plots. Also make an error table like Table 1 on the reference paper for both interpolations.
MATLAB Script for Sinc Interpolation
% Define the function to be interpolated
function_value = @(x) 2*x.^2 + x - 3*x.^3;

% Define the parameters


d = 1.0;
beta = 1.0;
N_values = [10, 20, 40, 80, 160, 320, 640];

% Generate the time vector for the original function


t_original = 0:0.01:1; % High-resolution time vector for plotting the original
function
f_original = function_value(t_original);

% Plot all graphs


for i = 1:length(N_values)
N = N_values(i);
% Calculate the increment
h = sqrt((pi * d) / (beta * N));

% Generate the time vector for interpolation


t_interpolate = 0:h:1;

% Generate the original function


f_original_interpolate = function_value(t_interpolate);

% Perform Sinc interpolation


y = zeros(size(t_original));
for j = 1:length(t_original)
t_diff = t_interpolate - t_original(j);
sinc_interp = zeros(size(t_diff));
for k = 1:length(t_diff)
if t_diff(k) == 0
sinc_interp(k) = 1; % Avoid division by zero
else
sinc_interp(k) = sin(pi * t_diff(k) / h) / (pi * t_diff(k) / h);
end
end
y(j) = sum(f_original_interpolate .* sinc_interp);
end

% Plot the original and interpolated functions


figure;
plot(t_original, f_original, 'b', 'LineWidth', 2); hold on;
plot(t_original, y, 'r--', 'LineWidth', 1.5);
legend('Original Function', 'Interpolated Function');
xlabel('x');
ylabel('f(x)');
title(['Sinc Interpolation for N = ' num2str(N)]);
grid on;
end
Plots
MATLAB Script for Akima Interpolation
% Define the function to interpolate
f = @(x) 2*x.^2 + x - 3*x.^3; % Function f(x) = 2x^2 + x - 3x^3

% Define the range over which to interpolate


x_min = 0;
x_max = 1;

% Generate x values for the original function


x_original = (x_min:0.01:x_max);

% Compute the y values of the function for original x values


y_original = f(x_original);

% Define the values of N


Ns = [10, 20, 40, 80, 160, 320, 640]; % Different numbers of interpolation points

% Plot for each N


for i = 1:length(Ns)
N = Ns(i);

% Generate equidistant x values for interpolation


x_interpolation = linspace(x_min, x_max, N); % Interpolation points

% Compute the y values of the function for interpolation points


y_interpolation = f(x_interpolation);

% Akima interpolation
yi = zeros(size(x_interpolation));

for j = 1:length(x_interpolation)
% Find the nearest indices in x for x_interpolation(j)
[~, idx] = min(abs(x_original - x_interpolation(j)));

% Ensure the index is within bounds


if idx < 3
% Use linear interpolation near the left boundary
yi(j) = interp1(x_original(1:4), y_original(1:4), x_interpolation(j),
'linear');
elseif idx > length(x_original) - 3
% Use linear interpolation near the right boundary
yi(j) = interp1(x_original(end-3:end), y_original(end-3:end),
x_interpolation(j), 'linear');
else
% Compute Akima coefficients
h1 = x_original(idx) - x_original(idx - 1);
h2 = x_original(idx + 1) - x_original(idx);
h3 = x_original(idx + 2) - x_original(idx + 1);
h4 = x_original(min(idx + 3, length(x_original))) - x_original(idx + 2);
m1 = (y_original(idx) - y_original(idx - 1)) / h1;
m2 = (y_original(idx + 1) - y_original(idx)) / h2;
m3 = (y_original(idx + 2) - y_original(idx + 1)) / h3;
m4 = (y_original(min(idx + 3, length(x_original))) - y_original(idx + 2))
/ h4;
% Compute polynomial coefficients
a = y_original(idx);
b = m2 + (1 - (h2^2 / (h1 * h3))) * ((m3 - m2) - (1 - (h1^2 / (h2 * h3)))
* (m2 - m1));
c = (3*(m3 - m2) - 2*b - (1 - (h1^2 / (h2 * h3)))* (m2 - m1)) / h2;
d = (2*(m2 - m3) + b + (1 - (h2^2 / (h1 * h3)))* (m3 - m2)) / h2^2;

% Evaluate polynomial at x_interpolation(j)


dx = x_interpolation(j) - x_original(idx);
yi(j) = a + b*dx + c*dx^2 + d*dx^3;
end
end

% Plot the interpolated function


figure;
plot(x_interpolation, yi, 'r--', 'LineWidth', 2);
hold on;

% Plot the original function


plot(x_original, y_original, 'b-', 'LineWidth', 0.5);
hold off;

xlabel('x')
ylabel('f(x)')
title(['Akima Interpolation for N = ', num2str(N)])
legend('Interpolated function', 'Original function')
end
Plot

You might also like