# Running Whole Script within a Loop?

I'm still inexperienced with Mathematica, and numerics in general, but I know how to apply For and Do in basic situations. However, what I'm having issues with recently is running a full script within some sort of loop. What I'd like to do is to define a handful of input variables, called tau below, and evaluate the script I've provided for each of the values, and collect the values CutLength, Angle, AbssS, and ArggS for each input into a list. I know that for each of these four I need to create a constant array, and use a loop to collect the values, but I'm not sure how to run a loop with an large script in the body.

tau={0.5+2*I, 0.3+2*I, 0.2+2*I, 0.1+2*I, 0.5+1*I, 0.3+1*I, 0.2+1*I, 0.1+1*I} M = 1; w1 = Pi/2; w2 = Pi*(tau)/2; inv = WeierstrassInvariants[{w1, w2}]; E2[t_] := 1 - 24*Sum[(n*Exp[2*Pi*I*(t)*n])/(1 - Exp[2*Pi*I*(t)*n]), {n, 1, 300}]; z[u_] := (I* M/2)*(WeierstrassZeta[u, inv] - ((1/3)*N[E2[tau], 50]*(u))); WP[x_, y_] := WeierstrassP[w1*x + w2*y, inv]; L = -(1/3)*N[E2[tau], 50]; f[x_, y_] := Re[WP[x, y] - L]; g[x_, y_] := Im[WP[x, y] - L]; E22[z_] = 1 - 24*Sum[(n*Exp[2*Pi*I*(z)*n])/(1 - Exp[2*Pi*I*(z)*n]), {n, 1, 800}]; EisensteinE2[4, z_] := (EllipticTheta[2, 0, Exp[I*Pi*(z)]]^8 + EllipticTheta[3, 0, Exp[I*Pi*(z)]]^8 + EllipticTheta[4, 0, Exp[I*Pi*(z)]]^8)/2; K2[z_] = (((E22[z])^2) - EisensteinE2[4, z])/144; V1 = Quiet[ FindRoot[{f[x, y] == 0, g[x, y] == 0}, {x, 0.5}, {y, 1}, WorkingPrecision -> 50]]; V2 = Quiet[ FindRoot[{f[x, y] == 0, g[x, y] == 0}, {x, 1.5}, {y, 1}, WorkingPrecision -> 50]]; V3 = Quiet[ FindRoot[{f[x, y] == 0, g[x, y] == 0}, {x, 0.5}, {y, -1}, WorkingPrecision -> 50]]; V4 = Quiet[ FindRoot[{f[x, y] == 0, g[x, y] == 0}, {x, 1.5}, {y, -1}, WorkingPrecision -> 50]]; A1 = x /. V1; B1 = y /. V1; A2 = x /. V2; B2 = y /. V2; A3 = x /. V3; B3 = y /. V3; A4 = x /. V4; B4 = y /. V4; Z1 = Quiet[N[z[w1*A1 + w2*B1], 50]] Z2 = Quiet[N[z[w1*A2 + w2*B2], 50]] Z3 = Quiet[N[z[w1*A3 + w2*B3], 50]] Z4 = Quiet[N[z[w1*A4 + w2*B4], 50]] CutLength = Abs[Z1 - Z2] Angle = Pi - Arg[Z1 - M/2] AbssS = Abs[N[K2[tau], 10]] ArggS = Arg[N[K2[tau], 10]]

The first line contains the inputs I want to range over, and the last four lines are the values I'd like to collect. Any tips here would be appreciated!

• You have tau as a list of complex numbers, but then you seem to use tau as an ordinary scalar variable (I think). When I try to run your code I see a flood of warnings and errors. I assume you have seen an even larger flood of warnings and that is why you have put so many Quiet into your code. I don't think that telling it "don't tell me about all the errors in my code" is a good way to deal with Mathematica. Now, if all that were fixed, then I would append Map[{Abs[Z1 - Z2], Pi - Arg[Z1 - M/2], Abs[N[K2[#], 10]], Arg[N[K2[#], 10]]} &, tau] to the end of your code to get your results out. – Bill Aug 8 '16 at 4:44
• @Bill Indeed, I'm aware of the flood of errors, and I've been trying to figure them out. I'm still not very good with Mathematica. I've added your line at the end of the code, and it appears to simply run indefinitely. I feel like the problem might be the Abs[Z1-Z2] and Arg[Z1-M/2] because they aren't directly functions of tau. You need to run the script for some given tau and then spit out those numbers. The last two are genuinely functions of tau. – Benighted Aug 8 '16 at 5:19
• Your fourth line is w2 = Pi*(tau)/2 and tau is a list of 8 different complex numbers. Are you expecting do do this whole block of code for 0.5+2*I and then the whole block of code for 0.3+2*I, etc? – Bill Aug 8 '16 at 5:55
• @Bill Yes exactly. That's what I was hoping for. It runs perfectly for me, when tau is a single value. Except for the errors you mention. But it does output the values properly. – Benighted Aug 8 '16 at 6:00

