Project

Taylor table method and Matlab code

AIM : To write a program in Matlab to evaluate the second order derivative of the analytical function exp(x)*cos(x) and compare it with the 3 numerical approximations.

PROCEDURE :

Derivation for Central Difference , Skewed Left and Right are solved and we get the values for co-efficients using MATLAB.

clear all
close all
clc

%analytical
x = pi/3;
f_x = -2*exp(x)*sin(x);

%calc. co-effs
A1 = [1 1 1 1 1; -2 -1 0 1 2; 2 0.5 0 0.5 2; -4/3 -1/6 0 1/6 4/3; 2/3 1/24 0 1/24 2/3];
B1 = [0; 0; 1; 0; 0];
C1 = A1\\B1;

A2 = [1 1 1 1 1; 0 1 2 3 4; 0 0.5 2 9/2 8; 0 1/6 4/3 9/2 32/3; 0 1/24 2/3 27/8 32/3];
B2 = [0; 0; 1; 0; 0];
C2 = A2\\B2;

A3 = [1 1 1 1 1; 0 -1 -2 -3 -4; 0 0.5 2 9/2 8; 0 -1/6 -4/3 -9/2 -32/3; 0 1/24 2/3 27/8 32/3]
B3 = [0; 0; 1; 0; 0];
C3 = A3\\B3;

%dx
dx = linspace(pi/3,pi/3000,20);

%error
for i = 1:length(dx)
    cds(i) = (C1(1)*exp(x-2*dx(i))*cos(x-2*dx(i))+C1(2)*exp(x-1*dx(i))*cos(x-1*dx(i))+C1(3)*exp(x)*cos(x)+C1(4)*exp(x+1*dx(i))*cos(x+1*dx(i))+C1(5)*exp(x+2*dx(i))*cos(x+2*dx(i)))/(dx(i)^2)
    rs(i) = (C2(1)*exp(x)*cos(x)+ C2(2)*exp(x+1*dx(i))*cos(x+1*dx(i))+C2(3)*exp(x+2*dx(i))*cos(x+2*dx(i))+C2(4)*exp(x+3*dx(i))*cos(x+3*dx(i))+C2(5)*exp(x+4*dx(i))*cos(x+4*dx(i)))/(dx(i)^2)
    ls(i) = (C3(1)*exp(x)*cos(x)+ C3(2)*exp(x-1*dx(i))*cos(x-1*dx(i))+C3(3)*exp(x-2*dx(i))*cos(x-2*dx(i))+C3(4)*exp(x-3*dx(i))*cos(x-3*dx(i))+C3(5)*exp(x-4*dx(i))*cos(x-4*dx(i)))/(dx(i)^2)
    bd(i) = (exp(x)*cos(x)-exp(x-dx(i))*cos(x-dx(i)))/(dx(i));
    fd(i) = (exp(x+dx(i))*cos(x+dx(i))-exp(x)*cos(x))/(dx(i));
    
    cde(i) = abs(cds(i)-f_x);
    rse(i) = abs(rs(i)-f_x);
    lse(i) = abs(ls(i)-f_x);
    bde(i) = abs(bd(i)-f_x);
    fde(i) = abs(fd(i)-f_x); 
end

loglog(dx,cde)
hold on
loglog(dx,rse)
hold on
loglog(dx,lse)
hold on
loglog(dx,bde)
hold on
loglog(dx,fde)
legend(\'Central Diff\',\'Right skewed\',\'Left skewed\',\'Backward diff\',\'Forward diff\')
title(\'Error comparison\')
xlabel(\'dx\')
ylabel(\'Error\')




\"\"

BDS & FDS COMPARISON:

When BDS and FDS are compared to other curves , we can clearly see the error is high in them .The input is less in FDS & BDS when compared to other schemes. The error drops in FDS as the value of dx inreases while for BDS, it remains hugely unchanged. In order to minimize those errors , we need to have finer grid spaces.

SKEWED SCHEME :

If we want to find fourth order approximations and we have 5 points on the both left and right hand side then the skewed scheme is useful .But when these points are not available , we use central difference scheme.

CONCLUSIONS : Program in MATLAB has been written and the graphs are obtained.

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.