Project

Skewed Difference Approximation using Matlab

Derivation:

Central difference approximation:

second derivative = [a*f(i-2)+b*f(i-1)+c*f(i)+d*f(i+1)+e*f(i+2)]/[dx]^2

By using taylor\'s series the taylor\'s table can be given as

By solving the matrix AX = B we can find the values of the coordinates.

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.3300 -0.1660 0 0.1660 1.3300
0.6600 0.0417 0 0.0417 0.6600

X = a_c B = 0

b_c 0

c_c 1

d_c 0

e_c 0

Skewed Left sided approximation:

second derivative = [a*f(i)+b*f(i-1)+c*f(i-2)+d*f(i-3)+e*f(i-4)]/[dx]^2

By using taylor\'s series the taylor\'s table can be given as

By solving the matrix AX = B we can find the values of the coordinates.

A = 1.0000 1.0000 1.0000 1.0000 1.0000
0 -1.0000 -2.0000 -3.0000 -4.0000
0 0.5000 2.0000 4.5000 8.0000
0 -0.1660 -1.3300 -4.5000 -10.6600
0 0.0416 0.6600 3.3750 10.6600

X = a_sl B = 0

b_sl 0

c_sl 1

d_sl 0

e_sl 0

Skewed Left sided approximation:

second derivative = [a*f(i)+b*f(i+1)+c*f(i+2)+d*f(i+3)+e*f(i+4)]/[dx]^2

By using taylor\'s series the taylor\'s table can be given as

By solving the matrix AX = B we can find the values of the coordinates.

A = 1.0000 1.0000 1.0000 1.0000 1.0000
0 1.0000 2.0000 3.0000 4.0000
0 0.5000 2.0000 4.5000 8.0000
0 0.1660 1.3300 4.5000 10.6600
0 0.0416 0.6600 3.3750 10.6600

X = a_sr B = 0

b_sr 0

c_sr 1

d_sr 0

e_sr 0

By using the above derived equations the second order approximation can be found

Coding:

clear all
close all
clc
%matrix AX = B
%In matrix B only the value for second order difference is considered as 1
B = [0;0;1;0;0];
%From taylor\'s table of central difference
cm = [1 -2 2 -1.33 0.66 ; 1 -1 0.5 -0.166 0.04166 ; 1 0 0 0 0 ; 1 1 0.5 0.166 0.04166 ; 1 2 2 1.33 0.66];
cm = cm\';
%Solving the matrix
X_c = linsolve(cm,B);
%Extracting the values of coefficient
a_c = X_c(1);b_c = X_c(2);c_c = X_c(3);d_c = X_c(4);e_c = X_c(5);
%From taylor\'s table of skewed left 
slm = [1 0 0 0 0 ; 1 -1 0.5 -0.166 0.0416 ; 1 -2 2 -1.33 0.66 ; 1 -3 4.5 -4.5 3.375 ; 1 -4 8 -10.66 10.66];
slm = slm\';
%Solving the matrix
X_sl = linsolve(slm,B);
%Extracting the values of coefficient
a_sl = X_sl(1);b_sl = X_sl(2);c_sl = X_sl(3);d_sl = X_sl(4);e_sl = X_sl(5);
%From taylor\'s table of skewed right
srm = [1 0 0 0 0 ; 1 1 0.5 0.166 0.04166 ; 1 2 2 1.33 0.66 ; 1 3 4.5 4.5 3.375 ; 1 4 8 10.66 10.66];
srm = srm\';
%Solving the matrix
X_sr = linsolve(srm,B);
%Extracting the values of coefficient
a_sr = X_sr(1);b_sr = X_sr(2);c_sr = X_sr(3);d_sr = X_sr(4);e_sr = X_sr(5);
x = pi/4;
%analytical_function = exp(x)*cos(x)
sec_derivative = -2*exp(x)*sin(x);
%Assigning values for dx
dx = linspace(pi/40,pi/4000,100);
for i = 1:length(dx)
    %Calculating the function values for different points
    left_4 = (exp(x-(4*dx(i))))*(cos(x-(4*dx(i))));
    left_3 = (exp(x-(3*dx(i))))*(cos(x-(3*dx(i))));
    left_2 = (exp(x-(2*dx(i))))*(cos(x-(2*dx(i))));
    left_1 = (exp(x-(dx(i))))*(cos(x-(dx(i))));
    point = (exp(x))*(cos(x));
    right_1 = (exp(x+(dx(i))))*(cos(x+(dx(i))));
    right_2 = (exp(x+(2*dx(i))))*(cos(x+(2*dx(i))));
    right_3 = (exp(x+(3*dx(i))))*(cos(x+(3*dx(i))));
    right_4 = (exp(x+(4*dx(i))))*(cos(x+(4*dx(i))));
    %Substituting the values in the derived formula
    central_difference = ((a_c*left_2)+(b_c*left_1)+(c_c*point)+(d_c*right_1)+(e_c*right_2))/((dx(i))^2);
    skewed_left = ((a_sr*point)+(b_sr*left_1)+(c_sr*left_2)+(d_sr*left_3)+(e_sr*left_4))/((dx(i))^2);
    skewed_right = ((a_sr*point)+(b_sr*right_1)+(c_sr*right_2)+(d_sr*right_3)+(e_sr*right_4))/((dx(i))^2);
    %Calculating error values
    error_c(i) = abs(central_difference-sec_derivative);
    error_sl(i) = abs(skewed_left-sec_derivative);
    error_sr(i) = abs(skewed_right-sec_derivative);
end
%Plotting the error values
figure(1)
loglog(dx,error_c,\'--b\',\'linewidth\',2)
hold on
loglog(dx,error_sl,\'--r\',\'linewidth\',2)
loglog(dx,error_sr,\'--y\',\'linewidth\',2)
grid on
xlabel(\'dx\')
ylabel(\'Error\')
legend(\'central difference\',\'skewed left\',\'skewed right\')

Solving Matrices:

  • There are three different approximations to be found.
  • Thus three different matrix solutions are obtained.
  • By using linsolve command the solution for the matrix is found.
  • These solutions represents the coefficients of the equations.

Function:

  • analytical_function is exp(x)*cos(x) the second derivative of the function is given as -2*exp(x)*sin(x).
  • x is assumed to be pi/4 and the dx value is ranged between pi/40 to pi/4000.

For loop:

  • For each value of dx the three approximation equation has to be performed.
  • Thus the function values at all the neighbouring points are calculated for each value of dx.
  • These values are substituted in the equations to obtain the second order approximation.
  • Error values can be calculated by finding the difference between the analytical differences and calculated differences.
  • Using loglog command the errors are plotted in the graph.

Reults:

From the results, it is clear that the values of central difference is more accurate than the values obtained from skewed differences.

In your own words, describe why a skewed scheme is useful? What can a skewed scheme do that a CD scheme cannot do?

In central difference scheme for taking the fourth order approximation points from both left and right side is needed. In case if there is no point on any side central difference scheme cannot be used. In such cases the skewed scheme will be useful.

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.