1
$\begingroup$

I need to optimize an expression that involves a number of trigonometric functions and Exp[]. How do I make sure that all my calculations have an accuracy of 120-200 digits after the decimal point? This includes the accuracy of Exp[] and trig functions.

To get my point across, here is part of the equation:

z[x_, y_]:= Exp[Sin[60.0*x]] + Sin[50.0*Exp[y]]

Mathematica lets you control Precision of computations (which is total number of digits in the number) with two global variables: $MinPrecision and $MaxPrecision. However, I am not looking for precision.

$\endgroup$
  • 2
    $\begingroup$ Do you know that keeping expressions in terms of integers and rationals will keep exact values? Try NestList[16 # (1 - #)/3 &, 1/5, 4] ? Though for heavy computations you will lose speed. $\endgroup$ – Vitaliy Kaurov Jul 1 '12 at 17:53
  • $\begingroup$ I am aware that Mathematica will give me exact values for integer & rational calculations, but my calculations are far from exact. $\endgroup$ – newprint Jul 1 '12 at 18:00
  • $\begingroup$ It seems that N[expr, {Infinity, accuracy}] might be the way to go, assuming the inputs are known to sufficient precision. (If the inputs are aren't, then you cannot know the result to the desired accuracy.) $\endgroup$ – Michael E2 Aug 6 '15 at 21:37
5
$\begingroup$

How about something like

z[x_, y_] := Exp[Sin[60*x]] + Sin[50*Exp[y]]
z[SetAccuracy[20., 200], SetAccuracy[20., 200]] // Accuracy

does this not do what you need?

$\endgroup$
  • $\begingroup$ In[8]:= z[SetAccuracy[20., 200], SetAccuracy[20., 200]] // Accuracy Out[8]= 13.6508 Don't think it worked. $\endgroup$ – newprint Jul 1 '12 at 18:25
  • 1
    $\begingroup$ @newprint Sorry, I forgot to add the modified definition of z I used. Or, is it that you simply don't know the coeffs? Because that's different... $\endgroup$ – acl Jul 1 '12 at 18:47
  • $\begingroup$ Thanks, I understood the SetAccuracy and // Accuracy $\endgroup$ – newprint Jul 2 '12 at 1:13
4
$\begingroup$

If you use inexact constant in your equation it helps if you increase their accuracy as well. You can do that easily using the backtick notation:

z[x_, y_] := Exp[Sin[60.0`200*x]] + Sin[50.0`200*Exp[y]]
z[SetAccuracy[20., 200], SetAccuracy[20., 200]] // Accuracy

190.0318717

$\endgroup$
  • $\begingroup$ Initially, I used the backtick for floating point values as well(I read about in Mathematical Cookbook), though it becomes somewhat cumbersome within long equation. $\endgroup$ – newprint Jul 2 '12 at 1:19
0
$\begingroup$

Short answer

If you want Mathematica to return arbitrary-precision calculations with (approximately) a given Accuracy you could ask for it in MantissaExponent form:

MantissaExponent[Exp[150] 123``50][[1]] // Accuracy
52.8558

Supporting information

Accuracy of input numbers can be set with double backticks:

12345.5678``50   // Accuracy
12345``50        // Accuracy
50.
50.

Single backticks define Precision:

12345.5678`50   // Precision
12345`50        // Precision
50.
50.

A "constant accuracy" doesn't make much sense for floating point calculations. Mathematica tracks and preserves Precision. This is called "arbitrary-precision" after all.

Exp[150] 123`50  // Precision
Exp[150] 123``50 // Accuracy
50.
-15.1442
$\endgroup$
  • 1
    $\begingroup$ I think in the "Short answer" section you use wrong formulation because MantissaExponent does not alter the accuracy of the result (I know, you know it - just for correctness). The right formulation could be: "If you want Mathematica to give you the accuracy of the result of calculations you could ask for it using MantissaExponent form: ..." $\endgroup$ – Alexey Popkov Jul 2 '12 at 17:43
  • $\begingroup$ @Alexey I wasn't sure how to say what I wanted to. I'll try to rephrase it as you suggest later today. $\endgroup$ – Mr.Wizard Jul 2 '12 at 18:00
  • $\begingroup$ One problem is that Wolfram's understanding of precision and accuracy is significantly misleading and the second one is that you use in this sentence the term "accuracy" with another meaning: the number of precise digits right to the decimal point in ScientificForm. In other words, it is something always approximately equal to Precision minus 1. $\endgroup$ – Alexey Popkov Jul 2 '12 at 18:14
  • $\begingroup$ Discussion on Precision and Accuracy in Mathematica: groups.google.com/d/msg/comp.soft-sys.math.mathematica/… $\endgroup$ – Alexey Popkov Jul 2 '12 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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