Project

TAYLOR TABLE METHOD FOR HIGHER ORDER APPROXIMATION

\"\"

\"\"

\"\"

\"\"

\"\"

\"\"

\"\"

\"\"

Plots :

1. Comparision of CDS, SLDS, SRDS

\"\"

2. Comparision of SDS,SLDS,SRDS,FD and BD

\"\"

\"\"

II. Matlab program

Main script

    clear all 
    close all
    clc
    % calculating the derivative of function
    %% given function, f(x)=exp(x).sin(x);
    % f\'\'(x)=-2.exp(x).sin(x);
    
    %% assuming x=pi/3
    x=pi/3;
    
    %% Definig the grid size
    dx=linspace(pi/10,pi/500,15);
    
    analytical_derivative=-2*exp(x)*sin(x);
    
    %% calculating error
    for i=1:length(dx)
      error_central(i)=central_approximation(x,dx(i));
      error_right(i)=right_skewed_approximation(x,dx(i));
      error_left(i)=left_skewed_approximation(x,dx(i));
      error_forward(i)=forward_difference(x,dx(i));
      error_backward(i)=backward_difference(x,dx(i));
      
    end
    
    figure(1)
    loglog(dx,error_central,\'color\',\'r\');
    loglog(dx,error_right,\'color\',\'y\');
    loglog(dx,error_left,\'color\',\'g\');
    
    xlabel(\'dx\')
    ylabel(\'trunction_error\')
    title(\'comparision between different schemes for a second order derivative of 4th order accuracy\')
    legend(\'central difference\',\'skewed right sided difference\',\'skewed left sided difference\')
    %% Figure(2)
    figure(2)
    loglog(dx,error_central);
    hold on
    loglog(dx,error_right);
    hold on
    loglog(dx,error_left);  
   hold on
  loglog(dx,error_forward); 
  hold on
  loglog(dx,error_backward);
  hold on
  xlabel(\'truncation Error\')
  title(\'second order derivative of 4th order accuracy vs FD vs BD\')
  legend(\'CD\',\'SRSD\',\'SLSD\',\'Forward difference\',\'Backward difference\')
  
  
      

2. For central approximation

function out = central_approximation (x,dx)
  % analytical function=exp(x)*sin(x);
  f=@(x) exp(x)*cos(x);
   % calculation of arbitary constants (a,b,c,d,e) for numerical derivative 
   syms a b c d e
   eqn1 = a+b+c+d+e==0;
   eqn2 = -2*a-b+0+d+2*e==0;
   eqn3 = 2*a+b/2+0+d/2+2*e==1;
   eqn4 = -8*a/6-b/6+0+d/6+8*e/6==0;
   eqn5 = 16*a/24+b/24+0+d/24+16*e/24==0;
   
   % solving Equation to calculating constants
   X = solve([eqn1, eqn2, eqn3, eqn4, eqn5],[a,b,c,d,e]);
   % Exracting constants
   a = x.a;
   b = x.b;
   c = x.c;
   d = x.d;
   e = x.e;
   
   % numerical approximation
   central_differencing = (a*f(x-2*dx)+b*f(x-dx)+c*f(x+dx)+e*f(x+2*dx))/(dx^2);
   
   out = abs(central_differencing - analytical_derivative);
   

endfunction

3. For right skewed approximation

function out = right_skewed_approximation (x,dx)
% analytical function = exp(x)*cos(x);
  % f\"(x) = -2*exp(x)*sin(x);
  analytical_derivative = -2*exp(x)*sin(x);
  
  f = @(x) exp(x)*cos(x);
  % calculating of arbitary constants (a,b,c,d,e) for numerical derivative 
  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;
  
  % solving Equation to calculate constants
  X =solv([eqn1,eqn2,eqn3,eqn4,eqn5],[a,b,c,d,e]);
  
  %Extracting constants
  a =x.a;
  b =x.b;
  c =x.c;
  d =x.d;
  e =x.e;
  
  % numerical approximation
  right_skewed_approximation_defferencing =(a*f(x-4*dx)+b*f(x-3*dx)+c*f(x-2*dx)+d*f(x-dx)+e*f(x))/(dx^2);
  out = abs(right_skewed_defferencing-analytical_derivative)
endfunction

4. For left skewed approximation

function out = left_skewed_approximation (x,dx)
  % analytical function = exp(x)*cos(x);
  % f\"(x) = -2*exp(x)*sin(x);
  analytical_derivative = -2*exp(x)*sin(x);
  
  f = @(x) exp(x)*cos(x);
  % calculating of arbitary constants (a,b,c,d,e) for numerical derivative 
  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-81*c/6-d/6==0;
  eqn5 = 256*a/24+81*b/24+16*c/24+d/24==0;
  
  % solving Equation to calculate constants
  X =solv([eqn1,eqn2,eqn3,eqn4,eqn5],[a,b,c,d,e]);
  
  %Extracting constants
  a =x.a;
  b =x.b;
  c =x.c;
  d =x.d;
  e =x.e;
  
  % numerical approximation
  left_skewed_approximation_defferencing =(a*f(x-4*dx)+b*f(x-3*dx)+c*f(x-2*dx)+d*f(x-dx)+e*f(x))/(dx^2);
  out = abs(left_skewed_defferencing-analytical_derivative);
  

endfunction

5. For forward difference

function out = forward_difference (x,dx)
  %%Analytical value
  analytical=-2*exp(x)*sin(x);
  %%Forward difference
  forward=(exp(x+dx)*cos(x+dx)-exp(x)*cos(x))/dx;
  %Trunction error
  out=abs(forward-analytical);
  

endfunction

6. For backward difference

function out = backward_defference (x,dx)
   %%Analytical value
  analytical=-2*exp(x)*sin(x);
  %%Forward difference
  backward=(exp(x)*cos(x)-exp(x-*dx)*cos(x-dx))/dx;
  %Trunction error
  out=abs(backward-analytical)

endfunction
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.