Challenge

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

Derivating fourth order approximation using second order derivative:

1. Central Difference Method:

Since we’re deriving using central difference; our numerical stencil will look like this,

Here, we’re computing the 4th order approximation at point (i), where we take information from the four-point surrounding (i); i.e. we’re going to take two points on the left of (i) and two points on the right of (i). Let those points be (i-2), (i-1), (i+1), and (i+2) and the distance between each of them is (∆x).">(Δx).(∆x). Our numerical stencil will like as shown in the above figure.

Based on the above stencil our hypothesis equation will be:-

`(partial^2u)/(partialx^2)=af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2)`--------->(1)

To find the values of coefficient(a,b,c,d,e),">(a,b,c,d,e),(a,b,c,d,e),we’re going to use the Taylor table method. Writing Taylor series expansion for each term of equation (1) as:-

a) f(i-2)=af(i)-(af'(i)(2`Delta`x))/1!+(af''(i)(2`Delta`x)^2)/2!-(af'''(i)(2`Delta`x)^3)/3!+

(af''''(i)(2`Delta`x)^4)/4!-(af'''''(2`Delta`x)^5)/5!+(af''''''(2`Delta`x)^6)/6!+-------------

b) f(i-1)=bf(i)-(bf'(i)(`Delta`x))/1!+(bf''(i)(`Delta`x)^2)/2!-(bf''(i)(`Delta`x)^3)/3!+

(bf''''(i)(`Delta`x)^4)/4!-(bf'''''(i)(`Delta`x)^5)/5!+--------------

c) f(i)=cf(i)

d) f(i+1)=df(i)+(df'(i)(`Delta`x))/1!+(df''(i)(`Delta`x)^2)2!+(df'''(i)(`Delta`x)^3)3!+

(df''''(i)(`Delta`x)^4)4!+(df'''''(i)(`Delta`x)^5)5!+------------

e) f(i+2)=ef(i)+(ef'(i)(2`Delta`x))/1!+(ef''(i)(2`Delta`x)^2)/2!+(ef'''(i)(2`Delta`x)^3)/3!+

(ef''''(i)(2`Delta`x)^4)/4!+(ef'''''(i)(2`Delta`x)^5)/5!+-----arranging

By arranagingTaylorbove taylor series expansion in the Taylor table form:-

f(i) f'(i)(`Delta`x) f''(i)(`Delta`x)^2 f'''(i)(`Delta`x)^3 f''''(i)(`Delta`x)^4 f'''''(i)(`Delta`x)^5
f(i-2) a -2a 4a/2 -8a/6 16a/24 -32a/120
f(i-1) b -b b/2 -b/6 b/24 -b/120
f(i) c 0 0 0 0 0
f(i+1) d d d/2 d/6 d/24 d/120
f(i+2) e 2e 4e/2 8e/6 16e/24 32e/120

`Sigma`f(i-2)+f(i-1)+f(i)+f(i+1)+f(i+2)=`(partial^2u)/(partialx^2)`

0 0 1 0 0 ?

