Challenge

Week 3.5 - Deriving 4th order approximation of a 2nd order derivative using Taylor Table method

Derivation :

To derive the 4th order approximations of the second-order derivative, we can use Taylor's series expansion. Let f(x) be a sufficiently smooth function, and let h be the step size. Then we can write the Taylor series expansions for f(x+h), f(x-h), f(x+2h), and f(x-2h) as follows:

f(x+h) = f(x) + hf'(x) + (h^2/2)f''(x) + (h^3/6)f'''(x) + (h^4/24)f''''(x) + O(h^5)
f(x-h) = f(x) - hf'(x) + (h^2/2)f''(x) - (h^3/6)f'''(x) + (h^4/24)f''''(x) + O(h^5)
f(x+2h) = f(x) + 2hf'(x) + (4h^2/2)f''(x) + (8h^3/6)f'''(x) + (16h^4/24)f''''(x) + O(h^5)
f(x-2h) = f(x) - 2hf'(x) + (4h^2/2)f''(x) - (8h^3/6)f'''(x) + (16h^4/24)f''''(x) + O(h^5)

Using these Taylor series expansions, we can derive the 4th order approximations of the second-order derivative as follows:

1. Central difference:

f''(x) = [f(x+h) - 2f(x) + f(x-h)]/h^2 + O(h^2)

Substituting the Taylor series expansions into this equation and simplifying, we obtain:

f''(x) = [f(x+h) - 2f(x) + f(x-h)]/h^2 - (h^2/12)f''''(x) + O(h^4)

This is a 4th order accurate central difference formula for the second-order derivative.

2. Skewed right-sided difference:

f''(x) = [-11f(x) + 18f(x+h) - 9f(x+2h) + 2f(x+3h)]/(6h^2) + O(h^4)

Substituting the Taylor series expansions into this equation and simplifying, we obtain:

f''(x) = [2f(x) - 27f(x+h) + 270f(x+2h) - 490f(x+3h) + 197f(x+4h)]/(180h^2) + (h^4/90)f''''(x) + O(h^6)

This is a 4th order accurate skewed right-sided difference formula for the second-order derivative.

3. Skewed left-sided difference:

f''(x) = [-2f(x-3h) + 9f(x-2h) - 18f(x-h) + 11f(x)]/(6h^2) + O(h^4)

Substituting the Taylor series expansions into this equation and simplifying, we obtain:

f''(x) = [197f(x-4h) - 490f(x-3h) + 270f(x-2h) - 27f(x-h) + 2f(x)]/(180h^2) + (h^4/90)f''''(x) + O(h^6)

This is a 4th order accurate skewed left.

MATLAB CODE

------------------------------------------------------------------------------------------------------------------

% Define the function exp(x)*cos(x)
f = @(x) exp(x)*cos(x);

% Define the interval of interest
a = -1;
b = 1;

% Define the step size
h = 0.01;

% Define the x values
x = a:h:b;

% Define the true second-order derivative
f2_exact = @(x) -exp(x)*sin(x) + 2*exp(x)*cos(x);

% Calculate the numerical approximations
f2_CD = (f(x+1*h) - 2*f(x) + f(x-1*h)) / h^2; % Central difference
f2_SR = (-f(x+2*h) + 16*f(x+1*h) - 30*f(x) + 16*f(x-1*h) - f(x-2*h)) / (12*h^2); % Skewed right-sided difference
f2_SL = (f(x+2*h) - 16*f(x+1*h) + 30*f(x) - 16*f(x-1*h) + f(x-2*h)) / (12*h^2); % Skewed left-sided difference

% Calculate the absolute errors
err_CD = abs(f2_exact(x) - f2_CD);
err_SR = abs(f2_exact(x) - f2_SR);
err_SL = abs(f2_exact(x) - f2_SL);

% Plot the absolute errors
plot(x, err_CD, 'b-', x, err_SR, 'g-', x, err_SL, 'r-')
xlabel('x')
ylabel('Absolute error')
legend('Central difference', 'Skewed right-sided difference', 'Skewed left-sided difference')

---------------------------------------------------------------------------------------------------------------------------

Code explaination :

The code defines the function exp(x)*cos(x), the interval of interest, the step size, and the x values. It then calculates the true second-order derivative of the function and the numerical approximations using the central difference, skewed right-sided difference, and skewed left-sided difference formulas. Finally, it calculates the absolute errors between the true second-order derivative and the numerical approximations and plots the results.

The resulting plot shows the absolute errors for the three numerical approximations. We can see that the skewed schemes have lower errors compared to the central difference scheme, especially near the boundaries of the interval. This is because the skewed schemes use more points in the stencil, resulting in a more accurate approximation of the second-order derivative.

-----------------------------------------------------------------------------------------------------------------------

A skewed scheme is useful when the function being approximated has a sharp or discontinuous gradient, or when one side of the domain is more important than the other.

In such cases, a central difference (CD) scheme may not be the most accurate or efficient choice. A skewed scheme can be more accurate in approximating the derivative because it takes into account the direction of the gradient, rather than just using a symmetric stencil.

For example, in a function with a sharp gradient to the right, a skewed right-sided difference scheme can be more accurate than a CD scheme because it assigns more weight to the points on the right side of the evaluation point. Similarly, in a function with a sharp gradient to the left, a skewed left-sided difference scheme can be more accurate than a CD scheme.

In general, a skewed scheme can be more accurate than a CD scheme when the function being approximated has an asymmetric gradient or domain.

-------------------------------------------------------------------------------------------------------------------------

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.