Problems with using small numbers in function computations
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Yakub Shalmiyev
am 16 Sep. 2018
Kommentiert: Walter Roberson
am 16 Sep. 2018
I'm trying to approximate the value of a function with use of Taylor and Maclaurin series.
My problem is that if the input to my function (the center at which I am approximating) is small, say 10E-8, then
I get issues with the calculation. As if Matlab neglects the small number value and treats it as zero.
I would like to understand how I can fix this.
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 16 Sep. 2018
Suppose you square the value, and add that to a value that is approximately 1 . And later you subtract 1 again. So like
(x^2 + 1) - 1
When abs(x) is less than roughly 1.5E-8, the result would not be x^2, but would instead be 0.
Binary floating point arithmetic has effectively 53 bits of precision. If you add two values in which one is less than about 2^-53 times the other, the result will be the larger value unchanged, because it would take more than 53 bits to represent the total. Then when you subtract off the original value, because the value was unchanged, you get a numeric 0, not the value that was added.
2 Kommentare
Walter Roberson
am 16 Sep. 2018
There are some tricks you can use:
- Use the symbolic toolbox. This can help in two ways: 1) Permits you to use more than the equivalent of 53 bits precision; and 2) in formula form, it would easily be able to find and cancel out the +1 and -1 leaving a numerically more accurate formula;
- instead of doing a straight addition, record the entries and sort them by absolute value, and add them in order of increasing absolute value so that the small values have a chance to accumulate significance. Especially if you do this in a recursive sense, that at any point you add the two values with smallest remaining absolute value, which might not always include the term that has had the most coefficients added to it so far
- Or use Jan's compensated sum file exchange contribution
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!