Here we have five unknowns, so, we have to solve five equations and get values of coefficients (a,b,c,d,e).We are going to form these equations by adding terms for the individual columns. The summation of coefficients of `f''(i) is equal to 1, and f(i),f′(i),f'''(i),f''''(i),f'''''(i) are equal to 0; because we want to calculate the second-order derivative,f′′(i).

Therefore, the five equations are:

a+b+c+d+e=0 ……….. (I)

−2a−b+0+d+2e=0……….. (II)

2a+b/2+0+d/2+2e=1 ……….. (III)

−8a/6−b/6+0+d/6+8e/6=0 ……….. (IV)

16a/24+b/24+0+d/24+16e/24=0 ……….. (V)

To get the solution to the above equations, we have to solve the above equations in matrix form; AX=B.

After solving the above matrix, we get the values of all the efficients as:

a=−0.083333

b=1.333333

c=-2.500000

d=1.333333

e=−0.083333

Here, we can easily notice that a=e and b=d.

Now, on dividing both sides of my hypothesis equation (1) by `Deltax^2`; we get:

`(partial^2u)/(partialx^2)=(af(i-2)+bf(i-1)+cf(i)+df(i+1)+cf(i+2))/(partialx^2)`

After putting the values of all the coefficients (a,b,c,d,e)n the above equation, we will get the fourth-order central difference approximation for the second-order derivative.

`(partial^2u)/(partialx^2)=((-0.0833)*f(i-1)+(1.333)f(i-1)+(-2.5)f(i)+(1.333)f(i+1)+(-0.0833)f(i+2))/(partialx^2)`

Now, from our Taylor table:

`(partial^2u)/(partialx^2)=((a+b+c+d+e)f(i)+(-2a-a+0+d+2e)f'(i)Deltax+(2a+1/2b+1/2d+2e)f''(i)Deltax^2+(-8/6a-1/6b+0+1/6d+8/6e)f'''(i)Deltax^3+(16/24a+1/24b+0+1/24d+16/24e)f''''(i)Deltax^4+(-32/120a-1/120b+0+1/120d+32/120e)f'''''(i)Deltax^ 5)`

To get second-order derivative the coefficient of f′′(i)">f′′(i) has to be 1. And as we want approximation for the second-order derivative, so, the term will have to ignore or set to ‘0’ in the above equation. On substituting the values of (I), (II), (IV), and (V) in the above equation; we’ll get:

`(partial^2u)/(partialx^2)=(2a+1/2b+0+1/2d+2e)f''(i)Deltax^2+(64/240a+1/240b+0+1/240d+64/240e)f''''''(i)Deltax^6`

On dividing the above equation by Deltax^2, we get:

`(partial^2u)/(partialx^2)=(2a+1/2b+0+1/2d+2e)f''(i)+(64/240a+1/240b+0+1/240d+64/240e)f''''''Deltax^4`

In the above equation, the term Deltax^4 denotes the truncation error. Here, we can see that the order of the truncation error; [O(Deltax^4)] is ‘4’. Since we know that the order of approximation is defined by the truncation error term. Therefore, we can say that our central difference scheme is 4th order accurate.

After putting the value of (a,b,c,d,e)">(a,b,c,d,e) in the above equation, the final equation looks like this:

`(partial^2u)/(partialx^2)=(2(-0.083)+1.33/2+1.33/2+2(0.83))f''(i)+(64/240(-0.083)+1.33/240+1.33/240+64/240(0.083))f''''''(i)Deltax^4`

2. Using Skewed Right-Sided Difference:

To derive 4th order skewed right-sided difference at point (i) of function. Since we’re using the skewed right-sided method, here we have to define five points on the right of (i). Let those points be (i+1), (i+2), (i+3), (i+4), and (i+5); with a spacing of `Deltax` each.

The numerical stencil will look like this:

According to the above stencil, our hypothesis equation will be:

`(partial^2u)/(partialx^2)=af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+gf(i+5)`

In order to find the values of coefficients(a,b,c,d,e,g),">(a,b,c,d,e,g), we’re going to use the Taylor table method.

Writing Taylor series expansion for each term of equation (1) as:-

a) f(i)=af(i)

b)`f(i+1)=bf(i)+(bf'(i)Deltax)/1!+(bf''(i)Deltax^2)/2!+(bf'''(i)Deltax^2)/3!+(bf''''(i)Deltax^4)/4!+(bf'''''(i)Deltax^5)/5!+--------`

c) `f(i+2)=cf(i)+(cf'(i)(2Deltax))/1!+(cf''(i)(2Deltax)^2)/2!+(cf'''(i)(2Deltax)^3)/3!+(cf''''(i)(2Deltax)^4)/4!+(cf'''''(i)(2Deltax)^5)/5!---------`

d) `f(i+2)=df(i)+(df'(i)(3Deltax))/1!+(df''(i)(3Deltax)^2)/2!+(df'''(i)(3Deltax)^3)/3!+(df''''(i)(3Deltax)^4)/4!+(df'''''(i)(3Deltax)^5)/5!---------`

e) `f(i+4)=ef(i)+(ef'(i)(4Deltax))/1!+(ef''(i)(4Deltax)^2)/2!+(ef'''(i)(4Deltax)^3)/3!+(ef''''(i)(4Deltax)^4)/4!+(ef'''''(i)(4Deltax)^5)/5!+----------`

By arranging the above Taylor series expthe mansions in Taylor table; we get:-

f(i) `Deltaxf'(i)` `Deltax^2f''(i)` `Deltax^3f'''(i)` `Deltax^4f''''(i)` `Deltax^5f'''''(i)`
af(i) a 0 0 0 0 0
bf(i+1) b b b/2 b/6 b/24 b/120
cf(i+2) c 2c 4c/2 8c/6 16c/24 32c/120
df(i+3) d 3d 9d/2 27d/6 81d/24 243d/120
ef(i+4) e 4e 16e/2 64e/6 256e/24 1024e/120
gf(i+5) g 5g 25g/2 125g/6 625g/24 3125g/120
`Sigmaaf(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+gf(i+5)=(partial^2u)/(partialx^2)` 0 0 1 0 0 0

Only for the first do six terms, we assume our hypothesis. That’s why we can’t say about the last term.

Here we have six unknowns, so, we have to solve six equations and get values of coefficients (a,b,c,d,e,g).">(a,b,c,d,e,g). We are going to form these equations by adding terms for the individual columns. The summation of coefficients of f′′(i)">f′′(i) is equal to 1, and f(i),f′(i),f′′(i),f′′′(i),f′′′′(i),f′′′′′(i)">f(i),f′(i),f′′(i),f′′′(i),f′′′′(i),f′′′′′(i) is equal to 0; because we want to calculate the second-order derivative, f′′(i).">f''(i).

a+b+c+d+e+g=0 ……….. (I)

0+b+2c+3d+4e+5g=0 ……….. (II)

0+b/2+2c+9d/2+8e+25g/2=1 ……….. (III)

0+b/6+8c/6+27d/6+64e/6+125g/6=0 ……….. (IV)

0+b/24+16c/24+81d/24+256e/24+625g/24=0 ……….. (V)

To get the solution to the above equations, we have to solve the above equations in matrix form; AX=B.

After solving the above matrix, we get the values of all the coefficients as:-

a=3.75000

b=-12.83333

c=17.83333

d=-13.00000

e=5.08333

g=-0.83333

Now, dividing both side of my hypothesis equation (1) by `Deltax^2`; we get:

`(partial^2u)/(partialx^2)=(af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+gf(i+5))/(Deltax^2)`

After putting the values of all the coefficients (a,b,c,d,e,g)">(a,b,c,d,e,g) in the above equation, we will get the fourth-order skewed right-sided difference approximation for second-order derivative

`(partial^ 2 u)/(partial x^2) =( ( 3.75000 ) f ( i ) + ( − 12.83333 ) f ( i + 1 ) + ( 17.83333 ) f ( i + 2 ) + ( − 13.00000 ) f ( i + 3 ) + ( 5.083333 ) f ( i + 4 ) + ( − 0.83333 ) f ( i + 5 ))/( Deltax^2) )`

Now, from our Taylor table:-

`(partial^2u)/( partialx^2) =( ( a + b + c + d + e + g ) f ( i ) + ( 0 + b + 2 c + 3 d + 4 e + 5 g ) f ′ ( i ) Deltax + ( 0 + b /2 + 4 c/ 2 + 9 d/ 2 + 16 e/ 2 + 25 g/ 2 ) f ′ ' ( i ) Deltax ^2 + ( 0 + b /6 + 8 c/ 6 + 27 d/ 6 + 64 e/ 6 + 125 g /6 ) f ′ ' ' ( i ) Deltax ^3 + ( 0 + b /24 + 16 c /24 + 81 d /24 + 256 e /24 + 625 g /24 ) f ′ ' ' ' ( i ) Delta x ^4 + ( 0 + b /120 + 32 c /120 + 243 d /120 + 1024 e/ 120 + 3125 g /120 ) f ′ ' ' ' ' ( i ) Deltax^5)/(Deltax^2)`

To get second-order derivative the coefficient of f′′(i)">f′′(i) has to be 1. And as we want approximation for the second-order derivative, so, the terms f(i),f′(i),f′′′(i),f′′′′(i),f′′′′′(i)">f(i),f′(i),f′′′(i),f′′′′(i),f′′′′′(i) will have to ignore or set to '0' in the above equation. On substituting the values of (I), (II), (IV), (V), and (VI) in the above equation; we’ll get:

`(partial ^2 u)/(partial x^ 2) = ( 0 + b /2 + 4 c/ 2 + 9 d/ 2 + 16 e/ 2 + 25 g/ 2 ) f ′ ' ( i ) Deltax ^2 + ( 0 + b /720 + 64 c /720 + 729 d /720 + 4096 e /720 + 15625 g /720 ) f ′ ' ' ' ' ' ( i ) Deltax ^6`

On dividing the above equation by `Deltax^2`, we get:-

`(partial^ 2 u)/(partial x ^2) = ( b/ 2 + 2 c + 9 d/ 2 + 8 e + 25 g/ 2 ) f ′ ' ( i ) + ( b/ 720 + 64 c/ 720 + 729 d /720 + 4096 e/ 720 + 15625 g/ 720 ) f ′ ' ' ' ' ' ( i ) Deltax ^4`

`s we discussed earlier also, the `Deltax^4` denotes the truncation error. Here we can see that order of the truncation error; [O(∆x4)]">[O(`Deltax^4)] is '4'. `Since, we know that the order of approximation is defined by the truncation error term. Therefore, we can say that our skewed right-sided difference scheme is of 4th order accurate.

