# Improving ContourPlot and Table output

I am trying to examine the results of and NDSolve solution for a BVP, and the output is not at all "pretty". Here's the code:

*d = .0005;        (* diffusion coefficient (L^2/t *)
v = .0010;         (* velocity (L/s) *)
r = 3.;            (* depth of lower boundary condition *)
xmax = r/1.;       (* maximum plotted value of x *)
NDSolve[
{
D[c[t, x], t] == d*D[c[t, x], x, x] - v*D[c[t, x], x],
c[0, x] == UnitStep[x - .002] - UnitStep[x - .15],
Derivative[0, 1][c][t, 0] == c[t, 0]*v/d,
c[t, r] == 0
},
c,
{t, 0, 50},
{x, 0, r}];
q = Evaluate[c[t, x] /. % ];
Table[{x, q}, {t, 50, 50}, {x, 0, r/2, .05}]
ContourPlot[q, {t, 0, 50}, {x, 0, xmax/5},
ColorFunction -> Hue,
Contours -> 20,
AspectRatio -> .75,
PlotPoints -> 25,
Axes -> True,
AxesLabel -> {"time", "distance"},
PlotLabel -> "Concentration"]
Plot3D[q, {t, 0, 50}, {x, 0, xmax}, PlotRange -> All]


There are two things I'd like to be able to change in the output:

1. The output by the Table command is one long line that wraps until it's done. I'd like to see two columns, one for x and one for q. The value of q should be t = 50, and x should be the sequence {x, 0, r/2, .05}.

2. I have been unsuccessful getting the ContourPlot to print out a legend showing which colour represents what values of the dependent variable q[t,x].

Any suggestions will be greatly appreciated.

-

Flatten the list of solutions:

q = c[t, x] /.
Quiet@Flatten@
NDSolve[{D[c[t, x], t] == d*D[c[t, x], x, x] - v*D[c[t, x], x],
c[0, x] == UnitStep[x - .002] - UnitStep[x - .15],
Derivative[0, 1][c][t, 0] == c[t, 0]*v/d, c[t, r] == 0},
c, {t, 0, 50}, {x, 0, r}];


Set t=50 (locally) and use TableForm. You can add the option TableHeadings:

TableForm[
Block[{t = 50}, Table[{x, q}, {x, 0, r/2, .05}]],

(* x      q
-------------------
0.     0.35899
0.05   0.385767
0.1    0.392973
0.15   0.38057
... *)


Of course you can also use Grid and/or add more options and Style to further improve the appearance of the table.

For the legend you might want to look into this answer.

I just used DensityPlot with the minimum and maximum values of q to build a simple legend:

legend = DensityPlot[y, {x, 0, 10}, {y, -0.045, 1},
AspectRatio -> 10,
ColorFunction -> Hue,
FrameTicks -> {False, True},
PlotLabel -> "q(x,t)",
ImagePadding -> {{Automatic, Automatic}, {40, 10}},
ImageSize -> {60, 225}]


Use the same ImagePadding in the plot to have the frames align:

plot = ContourPlot[q, {t, 0, 50}, {x, 0, xmax/5},
ColorFunction -> Hue, Contours -> 20,
AspectRatio -> 0.75,
PlotPoints -> 25,
FrameLabel -> {"time", "distance"},
PlotLabel -> "Concentration",
ImagePadding -> {{Automatic, Automatic}, {40, 10}},
ImageSize -> {300, 225}]


Finally, display the result with Row:

Row[{plot, legend}]


-
einbandi - thanks for a great reply. I tried your suggestions and they work great. thanks again! - eric – Eric Feb 22 '13 at 3:49
You're welcome! You may have noticed that your question was downvoted. Here are some tips to get better votes plus more/better answers to your future questions: 1) Read the FAQ section. 2) Use the Mathematica Documentation Center and this site's search function thoroughly before posting your question (you might want to check this out). – einbandi Feb 22 '13 at 17:28
3) Try to post minimal working examples. The more code people have to go through, the more time they have to spend with your question and the less likely it is to get good answers. 4) Try to show as much effort of your own as possible. 5) Note that you can mark an answer that satisifes your need as your accepted one by clicking the green tick mark below the vote number. – einbandi Feb 22 '13 at 17:30