3
$\begingroup$

I tried to plot a certain 3D sine function:

ContourPlot3D[(Sin[x^2] + 2) * (Sin[y^2] + 2) * (Sin[z^2] + 2)
 , {x, -2, 2}, {y, -2, 2}, {z, -2, 2}]

but ran into an issue similar to (but not the same as) my last post. This suggested that the plot requires more than 512 megabytes of RAM to generate (I'm using the online version, as opposed to the desktop application which can go as far as the system itself is capable of handling).

The problem is most likely due to the fact that the expression being plotted is evaluated more frequently where the plot is rapidly changing, making a function with more steep areas more intensive. An example showed a function with one contour being evaluated around 260,000 times. In my case, it may be more than that (as I was using the default 3 contours), possibly over a million evaluations since this function changes increasingly rapidly as we travel further from 0,0,0.

How can I trade detail for lower memory consumption? Simply setting a lower value of PlotPoints does not fix the problem.

$\endgroup$
1
  • $\begingroup$ You have a few questions with high up vote answers but have not accepted any (by clicking the checkmark under the voting buttons). Please revisit these questions and accept an appropriate answer where possible. $\endgroup$
    – Edmund
    Apr 4, 2022 at 0:04

1 Answer 1

7
$\begingroup$

You may use the MaxRecursion or PerformanceGoal options to trade detail for memory consumption.

Without MaxRecursion or PerformanceGoal

MaxMemoryUsed[
  p = ContourPlot3D[(Sin[x^2] + 2)*(Sin[y^2] + 2)*(Sin[z^2] + 2)
       , {x, -2, 2}, {y, -2, 2}, {z, -2, 2}]
  ] // N@UnitConvert[Quantity[#, "Bytes"], "Megabytes"] &
p
Quantity[798.611, "Megabytes"]

Mathematica graphics

With MaxRecursion -> 0

MaxMemoryUsed[
  p3 = ContourPlot3D[(Sin[x^2] + 2)*(Sin[y^2] + 2)*(Sin[z^2] + 2)
    , {x, -2, 2}, {y, -2, 2}, {z, -2, 2}
    , MaxRecursion -> 0]
  ] // N@UnitConvert[Quantity[#, "Bytes"], "Megabytes"] &
p3
Quantity[30.8063, "Megabytes"]

Mathematica graphics

MaxMemoryUsed drops from 800MB to 30MB with detail reduced.

With PerformanceGoal -> "Speed"

MaxMemoryUsed[
  p2 = ContourPlot3D[(Sin[x^2] + 2)*(Sin[y^2] + 2)*(Sin[z^2] + 2)
    , {x, -2, 2}, {y, -2, 2}, {z, -2, 2}
    , PerformanceGoal -> "Speed"]
  ] // N@UnitConvert[Quantity[#, "Bytes"], "Megabytes"] &
p2
Quantity[3.16101, "Megabytes"]

Mathematica graphics

MaxMemoryUsed drops from 800MB (default) or 30MB (MaxRecursion) to 3MB with detail taking a significant hit but still enough to see the what is going on.

Hope this helps.

$\endgroup$
1
  • $\begingroup$ This helped a lot, thanks! I can now explore 3D sine functions further. $\endgroup$
    – Allam A.
    Apr 3, 2022 at 23:49

Your Answer

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

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