After putting the values of (a,b,c,d,e,g)">(a,b,c,d,e,g)in the above equation, the final equation looks like this:-

`(partial^2 u)/(partial x^ 2) = ( − 12.83333/ 2 + 2 ( 17.83333 ) + 9 ( − 13.00000 ) /2 + 8 ( 5.08333 ) + 25 ( − 0.08333 ) /2 ) f ′ ' ( i ) + ( − 12.83333 /720 + 64 ( 17.83333 ) /720 + 729 ( − 13.00000 ) /720 + 4096 ( 5.08333 )/ 720 + 15625 ( − 0.08333 ) /720 ) f ′ ' ' ' ' ' ( i ) Deltax^4`

3. Using Skewed Left-Sided Difference:

To derive 4th order skewed left-sided difference at point (i) of function. Since we’re using the skewed left-sided method, here we have to define five points on the left of (i). Let those points be (i-1), (i-2), (i-3), (i-4), and (i-5); with a spacing of `Deltax`each.

The numerical stencil will look like this:

According to the above stencil, our hypothesis equation will be:

`(partial^ 2 u)/(partial x^ 2) = a f ( i − 5 ) + b f ( i − 4 ) + c f ( i − 3 ) + d f ( i − 2 ) + e f ( i − 1 ) + g f ( i )`

