I am trying to apply @bbgodfrey method for finding the right initial guess for the following problem but I get an error message and I don't understand where it comes from.
I would also like to know why FindRoot in Do loop
doesn't work in this case.
Below is my minimal working code:
l1 = 0.81;
Z = 500;
x0 = 10;
v0 = 0.02;
\[Epsilon] = $MachineEpsilon;
yl = -12;
yu = 0;
l0 = 0.0714`20.;
ps = ParametricNDSolveValue[{y''[r] +
2 y'[r]/r == -4 \[Pi] l k Exp[-y[r]], y[\[Epsilon]] == y0,
y'[\[Epsilon]] == 0, WhenEvent[r == 1, y'[r] -> y'[r] + Z l]}, {y,
y'}, {r, \[Epsilon], R}, {k, l},
Method -> {"StiffnessSwitching"}, AccuracyGoal -> 5,
PrecisionGoal -> 4, WorkingPrecision -> 15];
Do[x = i x0;
v = i^3 v0;
yl = -12;
yu = 0;
R = Rationalize[v^(-1/3), 0];
l = Rationalize[l1/(i x0), 0];
fy := (Do[yc = (yl + yu)/2; (* guess finder function *)
test = First[ps[yc]]["Domain"][[1, 2]];
If[test == 1, Throw[yc]];
If[Last[ps[yc]][test] > 0, yu = yc, yl = yc], {i, 50}]; yc);
yint =
Which[1 == First[ps[yl]]["Domain"][[1, 2]], yl,
1 == First[ps[yu]]["Domain"][[1, 2]], yu, True, Catch[fy]];
nn = FindRoot[Last[ps[y0]][R], {y0, yint}, Evaluated -> False][[1,
2]];
Tot = 4 \[Pi] nn NIntegrate[
r^2 Exp[-First[ps[nn, l]][r]], {r, \[Epsilon], R},
PrecisionGoal -> 4];
Print[NumberForm[i*1., 5], " ", NumberForm[Tot, 5]];, {i, 292/100,
31/10, 1/100}]
ps
was called with one parameter having a value of -12. That would be in the line ψint = ... ps[ yl ] ... Your definition of ps expects 2 parameters. However, the definition of ps contains y0 and R, which are undefined. Perhaps y0 and R should be a third and fourth parameters ofps
? If they are not parameters, they need numeric values. Also, to debug loops, start withWith[ {i=3}, ... ]
and add statements one at a time, making sure each one works for the value ofi
. Then try to replace theWith
withTable
instead ofDo
. $\endgroup$Do
. And inDo
statementsR
is numerical value. Fory0
, I belived that the code should takeyint
and pass it toy0
. Also, there is a typo ψint should be yint. $\endgroup$ps =
statement,y0 = 1; R = 1.2; Plot[ ps[0.5, 0.027] [[1]] [r], {r, 0, R}, PlotRange -> {{0, R}, {0, 4}}]
. This indicates we must set values of y0 and R, then evaluateps
with 2 parameters, followed by a subscript of 1 or 2 followed by[ r ]
. Of course we could useFirst
andLast
instead of the[[ ... ]]
notation. $\endgroup$