Can anyone explain why my approximations break down at the iteration 15? Is this a memory issue? How does it really work?
Thanks.
4 pts =[ 0.5 , 0.9 , 0.99];
5
6 h = 0.001; % h needs to be small
7
8 % f(x)= 1.0 / (x.^2 - 3x +2)
9
10 f=@(x) ( (1.0)./(x.^2.0 - 3.0*x +2) );
11
12
13 for pt=1:size(pts, 2)
14 fprintf("Approximation of values approaching x= %f\n", pts(pt));
15 for n=1:20
16
17 % this is also called the forward difference approxiumation
18 f_approx = (f(h+ pts(1) )-f( pts(1)))/h;
19
20 num1 = (f(h+ pts(1) )-f( pts(1)))
21 num2 = (f(pts(1)+h ) - f( pts(1)-h))
22 % centered difference
23 c_approx = (f(pts(1)+h ) - f( pts(1)-h))/(2*h);
24 fprintf("(Iteration %f) At h=%.20f, Forward approximation: %.20f; Centered Approximation: %.20f\n",n, h, f_approx, c_approx);
25 h = h/10;
26 fprintf("===========\n\n");
27 end
28 h=0.001;
29 end
OUTPUT:
num1 = 3.5527e-14
num2 = 7.1054e-14
(Iteration 12.000000) At h=0.00000000000001000000, Forward approximation: 3.55271367880050048527; Centered Approximation: 3.55271367880050048527
===========
num1 = 3.9968e-15
num2 = 7.7716e-15
(Iteration 13.000000) At h=0.00000000000000100000, Forward approximation: 3.99680288865056310144; Centered Approximation: 3.88578058618804744739
===========
num1 = 8.8818e-16
num2 = 1.5543e-15
(Iteration 14.000000) At h=0.00000000000000010000, Forward approximation: 8.88178419700125054703; Centered Approximation: 7.77156117237609489479
===========
num1 = 0
num2 = 0
(Iteration 15.000000) At h=0.00000000000000001000, Forward approximation: 0.00000000000000000000; Centered Approximation: 0.00000000000000000000
===========
num1 = 0
num2 = 0
(Iteration 16.000000) At h=0.00000000000000000100, Forward approximation: 0.00000000000000000000; Centered Approximation: 0.00000000000000000000
===========
--