This isn't a good style to use, but perhaps it will get you something you can look at

newfunction[tau_] := (M = 1;
w1 = Pi/2;
w2 = Pi*(tau)/2;
inv = WeierstrassInvariants[{w1, w2}];
E2[t_] := 1-24*Sum[(n*Exp[2*Pi*I*(t)*n])/(1-Exp[2*Pi*I*(t)*n]), {n,1,300}];
z[u_] := (I*M/2)*(WeierstrassZeta[u, inv]-((1/3)*N[E2[tau], 50]*(u)));
WP[x_, y_] := WeierstrassP[w1*x + w2*y, inv];
L = -(1/3)*N[E2[tau], 50];
f[x_, y_] := Re[WP[x, y] - L];
g[x_, y_] := Im[WP[x, y] - L];
E22[z_] = 1-24*Sum[(n*Exp[2*Pi*I*(z)*n])/(1-Exp[2*Pi*I*(z)*n]), {n,1,800}];
EisensteinE2[4, z_] := (EllipticTheta[2, 0, Exp[I*Pi*(z)]]^8 +
EllipticTheta[3, 0, Exp[I*Pi*(z)]]^8 + EllipticTheta[4, 0, Exp[I*Pi*(z)]]^8)/2;
K2[z_] = (((E22[z])^2) - EisensteinE2[4, z])/144;
V1 = Quiet[FindRoot[{f[x, y] == 0, g[x, y] == 0}, {x, 0.5}, {y, 1},
WorkingPrecision -> 50]];
V2 = Quiet[FindRoot[{f[x, y] == 0, g[x, y] == 0}, {x, 1.5}, {y, 1},
WorkingPrecision -> 50]];
V3 = Quiet[FindRoot[{f[x, y] == 0, g[x, y] == 0}, {x, 0.5}, {y, -1},
WorkingPrecision -> 50]];
V4 = Quiet[FindRoot[{f[x, y] == 0, g[x, y] == 0}, {x, 1.5}, {y, -1},
WorkingPrecision -> 50]];
A1 = x /. V1;
B1 = y /. V1;
A2 = x /. V2;
B2 = y /. V2;
A3 = x /. V3;
B3 = y /. V3;
A4 = x /. V4;
B4 = y /. V4;
Z1 = Quiet[N[z[w1*A1 + w2*B1], 50]];
Z2 = Quiet[N[z[w1*A2 + w2*B2], 50]];
Z3 = Quiet[N[z[w1*A3 + w2*B3], 50]];
Z4 = Quiet[N[z[w1*A4 + w2*B4], 50]];
{CutLength = Abs[Z1 - Z2],
Angle = Pi - Arg[Z1 - M/2],
AbssS = Abs[N[K2[tau], 10]],
ArggS = Arg[N[K2[tau], 10]]});

Map[newfunction, {0.5+2*I,0.3+2*I,0.2+2*I,0.1+2*I,0.5+1*I,0.3+1*I,0.2+1*I, 0.1+1*I}]


which in an instant gives you

{{0.00746972, 6.28319, 6.97454*10^-6, -2.04125*10^-17},
{0.00746975, 0.628312, 6.97464*10^-6, -1.25662},
{0.00746979, 0.942471, 6.97473*10^-6, -1.88494},
{0.00746981, 1.25663, 6.9748*10^-6, -2.51326},
{0.17221, 6.28319, 0.00369319, -5.43219*10^-16},
{0.172657, 0.624762, 0.00372204, -1.24597},
{0.173056, 0.93893, 0.0037479, -1.87431},
{0.173378, 1.25445, 0.00376887, -2.50671}}


Note the ( and ) around the many lines making up the definition of newfunction. Read the documentation for Map and see if you can make any sense of how I used that to give each of your complex numbers, one at a time, to newfunction.

Then start trying to track down the reason for all your warnings and errors.