0
$\begingroup$

I am trying to evaluate $h(z)=\frac{f(z)}{g(z)}$ at points $z$ close to 0. Here I have taken an example value of $z=\texttt{z1}=10^{-7}$ for demonstration. I notice that evaluating the fraction before simplification In[4] gives a different answer than what I get from forcing the equation to first simplify and then completing the evaluation (using substitute in In[5]). This looks like a precision issue as the difference in the results increases as the value for $\texttt{z1}$ gets closer to 0. Is there a way to enforce double precision (as in Fortran and C) for computations performed in different orders, to produce same numerical results? Or does the issue lie elsewhere?

In[1]:= f[z_] := -(1/64) (-Sin[z] + 3 Sin[3 z])^2 - 1/6 Cos[z] Sin[z] (Sin[2 z] - 2 Sin[4 z]);

In[2]:= g[z_] := -Cos[z]^2 Sin[z]^2 + 1/8 Sin[z] (-Sin[z] + 3 Sin[3 z]);

In[3]:= h[z_] := Simplify[f[z]/g[z]]

In[4]:= z1 = 0.0000001; NumberForm[h[z1], 16]

Out[4]//NumberForm= 1.4193548387096775

In[5]:= NumberForm[h[z] /. z -> z1, 16]

Out[5]//NumberForm= 1.333333333333325

This question comes in the wake of another related question I posted here. It seems that simplification of all terms (in my original problem) is time taking. It would be more optimal to compute them at points close to 0, with good precision so that the results would match. I have started this question as this problem seems related to the older post, but seems to me, to reside in the domain of precision issues.

$\endgroup$
0

1 Answer 1

2
$\begingroup$

This is a precision problem. First simplifying will give an more accurate result. However, if you change z1 to an exact number, you will get an exact result

z1= 10^-7;    
NumberForm[h[z1], 16]

enter image description here

You may convert the exact result to a machine number and verify that both of your calculations give the same result up to machine precision:

NumberForm[h[z1], 16] // N
NumberForm[h[z] /. z -> z1, 16] // N

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.