Skip to main content

differential equations - Need help in plotting a function with two variables in ODE


The solution of this ODE was given in this link


Here I am asking, if L is a function of tot, g, Z and tot depends on Z and g, which means I need to solve the ODE for each value of Z and g to get tot and then pass it to L function, after that Plot L.

I am not sure should I use ContourPlot or somethings els. Help please.


p[Z0_, g0_, k0_, R0_] := 
Block[{Z = Z0,
g = Rationalize[g0, 0],
k2 = Rationalize[k0, 0],
ϵ = 10^-4,
R = Rationalize[R0, 0]},
ps =
ParametricNDSolveValue[
{y''[r] + 2 y'[r]/r == k2 Sinh[y[r]],

y[ϵ] == y0, y'[ϵ] == 0,
WhenEvent[r == 1, y'[r] -> y'[r] + Z g]},
{y, y'}, {r, ϵ, R}, {y0},
Method -> "StiffnessSwitching",
WorkingPrecision -> 20];

sol = FindRoot[Last[ps[y0]][R], {y0, -1}, Evaluated -> False][[1, 2]];
tot = 4 π sol NIntegrate[r^2 Exp[-First[ps[sol]][r]], {r, 0, R}];
L= Z/g*tot;


ContourPlot[L, {g, 0.02, 0.06}, {Z, 500, 800}]]

Quiet[
Table[p[Z, g, 0.0002, 1.5], {g, 0.02, 0.06, .02},{Z, 500, 800, 200}]]

Answer



The desired contour plot can be obtained as follows. Begin with a slight modification to the code in the question.


p[Z0_, g0_, k0_, R0_] := Block[{Z = Z0, 
g = Rationalize[g0, 0], k2 = Rationalize[k0, 0], ϵ = 10^-4, R = Rationalize[R0, 0]},
ps = ParametricNDSolveValue[{y''[r] + 2 y'[r]/r == k2 Sinh[y[r]], y[ϵ] == y0, y'[ϵ] == 0,
WhenEvent[r == 1, y'[r] -> y'[r] + Z g]}, {y, y'[R]}, {r, ϵ], R}, {y0},

Method -> "StiffnessSwitching", WorkingPrecision -> 20];
sol = FindRoot[Last[ps[y0]], {y0, -1}, Evaluated -> False][[1, 2]];
tot = 4 π sol NIntegrate[r^2 Exp[-First[ps[sol]][r]], {r, ϵ, R}];
L = Z/g*tot]

Specifically, {y, y'} is replaced by {y, y'[R]} to save a bit of time, because the value of y' is needed only at r = R; a corresponding change is made in the first argument of FindRoot; the lower bound of NIntegrate is set to ϵ, because ps is undefined for smaller values of r; and ContourPlot is removed from within the definition of p.


The desired contour plot then is


ContourPlot[Quiet[p[Z, g, 0.0002, 1.5]], {g, 0.02, 0.06}, {Z, 500, 800}, 
PlotPoints -> {5, 4}, MaxRecursion -> 1, PlotLegends -> Automatic,
FrameLabel -> {g, Z}, ImageSize -> Large, LabelStyle -> {Bold, Black, 15}]


enter image description here


which takes about two minutes on my computer. The plotting can, however, be parallelized with equal resolution using


DistributeDefinitions[p];
tab = ParallelTable[Quiet[p[Z, g, 0.0002, 1.5]], {Z, 500, 800, 50}, {g, 0.02, 0.06, .005}];
ListContourPlot[tab, DataRange -> {{.02, .06}, {500, 800}}, PlotLegends -> Automatic,
FrameLabel -> {g, Z}, ImageSize -> Large, LabelStyle -> {Bold, Black, 15}]

which produces the same plot in 37 seconds. (My computer has six processors.) Finally, the table requested in the question is


Table[Quiet[p[Z, g, 0.0002, 1.5]], {g, 0.02, 0.06, .02}, {Z, 500, 800, 200}]

(* {{-3.41941*10^11, -6.80714*10^11}, {-3.51627*10^11, -6.9574*10^11},
{-3.55499*10^11, -7.0144*10^11}} *)

Comments

Popular posts from this blog

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...