Challenge

Week 3 - Taylor table method and Matlab code

FOURTH ORDER APPROXIMATION OF SECOND ORDER DERIVATIVE:

CENTRAL DIFFERENCE METHOD:

SKEWED LEFT SIDE METHOD

SKEWED RIGHT SIDE METHOD

MAIN PROGRAM

clear all
close all
clc
%f=exp(x)*cos(x)
x=input('Enter the point of approximation');
max_dx=input('Enter max step size');
size=input('Enter interval size');
min_dx=input('Enter min step size');
dx=linspace(max_dx,min_dx,size);
if(x<max_dx)||(x<min_dx)
    error('Invalid Entry')
end
%f"(x)=-2*exp(x)*sin(x);
analytical_derivative=-2*exp(x)*sin(x)
for i=1:length(dx)
    central_approximation_error(i)=central_approximation(x,dx(i));
    right_skewed_approximation_error(i)=right_skewed_approximation(x,dx(i));
    left_skewed_approximation_error(i)=left_skewed_approximation(x,dx(i));
end
 figure (1)
 loglog(dx,central_approximation_error,'b')
 hold on
 loglog(dx,right_skewed_approximation_error,'r')
 hold on
 loglog(dx,left_skewed_approximation_error,'g')
 legend('central_approximation_error','right_skewed_approximation_error','left_skewed_approximation_error')

RIGHT SKEWED METHOD

function error =right_skewed_approximation(x,dx )
f=@(x) exp(x)*cos(x)
%f"(x)=-2*exp(x)*sin(x);
analytical_derivative=-2*exp(x)*sin(x)
%to find a,b,c,d,e
syms a b c d e
eqn1= a+b+c+d+e ==0;
eqn2= b+2*c+3*d+4*e ==0;
eqn3=b/2+4*c/2+9*d/2+16*e/2 ==1;
eqn4=b/6+8*c/6+27*d/6+64*e/2 ==0;
eqn5=b/24+16*c/24+81*d/24+256*e/24==0;
X=solve([eqn1 eqn2 eqn3 eqn4 eqn5],[a,b,c,d,e]);
a=X.a;
b=X.b;
c=X.c;
d=X.d;
e=X.e;
right_skewed_differencing=(a*f(x)+b*f(x+dx)+c*f(x+2*dx)+d*f(x+3*dx)+e*f(x+4*dx))/(dx^2);
error=abs(right_skewed_differencing-analytical_derivative)
end

LEFT SKEWED METHOD

function error =left_skewed_approximation(x,dx)
f=@(x) exp(x)*cos(x)
%f"(x)=-2*exp(x)*sin(x);
analytical_derivative=-2*exp(x)*sin(x)
%to find a,b,c,d,e
syms a b c d e
eqn1= a+b+c+d+e==0;
eqn2= -4*a-3*b-2*c-d==0;
eqn3=16*a/2+9*b/2+4*c/2+d/2==1;
eqn4=-64*a/6-27*b/6-8*c/6-d/6==0;
eqn5=256*a/24+81*b/24+16*c/24+d/24==0;
X=solve([eqn1 eqn2 eqn3 eqn4 eqn5],[a,b,c,d,e]);
a=X.a;
b=X.b;
c=X.c;
d=X.d;
e=X.e;
left_skewed_differencing=(a*f(x)+b*f(x+dx)+c*f(x+2*dx)+d*f(x+3*dx)+e*f(x+4*dx))/(dx^2);
error=abs(left_skewed_differencing-analytical_derivative)
end

RESULT

From the graph it is evident that central differencing scheme has the least error when compared to the skewed method. The central differncing method cannot be used all the time as higher order approximation of corner nodes of a grid cannot be calculated usng central diferencing 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.

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.