Challenge

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

CODE:

clear all
clc
close all
x=pi/3;
n=20;
dx=linspace(pi/4000,pi/40,n);
analytical_solution=(exp(x)*cos(x)-sin(x)*exp(x));
a=exp(x)*cos(x);
for i=1:20
b=cos(x+dx(i))*exp(x+dx(i));
c=cos(x-dx(i))*exp(x-dx(i));
d=cos(x+2*dx(i))*exp(x+2*dx(i));
e=cos(x-2*dx(i))*exp(x-2*dx(i));
g=cos(x+3*dx(i))*exp(x+3*dx(i));
h=cos(x+4*dx(i))*exp(x+4*dx(i));
j=cos(x+5*dx(i))*exp(x+5*dx(i));
k=cos(x-3*dx(i))*exp(x-3*dx(i));
l=cos(x-4*dx(i))*exp(x-4*dx(i));
m=cos(x-5*dx(i))*exp(x-5*dx(i));
right_skewed_approach(i)=3.7500*a-12.8333*b+17.8333*d-13.0000*g+5.0833*h-0.0833*j;
left_skewed_approach(i)=3.7500*a-12.8333*c+17.8333*e-13.0000*k+5.0833*l-0.0833*m;
central_differencing_approach(i)=-2.5000*a+1.3333*c-0.0833*e+1.3333*b-0.0833*d;
right_skewed_approacherror(i)=abs(right_skewed_approach(i)-analytical_solution);
left_skewed_approacherror(i)=abs(left_skewed_approach(i)-analytical_solution);
central_differencing_approacherror(i)=abs(central_differencing_approach(i)-analytical_solution);
end
right_skewed_approach
left_skewed_approach
central_differencing_approach
right_skewed_approacherror
left_skewed_approacherror
central_differencing_approacherror
figure(1)
plot(dx,right_skewed_approacherror,'r','marker','x')
xlabel('dx')
ylabel('differentapproaches')
hold on
plot(dx,left_skewed_approacherror,'b','Marker','o')
hold on
plot(dx,central_differencing_approacherror,'k','Marker','+')
legend('right_skewed_approacherror','left_skewed_approacherror','central_skewed_approacherror')
figure(2)
loglog(dx,right_skewed_approacherror,'r','marker','x')
xlabel('dx')
ylabel('differentapproaches')
hold on
loglog(dx,left_skewed_approacherror,'b','Marker','o')
hold on
loglog(dx,central_differencing_approacherror,'k','Marker','+')
legend('right_skewed_approacherror','left_skewed_approacherror','central_skewed_approacherror')
SOLUTION:

right_skewed_approach =

Columns 1 through 10

1.0655 1.0483 1.0293 1.0085 0.9858 0.9612 0.9346 0.9060 0.8752 0.8423

Columns 11 through 20

0.8072 0.7699 0.7302 0.6882 0.6437 0.5967 0.5472 0.4950 0.4402 0.3827


left_skewed_approach =

Columns 1 through 10

1.0717 1.0865 1.0997 1.1112 1.1213 1.1298 1.1368 1.1424 1.1466 1.1495

Columns 11 through 20

1.1512 1.1515 1.1507 1.1487 1.1456 1.1414 1.1361 1.1299 1.1226 1.1145


central_differencing_approach =

Columns 1 through 10

-0.0000 -0.0001 -0.0004 -0.0008 -0.0015 -0.0022 -0.0032 -0.0043 -0.0055 -0.0070

Columns 11 through 20

-0.0086 -0.0104 -0.0123 -0.0144 -0.0167 -0.0191 -0.0217 -0.0244 -0.0274 -0.0304


right_skewed_approacherror =

Columns 1 through 10

2.1086 2.0914 2.0724 2.0516 2.0289 2.0043 1.9776 1.9490 1.9183 1.8854

Columns 11 through 20

1.8503 1.8129 1.7733 1.7312 1.6867 1.6398 1.5902 1.5381 1.4833 1.4257


left_skewed_approacherror =

Columns 1 through 10

2.1147 2.1295 2.1427 2.1543 2.1643 2.1728 2.1798 2.1854 2.1897 2.1926

Columns 11 through 20

2.1942 2.1946 2.1937 2.1917 2.1886 2.1844 2.1792 2.1729 2.1657 2.1575


central_differencing_approacherror =

Columns 1 through 10

1.0430 1.0429 1.0426 1.0422 1.0416 1.0408 1.0399 1.0388 1.0375 1.0361

Columns 11 through 20

1.0345 1.0327 1.0308 1.0287 1.0264 1.0240 1.0214 1.0186 1.0157 1.0126

>>VALUES OF COEFFIECENTS CODE

A=[1 1 1 1 1 1;0 -1 -2 -3 -4 -5;0 1/2 4/2 9/2 16/2 25/2;0 -1/6 -8/6 -27/6 -64/6 -125/6;0 1/24 16/24 81/24 256/24 625/24;0 -1/120 -32/120 -243/120 -1024/120 -3125/120];
syms a b c d e g
X=[a;b;c;d;e;g];
B=[0;0;1;0;0;0];
B1=[0;0;1;0;0];
A*X==B
C=[1 1 1 1 1 1;0 1 2 3 4 5;0 1/2 4/2 9/2 16/2 25/2;0 1/6 8/6 27/6 64/6 125/6;0 1/24 16/24 81/24 256/24 625/24;0 1/120 32/120 243/120 1024/120 3125/120];
X=inv(A)*B
v=inv(C)*B
D=[1 1 1 1 1 ;0 -1 -2 1 2;0 1/2 4/2 1/2 4/2 ;0 -1/6 -8/6 1/6 8/6;0 1/24 16/24 1/24 16/24];
x=inv(D)*B1

