5
$\begingroup$

I want to plot lines for a specific single value from the output in Nsolve (we need to find these nodes from the output ufun, then plot them).

 ufun = NDSolveValue[{-Laplacian[u[x, y], {x, y}] == 10, u[x, 0] == 0, 
    u[x, 1] == 1}, u, {x, 0, 1}, {y, 0, 1}];
ContourPlot[ufun[x, y], {x, 0, 1}, {y, 0, 1}, 
 ColorFunction -> "TemperatureMap", 
 PlotRange -> MinMax[ufun["ValuesOnGrid"]], 
 PlotLegends -> BarLegend[Automatic], AspectRatio -> Automatic]

The results something like, the given value is 1.5, the lines are denoted by red enter image description here

$\endgroup$
6
$\begingroup$

I don't know what is meant by "nodes" but this gives the indicated graphics:

ufun = NDSolveValue[{-Laplacian[u[x, y], {x, y}] == 10, u[x, 0] == 0, 
    u[x, 1] == 1}, u, {x, 0, 1}, {y, 0, 1}];
cp = ContourPlot[ufun[x, y], {x, 0, 1}, {y, 0, 1}, 
   ColorFunction -> "TemperatureMap", 
   PlotRange -> MinMax[ufun["ValuesOnGrid"]], 
   PlotLegends -> BarLegend[Automatic], AspectRatio -> Automatic];
lines = ContourPlot[ufun[x, y] == 1.5, {x, 0, 1}, {y, 0, 1}, 
  ContourStyle -> Red];
Show[cp, lines]

This gives the points computed by ContourPlot for each line:

Cases[Normal@lines, Line[p_] :> p, Infinity]
(* two lines/tables of points/nodes
{
 {{0.`, 0.35518590998042887`}, {0.0020263424518760335`, 
   0.35511651469098104`}, ..., {1.`, 0.35518590998043176`}},
 {{0.`, 0.8443496801705759`}, {0.012332990750256536`, 
   0.8448098663926003`}, ..., {1.`, 0.8443496801705764`}}
 }
*)
$\endgroup$
4
  • $\begingroup$ thanks, it is solved now! $\endgroup$
    – ABCDEMMM
    Aug 27 '21 at 12:12
  • 1
    $\begingroup$ @ABCDEMMM You're welcome. $\endgroup$
    – Michael E2
    Aug 27 '21 at 12:12
  • $\begingroup$ can we get these selected points (nodes) as a table/list in MMA ? $\endgroup$
    – ABCDEMMM
    Aug 27 '21 at 12:17
  • 1
    $\begingroup$ @ABCDEMMM You want the coordinates of the points on each red line? That can be done. Note they are not points computed by NDSolve. They are computed by ContourPlot from the interpolation. $\endgroup$
    – Michael E2
    Aug 27 '21 at 12:30
5
$\begingroup$

You can use the options MeshFunctions, Mesh and MeshStyle as follows:

ContourPlot[ufun[x, y], {x, 0, 1}, {y, 0, 1}, 
 ColorFunction -> "TemperatureMap", 
 PlotRange -> MinMax[ufun["ValuesOnGrid"]], 
 PlotLegends -> BarLegend[Automatic],
 MeshFunctions -> {#3 &}, 
 Mesh -> {{1.5}}, 
 MeshStyle -> Directive[Thick, CapForm["Butt"], Red],
 AspectRatio -> Automatic]

enter image description here

$\endgroup$
0
0
$\begingroup$

Do I understand that right that you want to include the red lines in the plot? In that case have a look at the code below:

ufun = NDSolveValue[{-Laplacian[u[x, y], {x, y}] == 10, u[x, 0] == 0, 
u[x, 1] == 1}, u, {x, 0, 1}, {y, 0, 1}];
ContourPlot[ufun[x, y], {x, 0, 1}, {y, 0, 1}, 
ColorFunction -> "TemperatureMap", 
PlotRange -> MinMax[ufun["ValuesOnGrid"]], 
PlotLegends -> BarLegend[Automatic], AspectRatio -> Automatic, 
Epilog -> {Red, Line[{{0, 0.85}, {1, 0.85}}], 
Line[{{0, 0.35}, {1, 0.35}}]}]
$\endgroup$
0

Your Answer

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

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