Challenge

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

from the above question fourth order approimation in second order differenence

i find out the central difference by using the taylors equation

our numerical stencil will be

f(i-2),f(i-1),f(i),f(i+1),f(i+2) are the points from the center for the f(i)

and the distance between the two points of the equation is (deltax)

based on the above analysis equation we write is

diff(f,x,2)=a*f(i-2)+b*f(i-1)+c*f(i)+d*f(i+1)+e*f(i+2)

inorder to find the value of coefficents of a,b,c,d,e we right as the follows the from the taylors equation table

f(i) dxf(i) dx^2f"(i) dx^3f'"f(i) dx^4f''"f(i) dx^5f'""f(i) dx^6f'""'f(i)
f(i-2) a -2a 4a/2 -8a/6 16a/24 -32a/120

64a/240

f(i-1) b -b b/2 -b/6 b/24 -b/120

b/240

f(i) c 0 0 0 0 0

0

f(i+1) d d d/2 d/6 d/24 d/120 d/240
f(i+2) e 2e 4e/2 8e/6 16e/24 -32e/120 64e/240
sum(f(i-2):f(i+2) 0 0 1 0 0

there fore from the above equation we find the

a+b+c+d+e=0

-2a-b+0+d+2e=0

4a/2+b/2+0+d/2+4e/2=1

-8a/6-b/6+0+d/6+8e/=0

16a/24+b/24+0+d/24+16e/24=0

we right this equation in matrix from as show 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]

inverse (A)

a=-0.0833

b=1.33

c=-2.5000

d=1.333

e=0.0833

we subsitite this in the above equation as shown

similaary for the skewed right hand difference

defined for the five pont from the right hand side as showm

f(i),f(i+1),f(i+2),f(i+3),f(i+4),f(i+5)

we right as the

diff(f,x,2)=a*f(i)+b*f(i+1)+c*f(i+2)+d*f(i+3)+e*f(i+4)+g*f(i+5)

in the above equation a,b,c,d,e,g are the coefficient of the above equation

in the similary way, we find the

a=3.75

b=-12.88

c=17.888

d=-13.00

e=5.088

g=-0.833

using the skewed left hand difference

defined for the five pont from the right hand side as showm

f(i-5),f(i-4),f(i-3),f(i-2),f(i-1),f(i)

e right as the

diff(f,x,2)=a*f(i-5)+b*f(i-4)+c*f(i-3)+d*f(i-2)+e*f(i-1)+g*f(i)

in the above equation a,b,c,d,e,g are the coefficient of the above equation

in the similary way, we find the

a=-0.8333

b=5.0833

c=13.000

d=-17.833

e=-12.8333

g=3.75

program i divided the program in four parts as the

central difference

skewed right side difference

skewed left side difference

function [error_1]= numerical_function(x,dx)
%second order differenece
anatical_difference= -2*exp(x)*sin(x)
%fourth order central difference approixmation
%f"(x)=((a*f(i-2)+b*f(i-1)+c*f(i)+d*f(i+1)+e*f(i+2))/(dx)^2
%a,b,c,d,e are coefficient of the equation
a= -0.0833
b= 1.333
c= -2.5000
d= 1.3333
e= -0.08333
error_1= ((a*exp(x-2*dx)*cos(x-2*dx)+b*exp(x-dx)*cos(x-dx)+c*exp(x)*cos(x)+d*exp(x+dx)*cos(x+dx)+e*exp(x+2*dx)*cos(x+2*dx)))/(dx)^2;
%errors in fourth order central diference approixmation
error_fcda =abs(error_1-anatical_difference)
end
function [error_2]=numerical_function1(x,dx)
%second order differenece
anatical_difference= -2*exp(x)*sin(x)
%fourth order skewed right side approixmation
%f"(x)=(g*f(i))+(h*f(i+1)+l*f(i+2)+j*f(i+3)+k*f(i+4)+m*f(i+5))/(dx)^2
%g,h,k,l,m are coefficent of approximation in taylors equation
g=3.75
h=-12.3
j=17.83
k=-13.00
l=5.08
m=-0.800
error_2 =(g*(exp(x)*cos(x)))+(h*(exp(x+dx)*cos(x+dx)))+(j*(exp(x+2*dx)*cos(x+2*dx)))+(k*(exp(x+3*dx)*cos(x+3*dx)))+(l*(exp(x+4*dx)*cos(x+4*dx))+(m*(exp(x+5*dx)*cos(x+5*dx)))/(dx)^2);
%error in skewed_right_side_approixmation
error_fsra=abs(error_2-anatical_difference)
end
function [error_3]=numerical_function3(x,dx)
%second order differenece
anatical_difference =-2*exp(x)*sin(x)
%fourth order skewed right side approixmation
%f(x)=p*f(i-5)+q*f(i-4)+r*f(i-3)+s*f(i-2)+t*f(i-1)+u*f(i)/(dx)^2
%n,p,q,r,s,t are coefficent of approximation in taylors equation
n=-0.8333
p=5.088
q=-13.000
r=17.800
s=-12.800
t=3.7500
%by using talyors equation
error_3=((n*exp(x-5*dx)*cos(x-5*dx))+(p*exp(x-4*dx)*cos(x-4*dx))+(q*(exp(x-3*dx)*cos(x-3*dx)))+(r*(exp(x-2*dx)*cos(x-2*dx)))+(s*(exp(x-dx)*cos(x-dx)))+(t*(exp(x)*cos(x))))/(dx)^2
%error finding the equation
error_fsla = abs(error_3-anatical_difference)
end
%program_on_the_fourth_order_derivation
%inputs
%functionf(x)=exp(x)*cos(x)
x=pi/3;
%discretization of size x
dx=linspace(pi/4000,pi/40,20);
%error in numerical discretizatio for the first,second and fourth order
%approixmation
for i= 1:length(dx)
error_fcda=numerical_function(x,dx(i));
error_fsra=numerical_function1(x,dx(i));
error_fsla=numerical_function3(x,dx(i));
end
%%plotting for the graph dx vs errors
loglog(dx,error_fcda,'b-*')
hold on
loglog(dx,error_fsra,'g-*')
hold on
loglog(dx,error_fsla,'r-*')
hold off
xlabel('range of dx')
ylabel('error in approximation')
legend('first order','second order','fourth order')
title('error for differentization')
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.