SOLUTION:

>> taylortable1

ans =

a + b + c + d + e + g == 0
- b - 2*c - 3*d - 4*e - 5*g == 0
b/2 + 2*c + (9*d)/2 + 8*e + (25*g)/2 == 1
- b/6 - (4*c)/3 - (9*d)/2 - (32*e)/3 - (125*g)/6 == 0
b/24 + (2*c)/3 + (27*d)/8 + (32*e)/3 + (625*g)/24 == 0
- b/120 - (4*c)/15 - (81*d)/40 - (128*e)/15 - (625*g)/24 == 0

X =

3.7500
-12.8333
17.8333
-13.0000
5.0833
-0.8333


v =

3.7500
-12.8333
17.8333
-13.0000
5.0833
-0.8333


x =

-2.5000
1.3333
-0.0833
1.3333
-0.0833

>>

EXPLANATION:

Firstly, in order to proceed with the taylor table for skewed and central differencing we gennerally need to find the no of nodes depending on the approximation order and derivative order .

for skewed approcaoch no of nodes in the stencil=order of derivative + approximation order

4+2=6

for central differencig approach no of nodes in stencil=order of derivative + approximation order-1

4+2-1=5

the right skewed approach will be as follows

f^(||)(x)`= a*f(i)+b*f(i+1)+c*f(i+2)+d*f(i+3)+e*f(i+4)+g*f(i+5)

the right skewed approach will be as follows

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

the right skewed approach will be as follows

`f^(||)(x)`= a*f(i)+b*f(i-1)+c*f(i-2)+d*f(i+1)+e*f(i+2)`

then after using the taylor table found six equations with six unknowns for left and right skewed approach .and solved them in matlab using the inverse command and the values i,e coefficients of left or right skewed approach are

3.7500
-12.8333
17.8333
-13.0000
5.0833
-0.8333

both have the same coefficents

and in case of central differencing approach five equations with 5 unknowns were found using the taylor table and solved them in matlab and the values are

-2.5000
1.3333
-0.0833
1.3333
-0.0833

after finding these coefficients submitted in the above equations and then dx is generated by using the linspace command it divided the pi/400 and pi/40 into 20 equal parts and we are finding the double derivative of fourth order approximation for 20 different dx values .in order to proceed before we are finding the analytical solution manually and giving input values in matlab and the it can be used to find the absolute difference error at the end.actually the skwed approaches and central differenecing approcach has many terms in it right i made those into variables such as

b=cos(x+dx(i))*exp(x+dx(i));-f(i+1)
c=cos(x-dx(i))*exp(x-dx(i));-f(i-1)
d=cos(x+2*dx(i))*exp(x+2*dx(i));-f(i+2)
e=cos(x-2*dx(i))*exp(x-2*dx(i));-f(i-2)
g=cos(x+3*dx(i))*exp(x+3*dx(i));-f(i+3)
h=cos(x+4*dx(i))*exp(x+4*dx(i));-f(i+4)
j=cos(x+5*dx(i))*exp(x+5*dx(i));-f(i+5)
k=cos(x-3*dx(i))*exp(x-3*dx(i));-f(i-3)
l=cos(x-4*dx(i))*exp(x-4*dx(i));-f(i-4)
m=cos(x-5*dx(i))*exp(x-5*dx(i));-f(i-5)
BEFORE MAKING THIS A FOR LOOP WAS INITIATED IN CHANGE THE VALUES OF dx after every iteration in such a way for 20 dx values were a double derivative at fourthorder approximation has been generated.after assiging the terms to particular variables and the particular approach is done using the coefficients and assigned variables in such a way as follows
right_skewed_approach(i)=3.7500*a-12.8333*b+17.8333*d-13.0000*g+5.0833*h-0.0833*j;
left_skewed_approach(i)=3.7500*a-12.8333*c+17.8333*e-13.0000*k+5.0833*l-0.0833*m;
central_differencing_approach(i)=-2.5000*a+1.3333*c-0.0833*e+1.3333*b-0.0833*d;
generally (i) command generally stores the values of a particular approach at the ith position and it generates 1x20 array
then after using the absolute difference command error has been found by differencing the particular approach and the analytical solution and stored as a 1x20 array which is as follows
right_skewed_approacherror(i)=abs(right_skewed_approach(i)-analytical_solution);
left_skewed_approacherror(i)=abs(left_skewed_approach(i)-analytical_solution);
central_differencing_approacherror(i)=abs(central_differencing_approach(i)-analytical_solution
thenafter a graph has been plotted between dx and the the particular approach followed which is as follows
figure(1)
plot(dx,right_skewed_approacherror,'r','marker','x')
xlabel('dx')
ylabel('differentapproaches')
hold on
plot(dx,left_skewed_approacherror,'b','Marker','o')
hold on
plot(dx,central_differencing_approacherror,'k','Marker','+')
legend('right_skewed_approacherror','left_skewed_approacherror','central_skewed_approacherror')
in order to visual the graph clesrly the loglog plot has been used the x coordinates are log(dx) and y coordinaes log(particular_approach_error)
WHY IS SKEWED APPROACH IS BETTER TO CENTRAL DIFFERENCING BECAUSE
generally in the central differencing approach we take values from all the neighbourhoods i,e, left,right,top and bottom.for suppose there is a mesh and we are at the staring nodal point and there are no points before then central differencing doesnot works because it takes values from both sides where as in case of skewed approach we prefer only side so that it works all the time .thats the advantage
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.