To find the values of coefficients (a,b,c,d,e,g), we’re going to use the Taylor table method.

Writing Taylor series expansion for each term of equation (1) as:-

`f ( i − 5 ) = ( a ) f ( i ) − (( a ) f ′ ( i ) ( 5 Δ x ))/ ( 1 ! ) + (( a ) f ′ ' ( i ) ( 5 Δ x ) ^2 )/( 2 ! ) − (( a ) f ′ ' ' ( i ) ( 5 Δ x ) ^3 )/( 3 ! ) + ( (a ) f ′ ' ' ' ( i ) ( 5 Δ x ) ^4)/ ( 4 ! ) − (( a ) f ′ ' ' ' ' ( i ) ( 5 Δ x )^ 5 )/( 5 ! ) +-----------`

b) `f ( i − 4 ) = ( b ) f ( i ) − (( b ) f ′ ( i ) ( 4 Δ x ))/ ( 1 ! ) + ( (b ) f ′ ' ( i ) ( 4 Δ x )^ 2 )/( 2 ! ) − ( (b ) f ′ ' ' ( i) ( 4 Δ x )^3) / ( 3 ! ) + ( (b ) f ′ ' ' ' ( i ) ( 4 Δ x )^ 4)/ ( 4 ! ) − (( b ) f ′ ' ' ' ' ( i ) ( 4 Δ x ) ^5)/ ( 5 ! ) +---------`

