1
$\begingroup$

I am simulating a partial differential equation and I would like to compare the results of Plot3D and listPlot3D. I have gotten the plot3D code to work (not included) however, I am struggling to get the listPlot3D code to run. Here is my latest attempt:

eq = {D[u[t, x], {t, 1}] + D[u[t, x], {x, 1}] == Cos[x], 
u[0, x] == x^2, u[t, -10] == u[t, 10]};

sol = NDSolve[eq, u, {t, 0, 10}, {x, -10, 10}, MaxStepSize -> 0.1];


data = Table[ u[t, x] /. sol, {t, 0, 10}, {x, -10, 10}];

ListPlot3D[data, Mesh -> None, InterpolationOrder -> 0, 
ColorFunction -> "Rainbow"]

But it produces a bunch of errors like:

Outer::normal: Nonatomic expression expected at position 2 in Outer[List,1.,{1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.,15.,16.,17.,18.,19.,20.,21.}].

The actual picture of the errors is below:

enter image description here

What is going wrong here?

$\endgroup$
2
  • $\begingroup$ whoops that shouldn't be there. $\endgroup$
    – Gr Eg
    Aug 7, 2017 at 20:48
  • $\begingroup$ Still getting the same error. $\endgroup$
    – Gr Eg
    Aug 7, 2017 at 20:49

2 Answers 2

2
$\begingroup$

You can use Plot3D

sol = u /. First@NDSolve[eq, u, {t, 0, 10}, {x, -10, 10}, MaxStepSize -> 0.1];

enter image description here

Plot3D[sol[x, y], {x, 0, 10}, {y, -10, 10}, PlotPoints -> 50]

enter image description here

Or

data = Table[ sol[x, y], {x, 0, 10}, {y, -10, 10}];

ListPlot3D[Transpose@data,
 Mesh -> None,
 InterpolationOrder -> 0, 
 ColorFunction -> "Rainbow"]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks, I was using plot3D, but I wanted to see if listPlot3D would get rid of the patches of "dents"/roughness around x = -10. Is there anyway to fix that? $\endgroup$
    – Gr Eg
    Aug 7, 2017 at 20:53
  • $\begingroup$ See updated answer - it looks indeed nicer :) $\endgroup$
    – eldo
    Aug 7, 2017 at 20:58
3
$\begingroup$

Not sure exactly what you are looking for, but if you want a ListPlot3D, perhaps you want

data = Flatten[Table[{t, x, u[t, x]} /. sol, {t, 0, 10}, {x, -10, 10}], 2]

Your data is just a list of the values of u[x,t], not points in 3-space.

$\endgroup$

Your Answer

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

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