1

enter image description here


As you can see here, the question is about part b. By using Matlab, the answer to the part a is -2.4, but by using "format long" to compute directly, the answer is -2.401923018799901, which I don't think the cancellation is catastrophic.

Bty, I tried to put rewrite it to 1/x - sqrt(1/x^2 + 5/x), while the answer is -2.4 as well.

I am also not confident with my rewriting, please show me if you have any better rewritings.

Thanks a lot.

JasonHu
  • 71
  • 1
    In "base 10, precision 2 floating point" we don't get $-2.401923018799901$ but rather $-2.35$ (note that all computations should use only 2 significant figures)! – Winther Sep 16 '14 at 22:51
  • @Winther Thanks for your comment. When I do this in Matlab, I used digits(2) and then used vpa() to perform "precision 2 floating point". Could you tell me how to do it correctly in Matlab? thanks – JasonHu Sep 16 '14 at 22:57
  • I don't know, but it's easy enough to do by hand: $1+5x = 1.17$ and $\sqrt{1.17} = 1.08167 \to 1.08$ so $1-\sqrt{1+5x} = -0.08$ and finally $(1-\sqrt{1+5x})/x=-0.08/0.034 = -2.35294 \to -2.35$. – Winther Sep 16 '14 at 23:01
  • @Winther Thank you, I'll try to figure it out. Really thank you for your calculation by hand. – JasonHu Sep 16 '14 at 23:03
  • btw about significant figures see here. So I think you might need digits(3) in MatLab?! – Winther Sep 16 '14 at 23:03
  • Thanks, I was thinking that is 2.35 precision 2? Precision is significant figures if I got it right and with 2 significant figures, is 2.35 2.4? @Winther – JasonHu Sep 16 '14 at 23:14
  • @Winther: Note that, as the Wikipedia article you link to implies, leading nonzero digits are included in the counting of significant figures. – hardmath Sep 16 '14 at 23:14
  • @Winther Yeah, and I think for your answer "-2.35", the first digit is 2, second is 3 and you have one more digit which is 5. – JasonHu Sep 16 '14 at 23:22
  • @Winther Thanks for your comment, please see answer by @ hardmath – JasonHu Sep 16 '14 at 23:29
  • @hardmath Yes of course, thanks for correcting it. 20 years of education I and still can't get the significant figures right! Someone should have to resign for this...and it should be me:) – Winther Sep 16 '14 at 23:47
  • @Winther: You aren't getting off that easily! Your comment was the first to point out the misunderstanding of fp precision, so for that the community thanks you. Beatings will continue until morale improves. – hardmath Sep 17 '14 at 00:10

3 Answers3

1

One way to rewrite the expression is to multiply by the conjugate:

$$\frac{1 - \sqrt{1 + 5x}}{x} \cdot \frac{1 + \sqrt{1 + 5x}}{1 + \sqrt{1 + 5x}} = \frac{1 - (1 + 5x)}{x(1 + \sqrt{1 + 5x})} = -\frac{5}{1 + \sqrt{1 + 5x}}$$

For this second expression, when $x \sim 0$ the denominator is $\sim 2$, and you get a number close to $-5/2$. In the original expression when $x \sim 0$ both the numerator and denominator are close to zero so you could get problems.

But it doesn't actually seem like you got any problems in your computation, so...?

Alex Zorn
  • 3,582
0

The trick here is to avoid the subtraction cancellation through multiplying numerator and denominator by the conjugate radical:

$$ \frac{1 - \sqrt{1+5x}}{x} = \frac{1 - (1+5x)}{x(1+\sqrt{1+5x})} = \frac{-5}{1+\sqrt{1+5x}} $$

Give that a try.


Here's what happens if you evaluate the original expression in 2 decimal digit floating point precision:

$$ x = 0.034 $$

$$ 5x \approx 0.17 $$

$$ 1 + 5x \approx 1.2 $$

$$ \sqrt{1+5x} \approx 1.1 $$

$$ 1 - \sqrt{1+5x} \approx -0.1 \; \text{ NB: subtractive cancellation }$$

$$ \frac{1 - \sqrt{1+5x}}{x} \approx -2.9 $$

So there's a significant loss of precision in doing it this way.

hardmath
  • 37,715
  • Thank you for your answer and with precision 2, the answer is -2.4 as well, and after part a and part b, the part c of this question is to compute relative errors in previous two parts but in this case, it's meaningless. Are there anything wrong with my performance? Thanks – JasonHu Sep 16 '14 at 22:53
  • @JasonHu: If you compute the intermediate expressions with two significant digits, you will not get -2.4. Perhaps you are computing the expression with high precision intermediate values, then doing one final rounding at the end. That's not what the exercise calls for. – hardmath Sep 16 '14 at 23:04
  • Do you know how to process this in Matlab? I used vpa() function with digits(2) and I think vpa do the precision 2 for every calculation it contains, not just the final answer. – JasonHu Sep 16 '14 at 23:21
  • Then write them out on separate lines, storing in separate variables if need be, and see if the Answer is the same. Or do it by hand on a sheet of paper, as I just did, and be done with it. – hardmath Sep 16 '14 at 23:23
  • I did it step by step and you are right, the answer is -2.9. Thanks. Do you know any appropriate function to use in matlab? – JasonHu Sep 16 '14 at 23:28
  • I think I have Octave installed on this laptop. Let me take a look. – hardmath Sep 16 '14 at 23:34
  • I've looked through my Octave documentation (ver. 3.6.1, not quite the latest), but functions for rounding seem to be integer outputs only (more useful for fixed point than floating point simulation) and for display width purposes (output_precision). The documentation is pretty clear that floating point numeric values are stored internally as double precision, so I'd expect an expression involving floating point numbers to convert all intermediate results to at least double precision before returning a result. – hardmath Sep 17 '14 at 00:07
  • Thank you a lot for your patience. Hope you'll have a great night. :) – JasonHu Sep 17 '14 at 00:25
0

I think the right rewriting is

$-\frac{5}{1+\sqrt{1+5x}} $

obtained by multiplication for $1+\sqrt{1+5x}$.

You usually have a cancellation with the sign minus when the result is very close to zero, so it's convenient use a rewriting where it doesn't appear

Exodd
  • 12,241
  • 2
  • 27
  • 44