Discretization basics
- AIM:
- To write a program in MATLAB that compute the first, second and fourth order approximations of the first derivative against the analytical or exact derivative.
- OBJECTIVE:
- To Compute the derivative both numerically and analytically.
- Plot a bar graph that compares the first, second and fourth order error.
- BODY OF THE CONTENT:
- We want to write program for to compute the first, second and fourth order approximations of the first derivative against the analytical or exact derivative.
- We want to compute 1st, 2nd and 4th order approximation of first order derivative.
- Now we discretise 1st, 2nd and 4th order approximation of first order derivative for programming prospective.
FIRST ORDER DERIVATIVE:
We used the “Taylor Series” to discretise the approximations.
- First Order Approximation:
Forward differencing
`f(x+dx)=f(x)+(f^' (x))/1! dx+(f^'' (x))/2! dx+(f^''' (x))/3! dx`
`f^' (x) or df(x)/dx=( f(x+dx)-f(x))/dx …… equation 1
- Second Order Approximation:
Central differencing
`f(x+dx)=f(x)+(f^' (x))/1! dx+(f^'' (x))/2! dx+(f^''' (x))/3! dx`
`f(x-dx)=f(x)-(f^' (x))/1! dx+(f^'' (x))/2! dx-(f^''' (x))/3! dx`
`2(f^' (x) or df(x)/dx)=( f(x+dx)-f(x-dx))/dx`
`f^' (x) or df(x)/dx=( f(x+dx)-f(x-dx))/2dx` …… equation 2
- Fourth Order Approximation:
Central differencing
`f^' (x) or df(x)/dx=(f(x-2dx)-8*f(x-dx)+8*f(x+dx)-f(x+2dx))/(12*dx) `
…… equation 3
When the discretization done, we used it in the programming for the compute the derivative.
Program Syntax:
clear all
close all
clc
% analytical function = sin(x)/x^3;
% analytical derivative
%f'(x) = (x^3*(cos(x))-sin(x)*3*x^2)/x^6;
x = pi/3;
analytical_derivative = (x^3*(cos(x))-sin(x)*3*x^2)/(x^6);
% numerical derivative
% first order derivative
% First order forward differencing = (f(x+dx)-f(x))/(dx);
% Second order central differencing = ((f(x+dx))-(f(x-dx)))/(2*dx);
% Fourth order central differencing =(f(x-2dx)-8*f(x-dx)+8*f(x+dx)-f(x+2dx))/(12*dx);
dx =pi/15
% First order approximatiom
first_order_forward_differencing= (sin(x+dx)/(x+dx)^3 - sin(x)/x^3)/(dx);
% Second order approximation
second_order_central_differencing= ((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx);
% fourth order approximatiom
fourth_order_central_differencing = ((sin(x-2*dx)/(x-2*dx)^3)-8*(sin(x-dx)/(x-dx)^3)+8*(sin(x+dx)/(x+dx)^3)-(sin(x+2*dx)/(x+2*dx)^3))/(12*dx);
error_first_order = abs(first_order_forward_differencing - analytical_derivative)
error_second_order = abs(second_order_central_differencing - analytical_derivative)
error_fourth_order = abs(fourth_order_central_differencing - analytical_derivative)
bar(1,error_first_order)
hold on
bar(2,error_second_order)
hold on
bar(3,error_fourth_order)
legend('error_first_order','error_second_order','error_fourth_order')
Syntax Explanation:
- We want to compute the derivative both analytically and numerically.
- Our computing function is and it is at line no. 4.
- As we know from the given data compute the first order derivation at and it is represented in line no. 7.
- First, we compute the function analytically and it is described at line no. 8 and we get below output screenshot for analytical derivative
- For the basic understanding of computing function numerically, we used discretising method for the 1st, 2nd and 4th order approximation of first order derivative and it is described from line no. 9 to 13.
- And we have already above discretised the equation for 1st, 2nd and 4th order approximation of first order derivative.
- We define the step size (dx) for the computing the function numerically in line no. 14 for first order derivative.
- Now we have to compute function numerically for 1st, 2nd and 4th order approximations for the first order derivative using the equation no…1, 2 & 3.
- And that computing function numerically for 1st, 2nd and 4th order approximations had been shown in line no. 16, 18 & 20 respectively.
- When we computed the function numerically, we got the values for 1st, 2nd and 4th order approximations and please find the below output screenshot.
- First of all, we define the error parameter, Error is defined as the difference between the absolute value and calculated value.
- After that we want to get the error for 1st, 2nd and 4th order approximation, we calculate the absolute difference in numerical and exact derivative at x = pi/3 in line no. 21, 22 & 23 respectively and we get the below output screenshot of the error.
- When we got the error for the 1st, 2nd and 4th order approximation, we plot the bar that shows the error in line 24 to 28.
- After that we include the legend for understand the bar graph.
- Please find the below bar graph output screenshot.
4.Conclusion:
- Hence, we had written a program to compute the derivative both numerically and analytically for the 1st, 2nd and 4th order approximation for first order derivative.
- Also, we had calculated the error for absolute difference in numerical and exact derivative.
- Plot a bar graph that shows the error.