c) `f ( i − 3 ) = ( c ) f ( i ) − (( c ) f ′ ( i ) ( 3 Δ x ))/ ( 1 ! ) + (( c ) f ′ ' ( i ) ( 3 Δ x ) ^2)/ ( 2 ! ) − (( c ) f ′ ' ' ( i ) ( 3 Δ x ) ^3)/ ( 3 ! ) + ( (c ) f ′ ' ' ' ( i ) ( 3 Δ x ) ^4)/ ( 4 ! ) − (( c ) f ′ ' ' ' ' ( i ) ( 3 Δ x )^ 5 )/( 5 ! ) +---------`

d) `f ( i − 2 ) = ( d ) f ( i ) − (( d ) f ′ ( i ) ( 2 Δ x ))/ ( 1 ! ) +( ( d ) f ′ ' ( i ) ( 2 Δ x ) ^2)/ ( 2 ! ) − (( d ) f ′ ' ' ( i ) ( 2 Δ x ) ^3) /( 3 ! ) + ( (d ) f ′ ' ' ' ( i ) ( 2 Δ x )^ 4)/ ( 4 ! ) − (( d ) f ′ ' ' ' ' ( i ) ( 2 Δ x )^ 5 )/( 5 ! ) +-----------`

e) `f ( i − 1 ) = ( e ) f ( i ) − (( e ) f ′ ( i ) ( Δ x ))/ ( 1 ! ) + (( e ) f ′ ' ( i ) ( Δ x ) ^2)/ ( 2 ! ) − ( (e ) f ′ ' ' ( i ) ( Δ x ) ^3)/ ( 3 ! ) + ( (e ) f ′ ' ' ' ( i ) ( Δ x ) ^4)/ ( 4 ! ) − (( e ) f ′ ' ' ' ' ( i ) ( Δ x ) ^5)/ ( 5 ! ) +-------`

g) `f ( i ) = ( g ) f ( i )`

By arranging the above Taylor series expansions in the Taylor table; we get:-

f(i) `Deltaxf'(i)` `Deltax^2f''(i)` `Deltax^3f'''(i)` `Deltax^f''''(i)` `Deltax^5f'''''(i)`
f(i-5) a -5a 25a/2 -125a/6 625a/24 -3125a/126
f(i-4) b -4b 8b -64b/6 256b/24 -1024b/120
f(i-3) c -3c 9c/2 -27c/6 81c/24 -243c/120
f(i-2) d -2d 2d -8d/6 16d/24 -32d/120
f(i-1) e -e e/2 -e/6 e/24 -e/120
f(i) g 0 0 0 0 0
`Sigmaf(i)+f(i-1)+f(i-2)+f(i-3)+f(i-4)+f(i-5)=(partial^2u)/(partialx^2)` 0 0 1 0 0 0

Only for the first six terms, we assume our hypothesis. That’s why we can’t say about the last term.

Here we have six unknowns, so, we have to solve six equations and get values of coefficients (a,b,c,d,e,g). We are going to form these equations by adding terms for the individual columns. The summation of coefficients of f′′(i) is equal to 1, and f(i),f′(i),f′′(i),f′′′(i),f′′′′(i),f′′′′′(i) is equal to 0; because we want to calculate the second-order derivative, f′′(i).

Therefore, the six equations are:-

a+b+c+d+e+g=0 ……….. (I)

0-5a-4b-3c-2d-e+0=0 ……….. (II)

25a/2+8b+9c/2+2d+e2+0=1 ……….. (III)

625a/24+256b/24+81c/24+16c/24+e/24+0=0 ……….. (V)

-3125a/120-1024b/120-243c/120-32d/120-e/120+0=0 ……….. (VI)

To get the solution of the above equations, we have to solve the above equations in matrix form; AX=B.

After solving the above matrix, we get the values of all the coefficients as:-

a=-0.83333

b=5.08333

c=-13.00000

d=17.83333

e=-12.83333

g=3.75000

Now, dividing both side of my hypothesis equation (1) by `Deltax^2`; we get:

