Challenge

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

main code

clear all 
close all
clc

% Input value for x
x=pi/3;
 
dx = linspace(pi/4,pi/4000,25);

for i=1:length(dx)
    central_diff_error(i) = fourth_order_centra_diff(x,dx(i));
    skewed_right_side_error(i)= skewed_right_side(x,dx(i));
    skewed_left_side_error(i)=  skewed_left_side(x,dx(i));
end

figure(1)
loglog(dx,central_diff_error,'b')
hold on
loglog(dx,skewed_right_side_error,'g')
hold on
loglog(dx,skewed_left_side_error,'r')
xlabel('dx')
ylabel('error')
legend('central diff error','skewed right side error','skewed left side error')
Central_differencing_error code
function Central_differencing_error = fourth_order_centra_diff(x,dx)

% Ana function = exp(x)*cos(x)
 
%ana dervative = -2*exp(x)*sin(x)
analatical_dervative = -2*exp(x)*sin(x);

% Central_differencing
central_diff_error = (((-0.0833)*((exp(x-(2*dx)))*(cos(x-(2*dx)))))+((1.3333)*((exp(x-dx))*(cos(x-dx))))-((2.5000)*((exp(x))*(cos(x))))+((1.3333)*((exp(x+dx))*(cos(x+dx))))-((0.0833)*((exp(x+(2*dx)))*(cos(x+(2*dx))))))/(dx^2);

% Central_differencing_error
Central_differencing_error = abs(central_diff_error-analatical_dervative);
end
Right_sided_Skew_error code
function Right_sided_Skew_error = skewed_right_side(x,dx)

% Ana function = exp(x)*cos(x)
 
%ana dervative = -2*exp(x)*sin(x)
analatical_dervative = -2*exp(x)*sin(x);


% Fourth_order_Right_sided_differencing
skewed_right_side_error = (((3.7500)*((exp(x))*(cos(x))))-((12.8333)*((exp(x+dx))*(cos(x+dx))))+((17.8333)*((exp(x+(2*dx)))*(cos(x+(2*dx)))))-((13.0000)*((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);

% error
Right_sided_Skew_error = abs(skewed_right_side_error - analatical_dervative);
end
Left_sided_Skew_error code
function Left_sided_Skew_error = skewed_left_side(x,dx)
% Ana function = exp(x)*cos(x)
 
%ana dervative = -2*exp(x)*sin(x)
analatical_dervative = -2*exp(x)*sin(x);

% Fourth_order_Left_sided_differencing
skewed_left_side_error = (((-0.8333)*((exp(x-(5*dx)))*(cos(x-(5*dx)))))+((5.0833)*((exp(x-(4*dx)))*(cos(x-(4*dx)))))-((13.0000)*((exp(x-(3*dx)))*(cos(x-(3*dx)))))+((17.8333)*((exp(x-(2*dx)))*(cos(x-(2*dx)))))-((12.8333)*((exp(x-dx))*(cos(x-dx))))+((3.7500)*((exp(x))*(cos(x)))))/(dx^2);

% error
Left_sided_Skew_error = abs(skewed_left_side_error-analatical_dervative);
end

Output-:

Expanation-:

From above graph central diff method shows the less error as compared to right and left skew method.

While consider accuracy, central diff method is useful because it take data from left and right side that is positive and negative so it is resulted most accurate and less error method.

The Skewed method is only supported when we dont have sufficient data for central method just like at the bpundr we can not use the central method.

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.