Challenge

Week 3 - Taylor table method and Matlab code

Code:

clc;clear all; close all;
syms x
func=diff(exp(x)*cos(x),x);
df= eval( (subs(diff(func,x),x,pi/3)) );

hvals = linspace(pi/4,pi/400,100); % vector of h values

myf=@(x) exp(x).*cos(x);
x=pi/3;
%Left Skewed approximation to the second derivative-Fourth order
f2prime_leftskew_4order = ((45*myf(x))-(154*myf(x-hvals))+(214*myf(x-(2*hvals)))-(156*myf(x-(3*hvals)))+(61*myf(x-(4*hvals)))-(10*myf(x-(5*hvals))))./(12.*(hvals.^2));



%Centered approximation to the second derivative-Fourth order
f2prime4thorder = (-myf(x+(2*hvals))+(16*myf(x+hvals))-(30*myf(x))+(16*myf(x-hvals))-myf(x-(2*hvals)))./(12.*(hvals.^2));

%Right Skewed approximation to the second derivative-Fourth order
f2prime_rightskew_4order = ((45*myf(x))-(154*myf(x+hvals))+(214*myf(x+(2*hvals)))-(156*myf(x+(3*hvals)))+(61*myf(x+(4*hvals)))-(10*myf(x+(5*hvals))))./(12.*(hvals.^2));

loglog(hvals,abs(f2prime_leftskew_4order-df),'marker','s');
hold on
loglog(hvals,abs(f2prime4thorder-df),'marker','o');
hold on
loglog(hvals,abs(f2prime_rightskew_4order-df),'marker','d');
xlim([10^-2.1 10^-0.1])
legend({'Left skewed-4th order','Centered-4th order','Right skewed-4th order'},'location','southeast')
title('log-log plot of absolute error of approx (2nd derivative) ');
xlabel('\deltax', 'fontweight','bold','fontsize',12);
ylabel('Absolute error (\epsilon)','fontweight','bold','fontsize',12);

Plot:

Results:

It can be observed from the plot that in the case of second derivative with 4th order approximation, the absolute error in least in the central difference scheme which is followed by left skewed and then by right skewed. The Central difference has less error because it uses datapoints symmetrically from both left and right sides.

Advantage of a skewed scheme over central difference scheme:

A skewed finite difference scheme is particularly useful for calculating the values of functions at the boundaries or interfaces. In such cases, information is generally available for the datapoints on the left or right side of the point in question. Due to this reason, the central difference schemes cannot be employed for approximating such points since a central difference scheme uses datapoints symmetrically from either directions.

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.