Message Boards Message Boards

Increasing the machine precision leads to long evaluation time

Posted 3 years ago

Hello guys So I've been trying to solve this equation numerically using ParametricNDSolve ( z is the parameter and y is a function of x) $$y_{xx}+(1-z ) sin(y) + \frac{z y}{2\pi^2} (y -2\pi) (y - \pi) =0 $$ But it fails to give an accurate output( not even close) So I increased the working precision to 30, and it gave the same errors but with MUCH more accuracy, but still not correct. my code:

Clear[y, x]
Clear[Derivative]
soln = ParametricNDSolve[{y''[
      x] == (1 - z) Sin[
        y[x]] + ((z *y[x])/(2 \[Pi]^2)) (y[x] - 
         2 \[Pi]) (y[x] - \[Pi]), y[15] == 2 Pi, y[-15] == 0}, 
   y, {x, -15, 15}, {z}, Method -> "BDF", MaxSteps -> Infinity, 
   WorkingPrecision -> 30];
Plot[Evaluate[Table[y[z][x]/Pi /. soln, {z, 0, 2.7, 0.5}]], {x, -15, 
  15}, PlotRange -> All]

this is the output (with a couple of errors but I didn't screenshot them and it took 3 hours to get this graph) enter image description here

So I tried to increase the working precision again to 40, but now it takes +10 hours running with no output!

This is what it should be enter image description here Any help would be very appreciated, thanks!

POSTED BY: Mohga Massoud
2 Replies
Posted 3 years ago

Hi Mohga,

Your result does not match the expected result because you didn't graph the same values for Z. You've also got a vulnerability by using machine numbers in your table, which will often cause Mathematica to lose internal precision because it will downgrade an expression result to account for the numerical properties of machine precision values. Try the following, which is a minimal edit of your version taking the above into account:

myprecision = 30;
Clear[y, x];
Clear[Derivative];
soln = ParametricNDSolve[{y''[
x] == (1 - z) Sin[
y[x]] + ((z*y[x])/(2 \[Pi]^2)) (y[x] - 
2 \[Pi]) (y[x] - \[Pi]), y[15] == 2 Pi, y[-15] == 0}, 
y, {x, -15, 15}, {z}, Method -> "BDF", MaxSteps -> Infinity, WorkingPrecision -> myprecision];

Plot[Evaluate[
Table[y[SetPrecision[z, myprecision]][SetPrecision[x, myprecision]]/
Pi /. soln, {z, {0, 0.5, 1, 2, 2.5, 2.609}}]], {x, -15, 15}, 
PlotRange -> All]

You can generally increase performance substantially by tuning AccuracyGoal, PrecisionGoal, and the step/recursion parameters for your chosen method. I usually start with conservative values, check that the result behaves well as I vary the values, then tune for performance for my particular situation.

POSTED BY: kirk reinholtz
  1. Increasing the floating point digits in a computation does not necessarily increase the accuracy of a computation.

  2. From https://reference.wolfram.com/language/ref/SetPrecision.html: "If there are numbers too large or small to represent machine-precision numbers, SetPrecision[expr,MachinePrecision] will convert them to arbitrary-precision numbers with precision $MachinePrecision."

POSTED BY: Richard Frost
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract