I have a (somewhat long) script which runs cleanly until the very last stage. Here, I'm using FindRoot to attempt to parameterize implicit curves in the zero set of a two variable function. The FindRoot is giving me the familiar error
"is neither a list of replacement rules nor a valid dispatch table..."
and
"is not a list of numbers with dimensions..."
Now, I know using ?NumericQ is a common solution here, and I think I've done that correctly below, but I'm still getting errors. I'm hoping someone can perhaps parse through what I'm overlooking here:
M = 1;
tau = (0.5) + (0.4)*I;
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_?NumericQ] = (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];
ContourPlot[{f[x, y] == 0, g[x, y] == 0}, {x, 0, 2}, {y, -2, 2}]
V1 = Quiet[
FindRoot[{f[x, y] == 0, g[x, y] == 0}, {x, 0.8}, {y, 0.5},
WorkingPrecision -> 50]];
V2 = Quiet[
FindRoot[{f[x, y] == 0, g[x, y] == 0}, {x, 1.2}, {y, 1.5},
WorkingPrecision -> 50]];
V3 = Quiet[
FindRoot[{f[x, y] == 0, g[x, y] == 0}, {x, 0.8}, {y, -1.5},
WorkingPrecision -> 50]];
V4 = Quiet[
FindRoot[{f[x, y] == 0, g[x, y] == 0}, {x, 1.2}, {y, -0.5},
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]]
m = (Im[Z1] - Im[Z2])/((Re[Z1] - Re[Z2]));
ListPlot[{{Re[Z1], Im[Z1]}, {Re[Z2], Im[Z2]}, {Re[Z3],
Im[Z3]}, {Re[Z4], Im[Z4]}}, PlotRange -> {{-5, 5}, {-5, 5}}]
Zed[x_?NumericQ, y_?NumericQ] = z[w1*x + w2*y];
Quiet[ContourPlot[{Im[
N[Zed[x, y]]] - (m*(Re[N[Zed[x, y]]] - Re[(M/2)]) - Im[M/2]),
0}, {x, 0, 2}, {y, -4, 4}, MeshFunctions -> {#3 &},
MeshStyle -> {{Thick, Blue}}, Mesh -> {{0}},
PlotStyle ->
Directive[Orange, Opacity[0.1], Specularity[White, 30]],
PlotPoints -> 75, WorkingPrecision -> 50, ClippingStyle -> None]]
deriv[expr_, var_] :=
D[expr, var] //. {Re'[e_] :> Re[D[e, var]]/D[e, var],
Im'[e_] :> Im[D[e, var]]/D[e, var],
Arg'[e_] :> Im[D[e, var]/e]/D[e, var],
Abs'[e_] :> (Re[e] Re[D[e, var]] +
Im[e] Im[D[e, var]])/(Abs[e] D[e, var])};
Fun[x_?NumericQ, y_?NumericQ] :=
Im[N[Zed[x, y]]] - (m*(Re[N[Zed[x, y]]] - Re[(M/2)]) - Im[M/2]);
TGrad[x_?NumericQ, y_?NumericQ] := {deriv[Fun[x, y], x],
deriv[Fun[x, y], y]};
rot90[{x_, y_}] := {y, -x}
step[f_, pt : {x_, y_}, pt0 : {x0_, y0_}, resolution_] :=
Module[{grad, grad0, t, contourPoint},
grad = TGrad[x, y] /. {x -> pt[1], y -> pt[2]};
grad0 = grad /. Thread[pt -> pt0];
contourPoint =
grad0 t + pt0 /.
First@FindRoot[f /. Thread[pt -> grad0 t + pt0], {t, 0}];
Sow[contourPoint];
grad = Normalize[grad /. Thread[pt -> contourPoint]];
contourPoint + rot90[grad] resolution];
result = Reap@NestList[step[Fun[x, y], {x, y}, #, .08] &, {0, 1}, 100];
Believe it or not, all that really does need to be there in some capacity. I should point out that the portion of the above code with deriv[expr_, var_] came from this very helpful post Derivative of real functions including Re and Im and the portion of the code at the end, came from this post How to find an approximate solution in a region when Solve or NSolve does not work?
Can anyone see where my syntax is going wrong leading to the errors I'm getting above?
inv = N[Weierstrass...];, 2) use:=instead of=everywhere you have patterns on the left hand side. Now you will get some error messages that clearly indicate thatFunandTGradare not being called in a way that works. – Marius Ladegård Meyer Jun 17 '16 at 08:10x_. When your function depends on an input, you usually want to evaluate the expression for the function for each given input. Hence one uses:=. There are lots of such functions above where you use=. Just try it on a fresh kernel and see! – Marius Ladegård Meyer Jun 17 '16 at 22:03