I get a problem as $z = \sqrt{x^2 + y^2}$, $x = 1e - 160$, $y = 2e - 160$. It prompts me to think a improved algorithm in Matlab which gives full machine accuracy without using hypot. Can anyone help me with that?
how to find full machine error without using hypot in Matlab
1
$\begingroup$
arithmetic
matlab
2 Answers
1
Without loss of generality (otherwise use absolute values and/or swap), let $ y \ge x \ge 0$. Then $$z=y \sqrt{ 1 + \left (\frac{x}{y}\right)^2}$$ In your case $$z=2\times 10^{-160}\sqrt{1+(1/2)^2}=2\times 10^{-160}\sqrt{5/4}=\sqrt{5}\times 10^{-160}$$ $$z\approx 2.23606797749979 \times 10^{-160}$$
-
0Thanks for you help. But can i pull out x instead of y? – 2017-02-13
-
0so when 0>y>x, I just need to add abs when I pull out the y to make sure I get a positive sigh for the equation. – 2017-02-13
-
0sry it might be a stupid question, but i just want to make sure i understand your saying. – 2017-02-13
0
$$z^2=x^2+y^2=(10^{-160})^2+(2*10^{-160})^2=(10^{-320})+4*(10^{-320})=5*(10^{-320})$$
Thus, taking the square root:
$$z=\sqrt{5}*10^{-160}$$
In this way, you can express your result while remaining in the same magnitude ($-160$) as components $x$ and $y$ instead of needing to go down to powers $-320$. In between, you may have the machine epsilon (https://en.wikipedia.org/wiki/Machine_epsilon).
-
0Corrected an error... – 2017-02-13
-
0Yeah. I thought this and it comes with a machine epsilon which I can't have in the assignment. – 2017-02-13