`(partial^2 u)/(partialx^2) = (a f ( i − 5 ) + b f ( i − 4 ) + c f ( i − 3 ) + d f ( i − 2 ) + e f ( i − 1 ) + g f ( i ) )/( Delta^x 2 )`

After putting the values of all the coefficients(a,b,c,d,e,g) in the above equation, we will get the fourth-order skewed right-sided difference approximation for second-order derivative.

`(partial^ 2 u)/(partialx^ 2) = (( − 0.83333 ) f ( i − 5 ) + ( 5.08333 ) f ( i − 4 ) + ( − 13.00000 ) f ( i − 3 ) + ( 17.83333 ) f ( i − 2 ) + ( − 12.83333 ) f ( i − 1 ) + ( 3.75000 ) f ( i ) )/( Deltax^ 2 )`

Now, from our Taylor table:-

`(partial^ 2 u)/ (partial x^ 2) = ( a + b + c + + d + e + g ) f ( i ) + ( − 5 a − 4 b − 3 c − 2 d − e + 0 ) f ′ ( i ) Δ Deltax + ( 25 a/ 2 + 8 b + 9 c/ 2 + 2 d + e/ 2 + 0 ) f ′ ' ( i ) Deltax^ 2 + ( − 125 a/ 6 − 64 b /6 − 27 c /6 − 8 d/ 6 − e /6 + 0 ) f ′ ' ' ( i ) Deltax^3 + ( 625 a/ 24 + 256 b/ 24 + 81 c/ 24 + 16 c/ 24 + e/ 24 + 0 ) f ′ ' ' ' ( i ) Deltax^4 + ( − 3125 a /120 − 1024 b /120 − 243 c /120 − 32 d/ 120 − e /120 + 0 ) f ′ ' ' ' ' ( i ) Deltax ^5 +`

To get a second-order derivative the coefficient of f′′(i) has to be 1. And as we want approximation for the second-order derivative, so, the terms f(i),f′(i),f′′′(i),f′′′′(i),f′′′′′(i) will have to ignore or set to 0 in the above equation. On substituting the values of (I), (II), (IV), (V), and (VI) in the above equation; we’ll get:

`(partial^2 u)/ (partial x^ 2) = ( 25 a/ 2 + 8 b + 9 c /2 + 2 d + e/ 2 + 0 ) f ′ ' ( i ) Deltax ^2 + ( 15625 a/ 720 + 4096 b/ 720 + 729 c /720 + 64 d/ 720 + e /720 + 0 ) f ′ ' ' ' ' ' ( i ) Deltax ^6`

On dividing the above equation by `Deltax^2`; we get:-

`(partial^2 u )/(partialx ^2) = ( 25 a /2 + 8 b + 9 c/ 2 + 2 d + e/ 2 ) f ′ ' ( i ) + ( 15625 a /720 + 4096 b /720 + 729 c /720 + 64 d /720 + e /720 ) f ′ ' ' ' ' ' ( i ) Deltax^4`

As we discussed earlier also, the `Deltax^4` denotes the truncation error. Here we can see that order of the truncation error; [O(`Deltax^4`)] is ‘4’. Since we know that the order of approximation is defined by the truncation error term. Therefore, we can say that our skewed left-sided difference scheme is of 4th order accurate.

After putting the values of (a,b,c,d,e,g) in the above equation, the final equation looks like this:-

`(partial^2 u )/(partial x ^2) = ( 25 ( − 0.833333 )/ 2 + 8 ( 5.08333 ) + 9 ( − 13.00000 )/ 2 + 2 ( 17.83333 ) + − 12.83333 /2 ) f ′ ' ( i ) + ( 15625 ( − 0.08333 )/ 720 + 4096 ( 5.08333 )/ 720 + 729 ( − 13.00000 ) /720 + 64 ( 17.83333 ) /720 + − 12.83333 /720 ) f ′ ' ' ' ' ' ( i ) Deltax^ 4`

Program in MATLAB to Evaluate the Second-Order Derivative of the analytical function exp(x)*cos(x) and compare it with the numerical approximation that we have derived above

