Taylor series forward, backward and central difference using MATLAB
The Taylor series can be used to calculate the value of an entire function at every point, if the value of the function, and of all of its derivatives, are known at a single point.
clear all
close all
clc
% Defining the function
poly_p = [3 5 7];
% Derivating the function theoritically
p_der = polyder(poly_p);
p_der_eval = polyval(p_der,0);
% Finite difference method - Forward difference method
x_0 = 0;
h = 0.1;
derivative_forward_difference_first_derivative = (polyval(poly_p, x_0+h) - polyval(poly_p,x_0))/h
% Finite difference method - Backward difference method
x_0 = 0;
h = 0.2;
derivative_backward_difference_first_derivative = (polyval(poly_p,x_0) - polyval(poly_p,x_0-h))/h
% Finite difference method - Central difference method
x_0 = 0;
h = 0.2;
derivative_central_difference_first_derivative = (polyval(poly_p,x_0+h) - polyval(poly_p,x_0-h))/(2*h)
Results
poly_p =
3 5 7
p_der =
6 5
p_der_eval =
5
derivative_forward_difference_first_derivative =
5.3000
derivative_backward_difference_first_derivative =
4.4000
derivative_central_difference_first_derivative =
5.0000