In: Advanced Math
Write a Matlab m-script to compute the backward difference approximation A = f(a)−f(a−h) h of the derivative T = f0(a) for f(x) = sin(x) and a = π/3 using each value of h in the sequence 2−n (n = 1,2,3,···,52).
MATLAB Code:
close all
clear
clc
f = @(x) sin(x);
a = pi/3;
fprintf('%-20s%-20s\n--------------------------------------------\n',
'h', 'Backward Diff., A')
for n = 1:52
h = 2^(-n);
back_diff = (f(a) - f(a - h)) / h;
fprintf('%-20.8e%-20.16f\n', h, back_diff)
end
Output: