Project

Comparing the first order derivative using analytical and different numerical methods

Objective :

To compare the accuracy of different Numerical Difference methods with Analytical Solution.

Numerical Methods :

Numerical Methods are really helpful in analysing the physical behaviours using computations.

They provide flexibility , and complexity in solving for results.

As in numerical methods , we convert complex function into Algebraic (polynomial) equaition , so that we can easily quanitify the problem.

Easy to compare things.

Considered Four Methods :

1. Forward Difference Approximation.

`f^'=frac{f(x + dx) - f(x)}{dx}`

2. Backward Difference Approximation.

`f^' = frac{f(x) - f(x -dx)}{dx}`

3. Central Difference Approximation.

`f^' = frac{f(x+dx) - f(x-dx)}{2*dx}`

4. Central 4th order Approximation.

`f^' = frac{f(x-2*dx) - 8*f(x-dx)+8*f(x+dx)-f(x+2*dx)}{12*dx}`

Just imagine a tangent curve at the peak of curve of red curve , that our actual study.

But we study the secent lines represented in graph.

As can observe that central difference approximation is near to the model(more accurate).

Taylor Series Expansion :

`f(x) = f(a) +frac{ f'(a)}{1!}*(x-a)^1 + frac{f''(a)}{2!}*(x-a)^2 + frac{f'''(a)}{!}*(x_(i+1)-f(x_i))^3+...`

  • Centered at position 'a'.
  • Considered Evenly spaced grid on both sides

For Foward Difference , put x = `x_(i+1)`.

a = `x_i`.

`f(x_(i+1)) = f(x_i) + frac{f'(x_i)}{1!}*(x_(i+1) - x_i) + frac{f''(x_i)}{2!}*(x_(i+1)-x_i)^2+frac{f'''(x_i)}{3!}.(x_(i+1)-x_(i))^3+...` --------->1

`frac{f(x_(i+1)) - f(x_i)}{h} = f'(x_i) + O(h)`

For Backward Difference , put x = `x_(i-1)`.

a = `x_i`.

`f(x_(i-1)) = f(x_i) - frac{f'(x_i)}{1!}*(x_(i-1) - x_i) + frac{f''(x_i)}{2!}*(x_(i-1)-x_i)^2-frac{f'''(x_i)}{3!}.(x_(i-1)-x_i)^3+...` ----------->2

`frac{f(x_(i) -x_(i-1))}{h} = f'(x) +O(h)`

For Centeral Difference Approximation :

Subtract equation 2 from equation 1, we get

`f(x_(i+1)) - f(x_(i-1)) = 2*frac{f'(x_i)}{1!}*(h)^1+2*frac{f'''(x_i)}{3!}*(h)^3+...`

Divide by 2h on both sides

`frac{f(x_(i+1)) - f(x_(i-1))}{(2*h)} = frac{f'(x_i)}{1!}+frac{f'''(x_i)}{3!}*(h)^2+...`

Can observe that order of Error is : O(`h^2`)

For centeral 4th order Approximation :

This can be solved by 'method of unknown variables' .

This method use the neighbour values for to find approximate at the particular points.

Number of terms depends on the order of approximation (or) order of error to be made.

since , our investigation is for 4th order approximation , we need four neighboring points.

And point consideration depends on the flow , like forward , backward , central Numerical scheme depends on the physical behaviour of model.

`f'(x) = frac{A1*f(x-2h) + A2*f(x-h) + A3*f(x) + A4*f(x+h) + A5*f(x+2h)}{h}`

Eapand the terms using Taylor sereies , by comparing the left and right oreder terms , we get algebraic equaitons

Considered 5 equations , as 5 unknown variables

As you can see , calculated the unknown variables , keep in above equation , we get the required format.

ORDER OF ERROR -> O(h^4) -> Since we considered 5 equations , so the leading term will be O(h^4) , if that term not equal to zero .

If that term equal to zero , then move the next term in the flow.

Code Snippet :

%% clearing commands
close all ; clear all ; clc ;
% Analytical function
% f(x) = sin(x)/x^3;
x = pi/3;
dx = pi/40;
True_value = (x^3*(cos(x)) - sin(x)*3*x^2)/(x^6);
Forward_Difference = (dis_r(x+dx) - dis_r(x))/dx;
Backward_Difference = (dis_r(x) - dis_r(x-dx))/dx;
Central_Difference = (dis_r(x+dx) - dis_r(x-dx))/(2*dx);
Central_4th_order = (dis_r(x-2*dx) - 8*dis_r(x-dx) + 8*dis_r(x+dx) - dis_r(x+2*dx))/(12*dx);
% error
error_Forward_Difference = abs(True_value - Forward_Difference);
error_Backward_Difference = abs(True_value - Backward_Difference);
error_Central_Difference = abs(True_value - Central_Difference);
error_Central_4th_Difference = abs(True_value - Central_4th_order);

%% Bar plot
bar(1,error_Forward_Difference)
hold on
bar(2,error_Backward_Difference)
bar(3,error_Central_Difference)
bar(4,error_Central_4th_Difference)
legend('Forward Difference','Backward Difference','Central Difference','Central 4th order Approximation')
xlabel('Approximation Methods')
ylabel('Absolute errors')

Explanation of code:

1. At first used clearing commands , to clear the present data on the interface.

2. Assigned given data variables.

3. Calculated True value (analytical Method) , and Numerical Method(By respective relations).

4. And got the absolute error for all methods.

5. Plotted the data using bar command.

7. Labeled the plot.

Function Snippet :

function [z] = dis_r(x)
   z = sin(x)/x^3;
end

1. Used function to reduce the complexity in representing the equation.

RESULTS :

CONCLUSIONS:

1 . From the bar observed , as increase in the order of approximation order of accuracy increases and error decreases.

2. Forward difference approximation is more accurate than Backward difference approximation.

3. Central difference Approximation is more accurate than Forward and Backward Approximations.

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.

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.