Firstly we’re going to write programs to develop functions for central, skewed right-sided errors at a particular point (x, dx), where dx represents spacing in between each node. These functions will help us to calculate errors over the range of ‘dx’. With the help of code ‘function out we’re going to make a function; which we save in the directory and call according to our need in the main program

Function for central difference 4th order error at particular spacing ‘dx’

% Creating function for the 4th order approximation of the second-order; derivative using central difference;
 
function error_central_differencing = central_difference(x,dx)
 
% Analytical function f(x) = exp(x)*cos(x);
% f''(x) = -2*exp(x)*sin(x);
 
analytical_derivative = -2*exp(x)*sin(x);
 
% values of coefficients calculated from our Taylor table;
 
a=-0.083333;
b=1.333333;
c=-2.500000;
d=1.333333;
e=-0.083333;
 
% numerical derivative;
% central differencing =
% ((a*f(x-2*dx))+(b*f(x-dx))+(c*f(x))+(d*f(x+dx))+(e*f(x+2*dx)))/dx^2;
%central difference fourth order approximation;
 
central_differencing_fourth_order_approx = ((a*exp(x-2*dx)*cos(x-2*dx))+(b*exp(x-dx)*cos(x-dx))+(c*exp(x)*cos(x))+(d*exp(x+dx)*cos(x+dx))+(e*exp(x+2*dx)*cos(x+2*dx)))/dx^2;
 
error_central_differencing = abs(central_differencing_fourth_order_approx-analytical_derivative);
end

Function for skewed left-sided difference 4th order error at particular spacing ‘dx’:

% Creating function for the 4th order approximation of the second-order derivative using skewed left-sided difference;
 
function error_skewed_left_sided=skewed_left_sided(x,dx)
 
% analytical function f(x)=exp(x)*cos(x);
%f''(x)=-2*exp(x)*sin(x);
 
analytical_derivative=-2*exp(x)*sin(x);
 
%values of coefficients calculated from our Taylor table;
 
a=-0.83333;
b=5.08333;
c=-13.00000;
d=17.83333;
e=-12.83333;
g=3.75000;
 
%numerical derivative;
%skewed left-sided differencing=((a*f(x-5*dx))+(b*f(x-4*dx))+(c*f(x-3*dx))+(d*f(x-2*dx))+(e*f(x-dx))+(g*f(x)))/dx^2;
%skewed left-sided fourth order approximation;
 
skewed_left_sided_fourth_order_approx=((a*exp(x-5*dx)*cos(x-5*dx))+(b*exp(x-4*dx)*cos(x-4*dx))+(c*exp(x-3*dx)*cos(x-3*dx))+(d*exp(x-2*dx)*cos(x-2*dx))+(e*exp(x-dx)*cos(x-dx))+(g*exp(x)*cos(x)))/dx^2;
 
error_skewed_left_sided=abs(skewed_left_sided_fourth_order_approx-analytical_derivative);
end

Program for calculating 4th order error of second-order derivative using central, skewed right-sided, and skewed left-sided scheme using previously developed functions:

%calculating 4th order error of second-order derivative using central, skewed right-sided and left-sided scheme;
clear all
close all
clc
 
x=pi/3;
dx=linspace(pi/4,pi/40000,40);
 
%creating 'for' loop;
for i=1:length(dx)
    central_difference_error(i)=central_difference(x,dx(i));
    skewed_left_side_error(i)=skewed_left_sided(x,dx(i));
end
 
%plotting graphs for dx vs errors for central & skewed left-sided approximations
 
figure(1)
plot(dx,central_difference_error,'linewidth',2)
hold on
grid on
plot(dx,skewed_left_side_error,'g','linewidth',2)
xlabel('dx')
ylabel('error')
legend('Central Difference Error','Skewed Right-Sided Error','Skewed Left-Sided Error')
 
figure(2)
loglog(dx,central_difference_error,'linewidth',2)
hold on
grid on
loglog(dx,skewed_left_side_error,'g','linewidth',2)
xlabel('dx')
ylabel('error')
legend('Central Difference Error','Skewed Right-Sided Error','Skewed Left-Sided Error')

Now inserting the graph in the below file:

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.