I am trying to combine 3D plots of different functions. Usually, this works:


However, I am encountering a weird phenomenon. Sometimes, when I plot f1 and f2 separately and combine the graphs with Show[...], all is fine. But if use Plot3D[{f1,f2}...] then the graph is messed up.

What is going on ? I am guessing that Plot3D[] with multiple functions tries to decide on a compromise mesh that will work for all arguments. But in the following example where both functions basically have the same values, I don’t understand how it can fail.

Here is an example of this problem on Mathematica 11.2 (MacOS 10.12.6). I solve the Burgers equation either exactly or numerically (examples from the documentation).

BurgersEquation1 = D[u[x,t],t] + u[x,t] D[u[x,t],x] == 0.1 D[u[x,t],{x,2}];
InitialCondition1 = u[x, 0] == Piecewise[{{1, x < 0}}];
f1 = DSolveValue[{BurgersEquation1, InitialCondition1}, u[x, t], {x, t}]
f2 = NDSolveValue[{BurgersEquation1, InitialCondition1, u[-2, t] == 1, u[2, t] == 0},
                     u, {x, -2, 2}, {t, 0, 1}];
   (* the extra boundary conditions for f2 help with the numerical
      computation but it’s not relevant to this question *)
g1=Plot3D[Evaluate[f1], {t, 0, 1}, {x, -2, 2}]
g2=Plot3D[f2[x,t], {t, 0, 1}, {x, -2, 2}]

The output ares two nice graphics that I cannot distinguish by eye:

sharp edge graph

But this one is bad:

Plot3D[{Evaluate[f1],f2[x,t]},{t, 0, 1}, {x, -2, 2}]

round edge on the exact solution

However, as one could expect, combining the two good ones with show gives anothe good one:


sharp edges on both

What is going on ? And what should I do to secure the output from Plot3D[{f1,f2}] ?

PS1. I added some colors and meshes options to generate the pictures above and make them clearer, but it does not change the core problem.

PS2. A better zoom on the problem. I have shifted the exact solution above in the Plot3D[{f1,f2}] and the exact one is really badly meshed:

zoom on the problem

                       ------- Better example -----

It’s really not a numeric vs exact problem. Look at the output of this:

  f[t_,x_]:=1/(1 + (E^(-2.5(t - 2 x)) (1 + Erf[(1.5811 x)/Sqrt[t]]))/(1 + Erf[(1.5811(t - x))/Sqrt[t]]));
  Plot3D[{0, f[t,x]}, {t, 0, .05}, {x, -0.2, 0.2},
          PlotStyle -> {Red, Green}, PlotTheme -> "ZMesh »]

two functions is bad

and compare to the output of this:

  Plot3D[f[t,x], {t, 0, .05}, {x, -0.2, 0.2},
          PlotStyle -> {Red, Green}, PlotTheme -> "ZMesh"]

one function is good

Plotting the zero function next to another one should not alter the graph....

  • $\begingroup$ Since a symbolic and numeric solution to a DE will always be different, I'm not surprised that there are slight differences in the Plot and I expect the glaring color shifts are just making this more obvious. Plot3D[f1-f2[x,t],{t, 0, 1}, {x, -2, 2}] to inspect the difference of the two solutions. Include a ,PlotRange->{-.01,.3} option to Plot to see a much larger error near (0,0). $\endgroup$ – Bill Apr 13 '18 at 20:26
  • $\begingroup$ @Bill: You might have missed the point. Look at the arrow I added on the 2nd graphic. The exact solution (the green one) is mis-meshed near the origin when it is plotted with something else, but not when it is plotted alone on the 3rd graph. I am adding a zoom so you can see the problem better. $\endgroup$ – Francois Vigneron Apr 13 '18 at 21:08
  • $\begingroup$ Hm. I think i see your problem but I cannot reproduce it with version 11.3 on macos 10.13.4. $\endgroup$ – Henrik Schumacher Apr 13 '18 at 21:15
  • $\begingroup$ @Henrik Schumacher: that would be good news if I just need to update. Try the simpler example I just found and added at the end. $\endgroup$ – Francois Vigneron Apr 13 '18 at 21:21
  • $\begingroup$ Ah, now I see it. Seems to happen only if the problematic function is not the first in the list... Very strange. I think it is related to the fact that 1/(1 + (E^(-2.5 (t - 2 x)) (1 + Erf[(1.5811 x)/Sqrt[t]]))/(1 + Erf[(1.5811 (t - x))/Sqrt[t]])) /. t -> 0 returns the wrong result 1/(1 + E^(5. x))`. $\endgroup$ – Henrik Schumacher Apr 13 '18 at 21:32

Your Answer

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

Browse other questions tagged or ask your own question.