Challenge

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

1. Provide Value of x and dx.

2. Convert Simple function to Analytical Function.

3. Find no. for nods for Central, Skewed_right and Skewed_left sided scheme.

4. Based on the stencils, Calculate the Taylor Expansion in order to evaluate the values of coefficinet with the help of Taylor Table with respect to matrix i.e AX =B

5. Apply for loop in order to find out fourth order approximation.

6. find out the error between Numerical method and Analytical derivative.

7. Do the same for leftsided, rightsided and central diffrencing.

8. Plotting graph between dx and error for central, right & left schemes.

9. In order to shown the outputs looks smoother we used loglog method for plotting the graphs.

clear all
close all
clc

x = pi/3;
dx=linspace(pi/4000,pi/40,100);
Function = exp(x)*cos(x);   
Analytical_derivative = -2*exp(x)*sin(x);    

% General Taylor Expansion 
%f(x+a) = f(a)+f'(a)*x+(f"(a)*x^2)/2+(f"'(a)*x^3)/6+.....

% for Central Differencing 
% P(order of accuracy) = N(no. of nodes in the stencil)-Q(order of derivative)+1
% 4 = N-2+1
% N = 5
% f_dx(i) = a*f(x-2*(dx(i)))+b*f(x-dx(i))+c*f(x)+d*f(x+dx(i))+e*f(x+2*(dx(i)));

% After putting thee values of all nodes we got givn below..
[A] = [ 1 1 1 1 1; -2 -1 0 1 2; 2 1/2 0 1/2 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] = [a; b; c; d; e]
[X]= inv(A)*(B)

a=X(1);
b=X(2);
c=X(3);
d=X(4);
e=X(5);

for i=1:length(dx)
Central_differencing_fourth_order_approx(i) = ((a*exp(x-2*dx(i))*cos(x-2*dx(i)))+(b*exp(x-dx(i))*cos(x-dx(i)))+(c*exp(x)*cos(x))+(d*exp(x+dx(i))*cos(x+dx(i)))+(e*exp(x+2*dx(i))*cos(x+2*dx(i))))/dx(i)^2;
error_central_differencing(i) = abs(Central_differencing_fourth_order_approx(i)-Analytical_derivative);
end

%For skewed Left sided
% % P = N-Q
% % 4 = N -2
% % N = 6
[A] = [ 1 1 1 1 1 1; -5 -4 -3 -2 -1 0; 25 16 9 4 1 0; -125 -64 -27 -8 -1 0; 625 256 81 16 1 0; -3125 -1024 -243 -32 -1 0];
[B] = [0; 0; 2; 0; 0; 0];
% [X] = [a; b; c; d; e; g]
[X]= inv(A)*(B)

a=X(1);
b=X(2);
c=X(3);
d=X(4);
e=X(5);
g=X(6);

for i = 1:length(dx)
    
Skewed_left_sided_fourth_order_approx(i) = ((a*exp(x-5*dx(i))*cos(x-5*dx(i)))+(b*exp(x-4*dx(i))*cos(x-4*dx(i)))+(c*exp(x-3*dx(i))*cos(x-3*dx(i)))+(d*exp(x-2*dx(i))*cos(x-2*dx(i)))+(e*exp(x-dx(i))*cos(x-dx(i)))+(g*exp(x)*cos(x)))/(dx(i))^2;
error_skewed_left_sided_differencing(i) = abs(Skewed_left_sided_fourth_order_approx(i)-Analytical_derivative);

end

%For skewed Right sided
[A] = [1 1 1 1 1 1; 0 1 2 3 4 5; 0 1 4 9 16 25; 0 1 8 27 64 125; 0 1 16 81 256 625; 0 1 32 243 1024 3125];
[B] = [0; 0; 2; 0; 0; 0];
%[X] = [a; b; c; d; e; g]
[X]= inv(A)*[B]
            
a=X(1);
b=X(2);
c=X(3);
d=X(4);
e=X(5);
g=X(6);
    

for i = 1:length(dx)
% skewed_right_sided_fourth_order_approx(i) = ((a*exp(x)*cos(x))+(b*exp(x+dx(i))*cos(x+dx(i)))+(c*exp(x+2*dx(i))*cos(x+2*dx(i)))+(d*exp(x+3*dx(i))*cos(x+3*dx(i)))+(e*exp(x+4*dx(i))*cos(x+4*dx(i)))+(g*exp(x+5*dx(i))*cos(x+5*dx(i))))/(dx(i))^2;
skewed_right_sided_fourth_order_approx(i) = ((a*exp(x)*cos(x))+(b*exp(x+dx(i))*cos(x+dx(i)))+(c*exp(x+2*dx(i))*cos(x+2*dx(i)))+(d*exp(x+3*dx(i))*cos(x+3*dx(i)))+(e*exp(x+4*dx(i))*cos(x+4*dx(i)))+(g*exp(x+5*dx(i))*cos(x+5*dx(i))))/(dx(i))^2;
error_skewed_right_sided_differencing(i) = abs(skewed_right_sided_fourth_order_approx(i)-Analytical_derivative);

end

% Plotting graph between dx(i) v/s error for central, right_sided and left_sided

figure(1)
plot(dx,error_central_differencing,'g','linewidth',1);% to use loglog plot later
hold on 
grid on
plot(dx,error_skewed_left_sided_differencing,'y','linewidth',1);
hold on
plot(dx,error_skewed_right_sided_differencing,'r','linewidth',1);
xlabel('dx')
ylabel('error')
legend('Error Central Differencing', 'Error Skewed Right Sided Diffferencing', 'Error Skewed Left Sided Differencing')

figure(2)
loglog(dx,error_central_differencing,'g','linewidth',1);
hold on 
grid on
loglog(dx,error_skewed_left_sided_differencing,'y','linewidth',1);
hold on
loglog(dx,error_skewed_right_sided_differencing,'r','linewidth',1);
xlabel('dx')
ylabel('error')
legend('Error Central Differencing', 'Error Skewed Right Sided Diffferencing', 'Error Skewed Left Sided Differencing')


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.