3
$\begingroup$
F1[r]=((37.3042 - r) (-25.578 + r) (62.8822 + r))/(3000 r)
FindMaximum[F1[r], r]

The output is {0.0345106, {r -> 31.0723}}. Then I defined the quantities

maxr = r /. FindMaximum[F1[r], r][[2]]
maxF1 = FindMaximum[F1[r], r][[1]]

maxr gives the value of r at which F1[r] is a maximum, and maxF1 is the value of that maximum.

Now I take the difference between the two ways of finding the maximum, which should be exactly 0, but I got a nonzero contribution.

maxF1 - F1[maxr] // N

The output I got is 1.38778*10^-17. How do I change the working precision or accuracy to always make this exactly 0. in a given notebook?

$\endgroup$
3
  • $\begingroup$ maxr and maxF1 seem to have the same definition; is this intentional? Also, what is maxxf? $\endgroup$
    – thorimur
    Mar 10 at 22:30
  • 1
    $\begingroup$ Ah, I think I understand, and I've edited your question to match, if that's ok. $\endgroup$
    – thorimur
    Mar 10 at 22:57
  • $\begingroup$ yes that is fine now $\endgroup$
    – Immy Salam
    Mar 11 at 12:43
3
$\begingroup$

Each function such as FindMaximum has its own settings for precision and accuracy. So, you could for example evaluate

FindMaximum[F1[r], r, WorkingPrecision -> 50]

for example, and this would use 50 digits of precision during that computation, which in this case seems to be enough to get the result to be exactly 0..

Another relevant option is AccuracyGoal, which will change the point that is found slightly, but not the precision with which it's located. So, you'd still need to modify WorkingPrecision, e.g.

FindMaximum[F1[r], r, WorkingPrecision -> 50, AccuracyGoal -> 50]

(Another possibly relevant option is PrecisionGoal, but usually Automatic is good enough.)

But to set these for the entire notebook, instead of in each individual function call, you can evaluate

SetOptions[FindMaximum, {WorkingPrecision -> 50, AccuracyGoal -> 50}]

and now FindMaximum[F1[r], r] will use those options by default. (At least, within that kernel session.)

Hope this helps!

$\endgroup$

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.