Project

Fourth order approximation of a second order derivative

The following methods of approximation have been considered in this assignment:

  1. Central difference
  2. Skewed left sided difference
  3. Skewed right sided difference

The approximation is of fourth order, and the derivative is of second order. The calculations done in order to obtain the coefficients associated with each point that we have considered date from are displayed in the images below. These coefficients play a vital role in the approximation of the function, and will be elaborated below.

1) Central difference:

In the central difference method, we use two points from both sides of the reference point, in order to calculate it.

Fig(a).Position of data points and direction for CD.

In Taylor\'s table, we observe that for the column \' `f\'\'(x)*trianglex^2`\' , the value of the summation of the coefficients is 1. This is due to the fact that we want to obtain the second order differential equation. Because of this, the summation of the other columns is zero.

MATLAB has been used to solve the matrix mentioned above. The code used to solve this matrix has been displayed after the images.

2)Skewed right side difference:

Fig(b).Position of data points and direction for SRSD.

Truncation error is the error that we obtain when we approximate the function to a higher order. In this case, it has been neglected.

3)Skewed left side differencing:

Fig(c).Position of data points and direction for SLSD.

Reason behind the difference in the number of coefficients in Central differencing and Skewed right and left sided differencing:

In central differencing, we are forced to keep the number of points we consider data from or the stencil, symmetric. This means, the number of points on the right side of the reference point and the left side of the reference point must be equal. If this is not done, the stencil becomes unsymmetric and hence, skewed. So, the number of data points that we considered is limited to an odd number.

This is not a limitation when we consider Skewed right and left sided differencing. The number of data points is not restricted in this fashion and can be both even and odd. This proves to be an advantage over Central differencing in some scenarios.

Matrix computation example:

The following code has been used to calculate the coefficients:

Example code:

The following code is for Skewed left sided differencing:

clear all
close all
clc

A = [ 1 1 1 1 1 1;
     0 -1 -2 -3 -4 -5;
     0 1/2 2 9/2 8 25/2;
     0 -1/6 -4/3 -9/2 -32/3 -125/6;
     0 1/24 2/3 27/8 32/3 625/24;
     0 -1/120 -32/120 -243/120 -1024/120 -3125/120];
 
 B = [0;0;1;0;0;0]
 
 X = A\\B

Results obtained:

The analytical function that is considered is analytical_function_derivative = exp(x)*cos(x)

The analytical derivative of the function is as follows:

analytical function derivative = (-2)*exp(x)*sin(x).

The value of the analytical derivative calculated on MATLAB is \'-4.9357\'.

The image below displays the same in the command window.

The following code has been used to compute the three different methods of approximation of a fourth order for a second order derivative.

clear all
close all
clc

% Assumed values of x and dx:
x = pi/3
dx = linspace(pi/50,pi/5000,20)


% given function:
analytical_function = exp(x)*cos(x) 
analytical_function_derivative = (-2)*exp(x)*sin(x)

% Central approximation of the fourth order for a second order derivative:
C_S_Approximation = (-0.0833.*exp(x-(2*dx)).*cos(x-(2*dx)) + (1.3333.*exp(x-dx).*cos(x-dx)) - (2.5.*exp(x).*cos(x)) + (1.3333.*exp(x+dx).*cos(x+dx)) - (0.0833.*exp(x+(2*dx)).*cos(x+(2*dx))))./(dx.^2);


% right side skewed fourth order approximation for a second order function. 

R_S_Skewed_approximation = (((3.75).*exp(x).*cos(x) - ((12.8333).*exp(x+dx).*cos(x+dx)) + ((17.8333).*exp(x+(2.*dx)).*cos(x+(2.*dx))) - ((13).*exp(x+(3.*dx)).*cos(x+(3.*dx)))+ ((5.0833).*exp(x+(4.*dx)).*cos(x+(4.*dx))))- ((0.8333).*exp(x+(5.*dx)).*cos(x+(5.*dx))))./(dx.^2)

% left side skewed fourth order approximation for a second order function. 
L_S_Skewed_approximation = (((3.75).*exp(x).*cos(x) - (12.8333).*exp(x-dx).*cos(x-dx) + (17.8333).*exp(x-(2.*dx)).*cos(x-(2.*dx)) - (13).*exp(x-(3.*dx)).*cos(x-(3.*dx))+ (5.0833).*exp(x-(4.*dx)).*cos(x-(4.*dx)))- (0.8333).*exp(x-(5.*dx)).*cos(x-(5.*dx)))./(dx.^2)

% first order approximation using backward approximation method 
backward_approximation_method = ((exp(x).*cos(x)) - (exp(x-dx).*cos(x-dx)))./dx ;

% first order approximation using forward approximation method :
 forward_approximation_method = ((exp(x+dx).*cos(x+dx)) - (exp(x).*cos(x)))./dx ;

% Error calculation:
Central_error = abs(analytical_function_derivative - C_S_Approximation);
R_S_Skewed_error = abs(analytical_function_derivative - R_S_Skewed_approximation);
L_S_Skewed_error = abs(analytical_function_derivative - L_S_Skewed_approximation);
forward_approximation_error = abs(analytical_function_derivative -  forward_approximation_method);
backward_approximation_error = abs(analytical_function_derivative -  backward_approximation_method);



loglog(dx,Central_error,\'r\')
hold on
loglog(dx,R_S_Skewed_error,\'b\')
hold on
loglog(dx,L_S_Skewed_error,\'g\')

legend(\'Central error\',\'Skewed right side error\',\'Skewed left side error\')
xlabel(\'Values of dx\')
ylabel(\'Error values\')

The coefficients that we place in the equation play a vital role in the approximation of the derivative. A point that has been observed during the course of this assignment is that rounding off a coefficient yields an error in the approximation. This becomes quite apparent in the graph that we plot for \'dx vs error\' ,that will be shown below.

For example, if 1.3333 was rounded off to 1.334, the plot changes drastically, and we get a slope, instead of a curve.

If we take the raw values of the coefficients that we obtain after solving the matrix from MATLAB,the graph of \'dx vs error\', displays the error in central differencing method as straight line, and is constant for all values of dx, right from the largest value of dx to the smallest value. Whereas, this is not the case with Skewed Right and Left sided differencing. In these two cases, we obtain a curve instead of a straight line. This is mainly because the approximation of the second order derivative of first value of dx is different from the final value dx. The accuracy of the answer increases as the value of dx decreases.

Any error in rounding off the coefficients lead to very different graphs, and plot different results.

For comparison, both graphs will be shown below.

Correct Plot:

fig(d). Correct plot.

From the above graph, we can see that for this particular case, the central differencing method yields a straight line, denoting constant error. The approximations obtained from this method are constant for all values of dx, and the results of the same have been displayed below.

The values for skewed left and right side differencing has been displayed below:

We can see that the values of skewed schemes are less accurate of the larger values of dx and becomes more accurate as dx decreases.

Due to this, we obtain a curve for error, and not a straight line as displayed by the central differencing scheme. We can say that in this particular case, the values obtained by central differencing, are more accurate as compared to the two skewed schemes, although this will not be the case for different scenarios. This point will be discussed below.

The large curves displayed by the two skewed schemes is due to higher, inaccurate values of approximation. Hence, the error is larger, but decreases with each incremental decrease of dx, until eventually, reaching a relatively accurate answer.

Effect of rounding off certain coefficients in central and skewed schemes:

The changes in the following code are the values of some coefficients have been rounded off.

clear all
close all
clc

% Assumed values of x and dx:
x = pi/3
dx = linspace(pi/50,pi/5000,20)


% given function:
analytical_function = exp(x)*cos(x) 
analytical_function_derivative = (-2)*exp(x)*sin(x)

% Central approximation of the fourth order for a second order derivative:
C_S_Approximation = (-0.0833.*exp(x-(2*dx)).*cos(x-(2*dx)) + (1.334.*exp(x-dx).*cos(x-dx)) - (2.5.*exp(x).*cos(x)) + (1.334.*exp(x+dx).*cos(x+dx)) - (0.0833.*exp(x+(2*dx)).*cos(x+(2*dx))))./(dx.^2)


% right side skewed fourth order approximation for a second order function. 

R_S_Skewed_approximation = (((3.75).*exp(x).*cos(x) - ((12.834).*exp(x+dx).*cos(x+dx)) + ((17.834).*exp(x+(2.*dx)).*cos(x+(2.*dx))) - ((13).*exp(x+(3.*dx)).*cos(x+(3.*dx)))+ ((5.0833).*exp(x+(4.*dx)).*cos(x+(4.*dx))))- ((0.834).*exp(x+(5.*dx)).*cos(x+(5.*dx))))./(dx.^2)

% left side skewed fourth order approximation for a second order function. 
L_S_Skewed_approximation = (((3.75).*exp(x).*cos(x) - (12.834).*exp(x-dx).*cos(x-dx) + (17.834).*exp(x-(2.*dx)).*cos(x-(2.*dx)) - (13).*exp(x-(3.*dx)).*cos(x-(3.*dx))+ (5.0833).*exp(x-(4.*dx)).*cos(x-(4.*dx)))- (0.834).*exp(x-(5.*dx)).*cos(x-(5.*dx)))./(dx.^2)

% first order approximation using backward approximation method 
backward_approximation_method = ((exp(x).*cos(x)) - (exp(x-dx).*cos(x-dx)))./dx ;

% first order approximation using forward approximation method :
 forward_approximation_method = ((exp(x+dx).*cos(x+dx)) - (exp(x).*cos(x)))./dx ;

% Error calculation:
Central_error = abs(analytical_function_derivative - C_S_Approximation);
R_S_Skewed_error = abs(analytical_function_derivative - R_S_Skewed_approximation);
L_S_Skewed_error = abs(analytical_function_derivative - L_S_Skewed_approximation);
forward_approximation_error = abs(analytical_function_derivative -  forward_approximation_method);
backward_approximation_error = abs(analytical_function_derivative -  backward_approximation_method);



loglog(dx,Central_error,\'r\')
hold on
loglog(dx,R_S_Skewed_error,\'b\')
hold on
loglog(dx,L_S_Skewed_error,\'g\')

legend(\'Central error\',\'Skewed right side error\',\'Skewed left side error\')
xlabel(\'Values of dx\')
ylabel(\'Error values\')

In the following image, the graph obtained after rounding off some coefficient values have been rounded off has been displayed below.

As we can see, the results have changed drastically, mostly because they are incorrect and this has been shown below. Although the graph is easier to read, and shows that central differencing is in this case, inferior to the skewed schemes, displaying much larger error, it cannot be considered, because the values obtained by approximation are smiply incorrect.

In the image displayed above, we can observe that the values of approximation(Skewed right side differencing) obtained after rounding off the coefficients, are far from the actual value obtained by the analytical method. But the graph obtained above has been obtained after computing the equation using rounded off values.

The point of this section was to show the sensitivity of the coefficients used in the various differencing schemes.

The values obtained by MATLAB after solving the matrix should be preserved, without rounding off some coefficients, in order to avoid change in results.

Comparison with forward and backward approximation:

The following code has been used to include FDA and BDA.

clear all
close all
clc

% Assumed values of x and dx:
x = pi/3
dx = linspace(pi/50,pi/5000,20)


% given function:
analytical_function = exp(x)*cos(x) 
analytical_function_derivative = (-2)*exp(x)*sin(x)

% Central approximation of the fourth order for a second order derivative:
C_S_Approximation = (-0.0833.*exp(x-(2*dx)).*cos(x-(2*dx)) + (1.3333.*exp(x-dx).*cos(x-dx)) - (2.5.*exp(x).*cos(x)) + (1.3333.*exp(x+dx).*cos(x+dx)) - (0.0833.*exp(x+(2*dx)).*cos(x+(2*dx))))./(dx.^2)


% right side skewed fourth order approximation for a second order function. 

R_S_Skewed_approximation = (((3.75).*exp(x).*cos(x) - ((12.8333).*exp(x+dx).*cos(x+dx)) + ((17.8333).*exp(x+(2.*dx)).*cos(x+(2.*dx))) - ((13).*exp(x+(3.*dx)).*cos(x+(3.*dx)))+ ((5.0833).*exp(x+(4.*dx)).*cos(x+(4.*dx))))- ((0.8333).*exp(x+(5.*dx)).*cos(x+(5.*dx))))./(dx.^2)

% left side skewed fourth order approximation for a second order function. 
L_S_Skewed_approximation = (((3.75).*exp(x).*cos(x) - (12.8333).*exp(x-dx).*cos(x-dx) + (17.8333).*exp(x-(2.*dx)).*cos(x-(2.*dx)) - (13).*exp(x-(3.*dx)).*cos(x-(3.*dx))+ (5.0833).*exp(x-(4.*dx)).*cos(x-(4.*dx)))- (0.8333).*exp(x-(5.*dx)).*cos(x-(5.*dx)))./(dx.^2)

% first order approximation using backward approximation method 
backward_approximation_method = ((exp(x).*cos(x)) - (exp(x-dx).*cos(x-dx)))./dx ;

% first order approximation using forward approximation method :
 forward_approximation_method = ((exp(x+dx).*cos(x+dx)) - (exp(x).*cos(x)))./dx ;

% Error calculation:
Central_error = abs(analytical_function_derivative - C_S_Approximation);
R_S_Skewed_error = abs(analytical_function_derivative - R_S_Skewed_approximation);
L_S_Skewed_error = abs(analytical_function_derivative - L_S_Skewed_approximation);
forward_approximation_error = abs(analytical_function_derivative -  forward_approximation_method);
backward_approximation_error = abs(analytical_function_derivative -  backward_approximation_method);



loglog(dx,Central_error,\'r\')
hold on
loglog(dx,R_S_Skewed_error,\'b\')
hold on
loglog(dx,L_S_Skewed_error,\'g\')
hold on
loglog(dx,forward_approximation_error,\'y\')
hold on
loglog(dx,backward_approximation_error,\'k\')


legend(\'Central error\',\'Skewed right side error\',\'Skewed left side error\',\'forward approximation\',\'backward approximation\')
xlabel(\'Values of dx\')
ylabel(\'Error values\')

The plot given below displays the all the errors mentioned in the program.

From the figure above, it is quite appparent that the accuracy of forward difference and backward difference approximation is not even close to the skewed schemes and central differencing methods.

Skewed scheme attributes, compared to central differencing :

  • In central differencing, there is a restriction on the number of data points that can be utilized for computation of a certain point. The number of data points collectively, will always be an odd number, mainly due to the fact that the stencil must be symmetric, as mentioned above. An inequality will cause it to become skewed. This restriction does not exist for skewed scheme, and the number of data points considered can be both even and odd.
  • The usage of a skewed scheme becomes apparent in unidirectional flows. Since a skewed scheme proceeds in one direction only, the computation of a single point in that scenario becomes far more accurate, as the data points are along the path of flow. As a simple example, suppose we would like to calculate heat moving in a straight line along a surface(this has been considered for simplicity). We can calculate the heat at a certain point far more accurately, mainly because we can take into consideration \'n\' number of nodes ahead of it. This proves to be an apparent advantage over central differencing. If central differencing was used in this case, it would consider nodes behind the data point as well, and in this case, the accuracy will decrease, as the information it can take from the nodes or points ahead of the main reference point is limited.
  • This is the main use of a skewed scheme, and for cases like these, the skewed scheme can prove to be more accurate.
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.