How to choose a sensible tolerance equivalent to 10 significant digits.

7 views (last 30 days)
By using a function called FixedPoint to compute 10 iterations for g(x) and h(x).
function [x, flag] = FixedPoint(g, x0, tol, nmax)
x(1) = x0; flag = 1;
for n = 1:nmax
x(n+1) = g(x(n));
if ( abs(x(n+1)-x(n)) <= tol*max(abs(x(n)), abs(x(n+1))))
flag = 0;
break end
end
x = x(:);
g = @(x) 1/(x^2 - 1);
[x, flag] = FixedPoint(g, 1.5, 1e-10, 10)
h = @(x) 1 + x/(x^2 + x + 1);
[x, flag] = FixedPoint(h, 1.5, 1e-10, 10)
I'm confused of what number to choose for tolerance to give 10 significant digits. I tried 1e-10.
  2 Comments
John D'Errico
John D'Errico on 2 Apr 2015
See how when I edited your post to make the code readable, how much it helps?
Please, when you post code like this, all you need do is select it, then click on the "{} Code} button.
John D'Errico
John D'Errico on 2 Apr 2015
Also, you will find it necessary to use the ./, .*, and .^ operators much of the time. I would strongly suggest learning how to use them, when, and why.
These operators are used whenever you will do element-wise operations on a matrix or vector.
g = @(x) 1./(x.^2 - 1);
h = @(x) 1 + x./(x.^2 + x + 1);

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 2 Apr 2015
Edited: John D'Errico on 2 Apr 2015
So you want 10 SIGNIFICANT digits. Wanting to meet a tolerance is good.
But what happens if the algorithm has not converged in 10 iterations? After all, you specified nmax=10 iterations. What happens if the algorithm is divergent for some set of starting values, or even for all starting values?
Under those conditions, can you rationally expect the tolerance to necessarily be met?
Essentially this fixed point iteration requires two things for that tolerance to have a chance.
1. It must be convergent.
2. Even if it is convergent, it must have converged by the iteration limit you have posed, and 10 iterations is not a big number here.
So think about these issues. Then look at the actual sequence the fixed point scheme took. Think about what you see. Did the function g converge? Should it have converged in that number of iterations? Why did it do what it did? I assume you have learned the circumstances that will allow a fixed point scheme like this to converge. Have they been met for g?
Then look at the iterations taken for h. What is different about h and g in the vicinity of the starting point?
When in doubt (and even when no doubt exists) follow John's rule: PLOT EVERYTHING. Then think about what you see.
ezplot(g),hold on, ezplot(@(x) x)
axis equal
ezplot(h),hold on, ezplot(@(x) x)
axis equal
  5 Comments
XIN JIN
XIN JIN on 2 Apr 2015
g'(x)=-2x/((x^2-1)^2),g'(x)<0, so it diverges. Any tolerance won't have an effect on g? After running what I wrote for g, i got
John D'Errico
John D'Errico on 2 Apr 2015
Edited: John D'Errico on 3 Apr 2015
Ok, so do you think that has converged? I think you miss the scientific notation there - that tiny little factor of 1e5 in front of the vector, multiplying every element. Using a large font does not prove anything, only that you need to think about what it is that is happening.
Perhaps you need to do some reading about the format used to display numbers.
help format
NO TOLERANCE WILL MAKE A DIVERGENT SEQUENCE CONVERGE. PERIOD. How many times can I say this?
Changing the tolerance has NO impact on the sequence. Only when the iterations stop.
The sequence for g is divergent. It generates what is essentially arbitrary numerical garbage, a sequence of values that are not convergent to the solution.
As for your statement about g'(x) being negative, that does NOT make it diverge.
The important factor for convergence is that abs(g'(x)) must not exceed 1 in that region.
I think you need to do some reading about fixed point iteration, convergence of that scheme, and about the format command in MATLAB.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!