Challenge

Week 3 - Taylor table method and Matlab code

function [error_1,error_2,error_3,central_difference,skewed_right_difference,skewed_left_difference]=taylor_table(x,dx,analytical_derivative)
    
    exp_x=@(x)exp(x)*cos(x);
    
A_1=[1 1 1 1 1;-2 -1 0 1 2;4 1 0 1 4;-8 -1 0 1 8;16 1 0 1 16];
A_2=[1 1 1 1 1 1;0 1 2 3 4 5;0 1 4 9 16 25;0 1 8 27 64 125;0 1 16 81 256 625;0 1 32 243 1024 3125];
A_3=[1 1 1 1 1 1;0 -1 -2 -3 -4 -5;0 1 4 9 16 25;0 -1 -8 -27 -64 -125;0 1 16 81 256 625;0 -1 -32 -243 -1024 -3125];

B_1=[0; 0; 2; 0; 0];
B_2=[0; 0; 2; 0; 0; 0];

x_1= inv(A_1)*B_1;
x_2= inv(A_2)*B_2;
x_3= inv(A_3)*B_2;

rats(x_1);
rats(x_2);
rats(x_3);


a_1=x_1(1);
b_1=x_1(2);
c_1=x_1(3);
d_1=x_1(4);
e_1=x_1(5);

a_2=x_2(1);
b_2=x_2(2);
c_2=x_2(3);
d_2=x_2(4);
e_2=x_2(5);
g_2=x_2(6);
    
  
    
    
    central_difference= ((a_1*exp_x(x-2.*dx))+(b_1*exp_x(x-dx))+(c_1*exp_x(x))+(d_1*exp_x(x+dx))+(e_1*exp_x(x+2.*dx)))./(dx^2);
    
    skewed_right_difference= ((a_2*exp_x(x))+(b_2*exp_x(x+dx))+(c_2*exp_x(x+2.*dx))+(d_2*exp_x(x+3.*dx))+(e_2*exp_x(x+4.*dx))+(g_2*exp_x(x+5.*dx)))./(dx.^2);
    
    skewed_left_difference= ((a_2*exp_x(x))+(b_2*exp_x(x-dx))+(c_2*exp_x(x-2.*dx))+(d_2*exp_x(x-3.*dx))+(e_2*exp_x(x-4.*dx))+(g_2*exp_x(x-5.*dx)))./(dx.^2);
    
   
    error_1 = abs(central_difference-analytical_derivative);
    error_2 = abs(skewed_right_difference-analytical_derivative);
    error_3 = abs(skewed_left_difference-analytical_derivative);
    
end


close all
clear all
clc

x=pi/3;
dx=linspace(pi/30,pi/30000,20);
analytical_derivative= (-2*(exp(x))*(sin(x)));

for i=1:length(dx)
   
    [error_1(i),error_2(i),error_3(i),central_difference(i),skewed_right_difference(i),skewed_left_difference(i)]=taylor_table(x,dx(i),analytical_derivative);
  
end

loglog(dx,error_1,dx,error_2,dx,error_3)
legend('central','right','left')

As we see that the central differencing scheme error is less compared to the right and left scheme.Right and left scheme error rate is almost same.Also we see that the error is decreasing with the decrease with value of dx.But after a certain value of , the error increases with the deacrese int the dx value that is due to the round off error that occurs because some numbers in fact, most of them cannot be represented exactly in binary form specifically, fractional numbers that are not powers of two.

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.