Week 3.5 - Deriving 4th order approximation of a 2nd order derivative using Taylor Table method
Deriving Fourth-Order Approximations for Second-Order Derivative:
Let’s derive the following 4th-order approximations of the second-order derivative:
1. Central Difference:
The central difference approximation for the second-order derivative can be written as:
For a fourth-order accurate approximation, we'll use more points. The stencil will be , so the approximation becomes:
To determine the coefficients we expand in a Taylor series about x and solve for these coefficients.
2. Skewed Right-Sided Difference:
For a skewed right-sided difference, we use a different stencil. For fourth-order accuracy, we use five points:
Here, we need to solve for
3. Skewed Left-Sided Difference:
Similarly, for a skewed left-sided difference, we have:
Again, the coefficients are determined by expanding the function in a Taylor series and solving a system of equations.
Step-by-Step Process to Derive the Coefficients Using Matrix Inversion
Set up Taylor series expansions: Expand .
Match terms: To find the coefficients for fourth-order accuracy, match the derivatives up to the fourth derivative and solve the resulting linear system of equations.
Solve the system: Use matrix inversion to solve for the coefficients .
MATLAB Code for Deriving the Coefficients
Here is an example MATLAB code that solves for the coefficients of the fourth-order central difference and skewed schemes using matrix inversion:
clc;
clear;
% Symbolic variables
syms h f0 f1 f2 f3 f4 f_1 f_2 f_3 f_4;
% Taylor series expansion up to h^4
T_f0 = f0; % f(x)
T_f1 = f0 + h*diff(f0) + (h^2/2)*diff(f0,2) + (h^3/6)*diff(f0,3) + (h^4/24)*diff(f0,4); % f(x+h)
T_f2 = f0 + 2*h*diff(f0) + (2*h^2/2)*diff(f0,2) + (8*h^3/6)*diff(f0,3) + (16*h^4/24)*diff(f0,4); % f(x+2h)
% Similarly for f(x-2h), f(x-3h), etc. using symbolic expansion
% System of equations for central difference
% For central difference: f(x), f(x+h), f(x-h), f(x+2h), f(x-2h)
% To solve the system of equations for coefficients, you can set up a
% matrix inversion problem. The idea is to match the Taylor expansion terms
% and solve the linear system.
% Matrix inversion method for central difference coefficients
A = [1, 1, 1, 1, 1;
0, 1, 2, 3, 4;
0, 1^2, 2^2, 3^2, 4^2;
0, 1^3, 2^3, 3^3, 4^3;
0, 1^4, 2^4, 3^4, 4^4];
b = [0; 0; 2; 0; 0]; % Right-hand side for second derivative
coeff = A\b;
% Display the coefficients
disp('Fourth-order central difference coefficients:');
disp(coeff');
MATLAB Code for Comparing Derivatives and Plotting Errors
% MATLAB Code for comparing numerical schemes for second-order derivatives
clc;
clear;
% Define the function and its analytical second derivative
f = @(x) exp(x) .* cos(x); % Function
f_analytical = @(x) -2*exp(x) .* sin(x); % Exact second derivative
x = pi/4; % Test point
h_values = logspace(-4, -1, 100); % Range of h values
% Initialize error arrays
central_error = zeros(size(h_values));
right_skewed_error = zeros(size(h_values));
left_skewed_error = zeros(size(h_values));
% Loop through different step sizes h
for i = 1:length(h_values)
h = h_values(i);
% Central difference 4th order approximation
central_diff = (-f(x - 2*h) + 16*f(x - h) - 30*f(x) + 16*f(x + h) - f(x + 2*h)) / (12*h^2);
central_error(i) = abs(central_diff - f_analytical(x));
% Right-skewed difference 4th order approximation
right_skewed_diff = (35*f(x) - 104*f(x + h) + 114*f(x + 2*h) - 56*f(x + 3*h) + 11*f(x + 4*h)) / (12*h^2);
right_skewed_error(i) = abs(right_skewed_diff - f_analytical(x));
% Left-skewed difference 4th order approximation
left_skewed_diff = (35*f(x) - 104*f(x - h) + 114*f(x - 2*h) - 56*f(x - 3*h) + 11*f(x - 4*h)) / (12*h^2);
left_skewed_error(i) = abs(left_skewed_diff - f_analytical(x));
end
% Plot the errors
figure;
loglog(h_values, central_error, 'r-', 'LineWidth', 1.5); hold on;
loglog(h_values, right_skewed_error, 'g--', 'LineWidth', 1.5);
loglog(h_values, left_skewed_error, 'b:', 'LineWidth', 1.5);
xlabel('Step size (h)');
ylabel('Absolute Error');
legend('Central Difference', 'Right-Skewed Difference', 'Left-Skewed Difference', 'Location', 'Best');
title('Error Comparison for 2nd Order Derivative Approximations');
grid on;
Explanation of Skewed Schemes:
Skewed schemes are useful in situations where boundary conditions limit the availability of data on one side of a point. For example, if we are near the boundary of a domain, we may not have access to data points on both sides of a point, making a central difference scheme unusable. In such cases, skewed (left-sided or right-sided) schemes are essential as they allow us to compute the derivative using points only on one side of the evaluation point.
CONCLUSION: