All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Taylor Series The Taylor series of a function is an infinite sum of terms that are expressed in terms of the function's derivatives at a single point. f(x)=f(a)+f'(a)1!(x−a)+f''(a)2!(x−a)2+f'''(a)3!(x−a)3+...,�(�)=�(�)+�′(�)1!(�-�)+�′′(�)2!(�-�)2+�′′′(�)3!(�-�)3+...,…
Syed Saquib
updated on 14 Apr 2023
Taylor Series
The Taylor series of a function is an infinite sum of terms that are expressed in terms of the function's derivatives at a single point.
f(x)=f(a)+f'(a)1!(x−a)+f''(a)2!(x−a)2+f'''(a)3!(x−a)3+...,�(�)=�(�)+�′(�)1!(�-�)+�′′(�)2!(�-�)2+�′′′(�)3!(�-�)3+...,
For example, the below graph shows approximation at x=0, here we can see that as the number of terms increases the two curves becomes coincident.
The exponential function ex (in blue), and the sum of the first n + 1 terms of its Taylor series at 0 (in red).
Deriving the 4th order approximations of the second-order derivative
1) Skewed right-sided difference
The number of Nodes required for 4th order approximation of a second-order derivative can be given by the equation
N=P+Q�=�+� where
N� is the number of nodes
Q� is the order of derivative
P� is the order of approximation
So, here we need 6 nodes in order to find the 2nd order derivative with the 4th order of approximation.
Consider the value of ∂2y∂x2∂2�∂�2 with the 4th order of approximation is given by the equation
∂2y∂x2=af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+gf(i+5)∂2�∂�2=��(�)+��(�+1)+��(�+2)+��(�+3)+��(�+4)+��(�+5)
Now, using taylor series to find the values of discrete poits with repect to point i�
af(i)=af(i)��(�)=��(�)
bf(i+1)=bf(i)+bΔxf'(i)+bΔx22!f''(i)+bΔx33!f'''(i)+bΔx44!f''''(i)+bΔx55!f'''''(i)+bΔx66!f''''''(i)+...��(�+1)=��(�)+�Δ��′(�)+�Δ�22!�′′(�)+�Δ�33!�′′′(�)+�Δ�44!�′′′′(�)+�Δ�55!�′′′′′(�)+�Δ�66!�′′′′′′(�)+...
cf(i+2)=cf(i)+c(2Δx)f'(i)+c(2Δx)22!f''(i)+c(2Δx)33!f'''(i)+c(2Δx)44!f''''(i)+c(2Δx)55!f'''''(i)+c(2Δx)66!f''''''(i)+...��(�+2)=��(�)+�(2Δ�)�′(�)+�(2Δ�)22!�′′(�)+�(2Δ�)33!�′′′(�)+�(2Δ�)44!�′′′′(�)+�(2Δ�)55!�′′′′′(�)+�(2Δ�)66!�′′′′′′(�)+...
df(i+3)=df(i)+d(3Δx)f'(i)+d(3Δx)22!f''(i)+d(3Δx)33!f'''(i)+d(3Δx)44!f''''(i)+d(3Δx)55!f'''''(i)+d(3Δx)66!f''''''(i)+...��(�+3)=��(�)+�(3Δ�)�′(�)+�(3Δ�)22!�′′(�)+�(3Δ�)33!�′′′(�)+�(3Δ�)44!�′′′′(�)+�(3Δ�)55!�′′′′′(�)+�(3Δ�)66!�′′′′′′(�)+...
ef(i+4)=ef(i)+e(4Δx)f'(i)+e(4Δx)22!f''(i)+e(4Δx)33!f'''(i)+e(4Δx)44!f''''(i)+e(4Δx)55!f'''''(i)+e(4Δx)66!f''''''(i)+...��(�+4)=��(�)+�(4Δ�)�′(�)+�(4Δ�)22!�′′(�)+�(4Δ�)33!�′′′(�)+�(4Δ�)44!�′′′′(�)+�(4Δ�)55!�′′′′′(�)+�(4Δ�)66!�′′′′′′(�)+...
gf(i+5)=gf(i)+g(5Δx)f'(i)+g(5Δx)22!f''(i)+g(5Δx)33!f'''(i)+g(5Δx)44!f''''(i)+g(5Δx)55!f'''''(i)+g(5Δx)66!f''''''(i)+...��(�+5)=��(�)+�(5Δ�)�′(�)+�(5Δ�)22!�′′(�)+�(5Δ�)33!�′′′(�)+�(5Δ�)44!�′′′′(�)+�(5Δ�)55!�′′′′′(�)+�(5Δ�)66!�′′′′′′(�)+...
Adding the LHS and RHS of the above equations we get
af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+gf(i+5)=f(i)[a+b+c+d+e+g]+Δxf'(i)[b+2c+3d+4e+5g]+Δx2f''(i)[b2+4c2+9d2+16e2+25g2]+Δx3f'''(i)[b6+8c6+27d6+64e6+125g6]+Δx4f''''(i)[b24+16c24+81d24+256e24+625g24]+Δx5f'''''(i)[b120+32c120+243d120+1024e120+3125g120]��(�)+��(�+1)+��(�+2)+��(�+3)+��(�+4)+��(�+5)=�(�)[�+�+�+�+�+�]+Δ��′(�)[�+2�+3�+4�+5�]+Δ�2�′′(�)[�2+4�2+9�2+16�2+25�2]+Δ�3�′′′(�)[�6+8�6+27�6+64�6+125�6]+Δ�4�′′′′(�)[�24+16�24+81�24+256�24+625�24]+Δ�5�′′′′′(�)[�120+32�120+243�120+1024�120+3125�120]
In the above equations, we want only the coefficients of Δx2f''(i)Δ�2�′′(�) and we assume the coefficient to be one and for the remaining terms we assume the coefficients to be zero.
a+b+c+d+e+g=0�+�+�+�+�+�=0
b+2c+3d+4e+5g=0�+2�+3�+4�+5�=0
b2+4c2+9d2+16e2+25g2=1�2+4�2+9�2+16�2+25�2=1
b6+8c6+27d6+64e6+125g6=0�6+8�6+27�6+64�6+125�6=0
b24+16c24+81d24+256e24+625g24=0�24+16�24+81�24+256�24+625�24=0
b120+32c120+243d120+1024e120+3125g120=0�120+32�120+243�120+1024�120+3125�120=0
Taylor Table | ||||||||
Assumed Function | Taylor Series Expansion | |||||||
f(i)�(�) | Δf'(i)Δ�′(�) | Δx2f''(i)Δ�2�′′(�) | Δx3f'''(i)Δ�3�′′′(�) | Δx4f''''(i)Δ�4�′′′′(�) | Δx5f'''''(i)Δ�5�′′′′′(�) | Δx6f''''''(i)Δ�6�′′′′′′(�) | ||
Stencil
|
af(i)��(�) | a� | 00 | 00 | 00 | 00 | 00 | 00 |
bf(i+1)��(�+1) | b� | b� | b2�2 | b6�6 | b24�24 | b120�120 | b720�720 | |
cf(i+2)��(�+2) | c� | 2c2� | 4c24�2 | 8c68�6 | 16c2416�24 | 32c12032�120 | 64c72064�720 | |
df(i+3)��(�+3) | d� | 3d3� | 9d29�2 | 27d627�6 | 81d2481�24 | 243d120243�120 | 729d720729�720 | |
ef(i+4)��(�+4) | e� | 4e4� | 16e216�2 | 64e664�6 | 256e24256�24 | 1024e1201024�120 | (4096e)720(4096�)720 | |
gf(i+5)��(�+5) | g� | 5g5� | 25g225�2 | 125g6125�6 | 625g24625�24 | 3125g1203125�120 | 15625g72015625�720 | |
Sum of Coefficients | 0 | 0 | 1 | 0 | 0 | 0 | ? |
Writing the coefficients in the form of a matrix we get
⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣11111101234501242921622520168627664612560124162481242562462524011203212024312010241203125120⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣abcdeg⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦=⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣001000⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦[11111101234501242921622520168627664612560124162481242562462524011203212024312010241203125120][������]=[001000]
Solving using MATLAB to find the values of the coefficients
>> A=[1 1 1 1 1 1;0 1 2 3 4 5;0 1/2 4/2 9/2 16/2 25/2;0 1/6 8/6 27/6 64/6 125/6;0 1/24 16/24 81/24 256/24 625/24;0 1/120 32/120 243/120 1024/120 3125/120]
B=[0;0;1;0;0;0]
X=AB
A =
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0 1.0000 2.0000 3.0000 4.0000 5.0000
0 0.5000 2.0000 4.5000 8.0000 12.5000
0 0.1667 1.3333 4.5000 10.6667 20.8333
0 0.0417 0.6667 3.3750 10.6667 26.0417
0 0.0083 0.2667 2.0250 8.5333 26.0417
B =
0
0
1
0
0
0
X =
3.7500
-12.8333
17.8333
-13.0000
5.0833
-0.8333
Now we can write the equation using the Taylors Table as ∂2y∂x2=af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+gf(i+5)Δx2∂2�∂�2=��(�)+��(�+1)+��(�+2)+��(�+3)+��(�+4)+��(�+5)Δ�2
2) Skewed left-sided difference
The number of Nodes required for 4th order approximation of a second-order derivative can be given by the equation
N=P+Q�=�+� where
N� is the number of nodes
Q� is the order of derivative
P� is the order of approximation
So, here we need 6 nodes in order to find the 2nd order derivative with the 4th order of approximation.
Consider the value of ∂2y∂x2∂2�∂�2 with the 4th order of approximation is given by the equation
∂2y∂x2=af(i)+bf(i−1)+cf(i−2)+df(i−3)+ef(i−4)+gf(i−5)∂2�∂�2=��(�)+��(�-1)+��(�-2)+��(�-3)+��(�-4)+��(�-5)
Consider Taylor Table for left-sided difference
Taylor Table | ||||||||
Assumed Function | Taylor Series Expansion | |||||||
f(i)�(�) | Δxf'(i)Δ��′(�) | Δx2f''(i)Δ�2�′′(�) | Δx3f'''(i)Δ�3�′′′(�) | Δx4f''''(i)Δ�4�′′′′(�) | Δx5f'''''(i)Δ�5�′′′′′(�) | Δx6f''''''(i)Δ�6�′′′′′′(�) | ||
Stencil
|
af(i)��(�) | a� | 00 | 00 | 00 | 00 | 00 | 00 |
bf(i−1)��(�-1) | b� | −b-� | b2�2 | −b6-�6 | b24�24 | −b120-�120 | b720�720 | |
cf(i−2)��(�-2) | c� | −2c-2� | 4c24�2 | −8c6-8�6 | 16c2416�24 | −32c120-32�120 | 64c72064�720 | |
df(i−3)��(�-3) | d� | −3d-3� | 9d29�2 | −27d6-27�6 | 81d2481�24 | −243d120-243�120 | 729d720729�720 | |
ef(i−4)��(�-4) | e� | −4e-4� | 16e216�2 | −64e6-64�6 | 256e24256�24 | −1024e120-1024�120 | (4096e)720(4096�)720 | |
gf(i−5)��(�-5) | g� | −5g-5� | 25g225�2 | −125g6-125�6 | 625g24625�24 | −3125g120-3125�120 | 15625g72015625�720 | |
Sum of Coefficients | 0 | 0 | 1 | 0 | 0 | 0 | ? |
Writing the coefficients in the form of a matrix we get
⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣1111110−1−2−3−4−501242921622520−16−86−276−646−125601241624812425624625240−1120−32120−243120−1024120−3125120⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣abcdeg⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦=⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣001000⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦[1111110-1-2-3-4-501242921622520-16-86-276-646-125601241624812425624625240-1120-32120-243120-1024120-3125120][������]=[001000]
Solving using MATLAB to find the values of the coefficients
>> A=[1 1 1 1 1 1;0 -1 -2 -3 -4 -5;0 1/2 4/2 9/2 16/2 25/2;0 -1/6 -8/6 -27/6 -64/6 -125/6;0 1/24 16/24 81/24 256/24 625/24;0 -1/120 -32/120 -243/120 -1024/120 -3125/120]
B=[0;0;1;0;0;0]
X=AB
A =
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0 -1.0000 -2.0000 -3.0000 -4.0000 -5.0000
0 0.5000 2.0000 4.5000 8.0000 12.5000
0 -0.1667 -1.3333 -4.5000 -10.6667 -20.8333
0 0.0417 0.6667 3.3750 10.6667 26.0417
0 -0.0083 -0.2667 -2.0250 -8.5333 -26.0417
B =
0
0
1
0
0
0
X =
3.7500
-12.8333
17.8333
-13.0000
5.0833
-0.8333
Now we can write the equation using the Taylors Table as ∂2y∂x2=af(i)+bf(i−1)+cf(i−2)+df(i−3)+ef(i−4)+gf(i−5)Δx2∂2�∂�2=��(�)+��(�-1)+��(�-2)+��(�-3)+��(�-4)+��(�-5)Δ�2
3. Central Difference
The number of Nodes required for 4th order approximation of a second-order derivative can be given by the equation
N=P+Q−1�=�+�-1 where
N� is the number of nodes
Q� is the order of derivative
P� is the order of approximation
So, here we need 5 nodes in order to find the 2nd order derivative with the 4th order of approximation.
Consider the value of ∂2y∂x2∂2�∂�2 with the 4th order of approximation is given by the equation
∂2y∂x2=af(i−2)+bf(i−1)+cf(i)+df(i+1)+ef(i+2)∂2�∂�2=��(�-2)+��(�-1)+��(�)+��(�+1)+��(�+2)
Consider Taylor Table for central difference
Taylor Table | |||||||
Assumed Function | Taylor Series Expansion | ||||||
f(i)�(�) | Δxf'(i)Δ��′(�) | Δx2f''(i)Δ�2�′′(�) | Δx3f'''(i)Δ�3�′′′(�) | Δx4f''''(i)Δ�4�′′′′(�) | Δx5f'''''(i)Δ�5�′′′′′(�) | ||
Stencil
|
af(i−2)��(�-2) | a� | −2a-2� | 4a24�2 | −8a6-8�6 | 16a2416�24 | −32a120-32�120 |
bf(i−1)��(�-1) | b� | −b-� | b2�2 | −b6-�6 | b24�24 | −b120-�120 | |
cf(i)��(�) | c� | 00 | 00 | 00 | 00 | 00 | |
df(i+1)��(�+1) | d� | d� | d2�2 | d6�6 | d24�24 | −c120-�120 | |
ef(i+2)��(�+2) | e� | 2e2� | 4e24�2 | −8e6-8�6 | 16e2416�24 | 32e12032�120 | |
Sum of Coefficients | 0 | 0 | 1 | 0 | 0 | ? |
Writing the coefficients in the form of a matrix we get
⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣11111−2−1012421201242−86−1601686162412401241624−32120−11200112032120⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣abcde⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦=⎡⎢ ⎢ ⎢ ⎢ ⎢ ⎢⎣00100⎤⎥ ⎥ ⎥ ⎥ ⎥ ⎥⎦[11111-2-1012421201242-86-1601686162412401241624-32120-11200112032120][�����]=[00100]
Solving using MATLAB to find the values of the coefficients
>> A=[1, 1, 1, 1, 1;-2, -1, 0, 1, 2;4/2, 1/2, 0, 1/2, 4/2;-8/6, -1/6, 0, 1/6, 8/6;16/24, 1/24, 0, 1/24, 16/24]
B=[0;0;1;0;0]
X=AB
A =
1.0000 1.0000 1.0000 1.0000 1.0000
-2.0000 -1.0000 0 1.0000 2.0000
2.0000 0.5000 0 0.5000 2.0000
-1.3333 -0.1667 0 0.1667 1.3333
0.6667 0.0417 0 0.0417 0.6667
B =
0
0
1
0
0
X =
-0.0833
1.3333
-2.5000
1.3333
-0.0833
Now we can write the equation using the Taylors Table as ∂2y∂x2=af(i−2)+bf(i−1)+cf(i)+df(i+1)+ef(i+2)Δx2∂2�∂�2=��(�-2)+��(�-1)+��(�)+��(�+1)+��(�+2)Δ�2
Inputs
The input here is the given function, f(x)=excosx�(�)=��cos�
The assumed inputs include the value of x� and dx��, the point where the function f(x)�(�) is defined and the grid spacing value dx��. The value dx ranges form π4000�4000 to 1.5π1.5�. The analytical derivative of the function is found at the point x�.
%analytical fucntion=(e^x)cos(x);
%analytical derivative
%f(x)=(e^x)cos(x)
%f'(x)=(e^x)cos(x)-(e^x)sin(x)=e^x[cosx-sinx]
%f''(x)=(e^x)cos(x)-(e^x)sin(x)-[(e^x)sin(x)+(e^x)cos(x)]
%f''(x)=-2*e^x*sin(x)
%given inputs
%given inputs
x=pi/3; %derivative to be computed at x=pi/3
dx=linspace(pi/4000,1.5*pi,2000); %grid spacing dx=(pi*(4000-1.5))/(4000*2000)
%analytical derivative
analytical_derivative=-2*exp(x)*sin(x);
Finding the coefficients and designing the anonyms fucntion
An anonyms function f(x)=e^xcosx is defined to compute the value to f(x) upon taking the value of x as input.
The coefficients of the assumed equation of the 2nd order derivative for each differencing technique is calculated and stored.
%creating Anonymous Function
f=@(x) exp(x)*cos(x);
%Skewed right-sided(4th order approimation)
%Solving for the coefficients
A=[1 1 1 1 1 1;0 1 2 3 4 5;0 1/2 4/2 9/2 16/2 25/2;0 1/6 8/6 27/6 64/6 125/6;0 1/24 16/24 81/24 256/24 625/24;0 1/120 32/120 243/120 1024/120 3125/120];
B=[0;0;1;0;0;0];
x_r=AB;
%Skewed left-sided(4th order approimation)
%Solving for the coefficients
C=[1 1 1 1 1 1;0 -1 -2 -3 -4 -5;0 1/2 4/2 9/2 16/2 25/2;0 -1/6 -8/6 -27/6 -64/6 -125/6;0 1/24 16/24 81/24 256/24 625/24;0 -1/120 -32/120 -243/120 -1024/120 -3125/120];
D=[0;0;1;0;0;0];
x_l=CD;
%central differencing(4th order approimation)
%Solving for the coefficients
E=[1, 1, 1, 1, 1;-2, -1, 0, 1, 2;4/2, 1/2, 0, 1/2, 4/2;-8/6, -1/6, 0, 1/6, 8/6;16/24, 1/24, 0, 1/24, 16/24];
F=[0;0;1;0;0];
x_c=EF;
Numerical Derivative
A for loop is formulated to calculate the numerical derivative of the function using the assumed equation and found coefficients. For loops runs for every value of dx and the loop indexing is used to store the values at respective indexes.
The value of the numerical derivative is compared with the analytical derivative to find the absolute error.
for i=1:length(dx)
%numerical derivative
%Skewed right-sided(4th order approimation)
%f''(x)=af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+gf(i+5)
nd_right_difference(i)=(x_r(1)*f(x)+x_r(2)*f(x+dx(i))+x_r(3)*f(x+2*dx(i))+x_r(4)*f(x+3*dx(i))+x_r(5)*f(x+4*dx(i))+x_r(6)*f(x+5*dx(i)))/(dx(i)^2);
%Skewed left-sided(4th order approimation)
%f''(x)=af(i)+bf(i-1)+cf(i-2)+df(i-3)+ef(i-4)+gf(i-5)
nd_left_difference(i)=(x_l(1)*f(x)+x_l(2)*f(x-dx(i))+x_l(3)*f(x-2*dx(i))+x_l(4)*f(x-3*dx(i))+x_l(5)*f(x-4*dx(i))+x_l(6)*f(x-5*dx(i)))/(dx(i)^2);
%central differencing(4th order approimation)
%f''(x)=af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2)
nd_cent_difference(i)=(x_c(1)*f(x-2*dx(i))+x_c(2)*f(x-1*dx(i))+x_c(3)*f(x)+x_c(4)*f(x+dx(i))+x_c(5)*f(x+2*dx(i)))/(dx(i)^2);
%absolute error at x = pi/3
error_r(i)=abs(analytical_derivative-nd_right_difference(i));
error_l(i)=abs(analytical_derivative-nd_left_difference(i));
error_c(i)=abs(analytical_derivative-nd_cent_difference(i));
error=[error_r;error_l;error_c];
end
Plotting the function
To the error comparison for the three methods, we use the function 'loglog' with the 'absolute error' on the y-axis and the 'dx' on the x-axis, and we use the respective functions title, label and legend for annotating the plot
%Plotting the Absolute Error Curves
figure(1)
loglog(dx,error_r) %Plot both the axis on the log scale
grid on
hold on
loglog(dx,error_l)
loglog(dx,error_c)
ylabel('Absolute Error (Log Scale)')
xlabel('Grid Spacing (dx in Log Scale)')
legend('Right Differencing','Left Differencing','Central Differencing')
title('Absolute Error Comparison')
Complete Code
clear all
close all
clc
%analytical fucntion=(e^x)cos(x);
%analytical derivative
%f(x)=(e^x)cos(x)
%f'(x)=(e^x)cos(x)-(e^x)sin(x)=e^x[cosx-sinx]
%f''(x)=(e^x)cos(x)-(e^x)sin(x)-[(e^x)sin(x)+(e^x)cos(x)]
%f''(x)=-2*e^x*sin(x)
%given inputs
%given inputs
x=pi/3; %derivative to be computed at x=pi/3
dx=linspace(pi/4000,1.5*pi,2000); %grid spacing dx=(pi*(4000-1.5))/(4000*2000)
%analytical derivative
analytical_derivative=-2*exp(x)*sin(x);
%creating Anonymous Function
f=@(x) exp(x)*cos(x);
%Skewed right-sided(4th order approimation)
%Solving for the coefficients
A=[1 1 1 1 1 1;0 1 2 3 4 5;0 1/2 4/2 9/2 16/2 25/2;0 1/6 8/6 27/6 64/6 125/6;0 1/24 16/24 81/24 256/24 625/24;0 1/120 32/120 243/120 1024/120 3125/120];
B=[0;0;1;0;0;0];
x_r=AB;
%Skewed left-sided(4th order approimation)
%Solving for the coefficients
C=[1 1 1 1 1 1;0 -1 -2 -3 -4 -5;0 1/2 4/2 9/2 16/2 25/2;0 -1/6 -8/6 -27/6 -64/6 -125/6;0 1/24 16/24 81/24 256/24 625/24;0 -1/120 -32/120 -243/120 -1024/120 -3125/120];
D=[0;0;1;0;0;0];
x_l=CD;
%central differencing(4th order approimation)
%Solving for the coefficients
E=[1, 1, 1, 1, 1;-2, -1, 0, 1, 2;4/2, 1/2, 0, 1/2, 4/2;-8/6, -1/6, 0, 1/6, 8/6;16/24, 1/24, 0, 1/24, 16/24];
F=[0;0;1;0;0];
x_c=EF;
for i=1:length(dx)
%numerical derivative
%Skewed right-sided(4th order approimation)
%f''(x)=af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+gf(i+5)
nd_right_difference(i)=(x_r(1)*f(x)+x_r(2)*f(x+dx(i))+x_r(3)*f(x+2*dx(i))+x_r(4)*f(x+3*dx(i))+x_r(5)*f(x+4*dx(i))+x_r(6)*f(x+5*dx(i)))/(dx(i)^2);
%Skewed left-sided(4th order approimation)
%f''(x)=af(i)+bf(i-1)+cf(i-2)+df(i-3)+ef(i-4)+gf(i-5)
nd_left_difference(i)=(x_l(1)*f(x)+x_l(2)*f(x-dx(i))+x_l(3)*f(x-2*dx(i))+x_l(4)*f(x-3*dx(i))+x_l(5)*f(x-4*dx(i))+x_l(6)*f(x-5*dx(i)))/(dx(i)^2);
%central differencing(4th order approimation)
%f''(x)=af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2)
nd_cent_difference(i)=(x_c(1)*f(x-2*dx(i))+x_c(2)*f(x-1*dx(i))+x_c(3)*f(x)+x_c(4)*f(x+dx(i))+x_c(5)*f(x+2*dx(i)))/(dx(i)^2);
%absolute error at x = pi/3
error_r(i)=abs(analytical_derivative-nd_right_difference(i));
error_l(i)=abs(analytical_derivative-nd_left_difference(i));
error_c(i)=abs(analytical_derivative-nd_cent_difference(i));
error=[error_r;error_l;error_c];
end
%Plotting the Absolute Error Curves
figure(1)
loglog(dx,error_r) %Plot both the axis on the log scale
grid on
hold on
loglog(dx,error_l)
loglog(dx,error_c)
ylabel('Absolute Error (Log Scale)')
xlabel('Grid Spacing (dx in Log Scale)')
legend('Right Differencing','Left Differencing','Central Differencing')
title('Absolute Error Comparison')
Results
The comparison of absolute error for the left, right and central differencing scheme can be seen in the below graph. The graph shows that the central scheme is most accurate compared to the skewed differencing. The curves also show that when the value of dx is very small or large the approximating function becomes unstable. This instability is caused due to the combination of various errors such as roundoff error, residual error and truncation error.
From the below graph it is seen that the range of dx where the function is stable ranges from 0.1 to 0.01, beyond this range we see an abrupt rise or fall.
Q. Why skewed scheme is useful? What can a skewed scheme do that a central differencing scheme cannot?
A skewed scheme is useful when we have to predict using the boundary condition as we have only one direction to go. Hence the skewed scheme will be useful and we cannot use the central differencing scheme for such an application because it required information from both directions of the point.
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Week - 4 - 2D meshing for Plastic components
14 Feb 2024 04:24 PM IST
Week 3 - 2D meshing for Sheet metal
14 Feb 2024 04:10 PM IST
Project
AIM: To carry out a system-level simulation of an All-Terrain Vehicle (ATV). OBJECTIVES : To carry out a Simulation of ATV. To prepare a technical report explaining the model properties & comments on the results. THEORY : All-Terrain Vehicle (ATV) An All-Terrain Vehicle (ATV), also known as a light utility…
03 Jan 2024 10:45 AM IST
Project 1
Aim : Develop a double-acting actuator model using Simscape Multibody and Simscape components. Objective : The mechanical system of the cylinder needs to be built using Simscape Multibody library components/blocks, and the hydraulic system needs to be modeled using Simscape library physical components. Theory : The…
16 Oct 2023 03:59 PM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.