Problems with using small numbers in function computations

2 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Walter Roberson
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
Yakub Shalmiyev
Yakub Shalmiyev am 16 Sep. 2018
Thanks for your response and the article, it was very interesting and helped me understand a bit more about precision in calculations.
After so much time, I understood the issue and was able to complete my task.
It turned out that my taylor series starts with one regardless of center. And that my function subtracts my result from the series FROM 1. So starting my series on the second term helped avoid the issue with adding/subtracting big numbers and small numbers.
I basically avoided the issue. But if my function wasn't set up that way, and I used a small center to compute cos(x), would that ever be possible? I understand something like that may not be necessary, especially after reading that NASA article, but still?
Walter Roberson
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by