I know that this is a "beginner question", and, in fact, I am. I want to improve my code, as it takes really too much time to run.
I have already read some other discussions, like here,
but I am struggling in translating the simplifications with Table or Do into my example.
The For cycle I want to improve is the following:
zh = 0.4;
list = {};
For[i = 1, i < 10000, i++,
With[{zg = -0.6, dh = i10^-2},
nsol = Block[{eps = $MachineEpsilon},
NDSolve[{phi''[x] + 2phi'[x]/x + (2(zg + zhExp[-zh*x/dh])/x + 1)phi[x] == 0, phi[eps] == 1, phi'[eps] == -(zg + zh)}, phi, {x, eps, 20000},
WorkingPrecision->MachinePrecision, AccuracyGoal->15, PrecisionGoal->8, MaxSteps->Infinity]]];
AppendTo[list, 1/Evaluate[(15000phi[15000])^2 + ((15000-Pi/2)*phi[15000-Pi/2])^2 /. nsol[[1]]]];]
Clearly, this code, written in this way, is highly inefficient. Also, I need to do more of these, with different values for zg inside With, and make some plots out of the lists.
Anyone that can help me with this noob question? Thanks a lot!
SowandReaphas several relevant examples. – Szabolcs Oct 21 '20 at 16:43Tablewouldn't work—what exactly have you tried? Also, the code you posted here doesn't run (which is why I can't directly give you theTabletranslation). Please show a complete, minimal, working example. – Szabolcs Oct 21 '20 at 18:58Withdoes literal replacement.a = b; With[{b=1}, a]does not work. You needBlockfor that, notWith. – Szabolcs Oct 22 '20 at 12:51Table. – Szabolcs Oct 22 '20 at 15:44