# Subscripted variables give errors in Module expression [duplicate]

I have following problem. I want the R not to calculate before the integration but substitute later as a function of z. My Mathematica codes look like: Codes like big, (sorry) but most of them are just the parameters.

yCal[z_] :=
Module[{ℏ = 1.05*10^-27, e = 4.8*10^-10, c = 3*10^10,
Subscript[ϵ, ∞] = 1.1431, ωd =
1.3202*10^16, γ = 1.0805*10^14,
Subscript[Ω, 1] = 3.8711*10^15,
Subscript[Γ, 1] = 4.4642*10^14,
Subscript[Ω, 2] = 4.1684*10^15,
Subscript[Γ, 2] = 2.3555*10^15,
Subscript[ϕ, 1] = -1.2371, Subscript[ϕ, 2] = -1.0968,
Subscript[A, 1] = 0.26698, Subscript[A, 2] = 3.0834,
Subscript[ϵ, d] = 1, λ = 630*10^-7,
L = 1000*10^-7,
ko = (2 Pi)/(630*10^-7), ω = (2.0*π*3*10^10)/(
630*10^-7), R, Subscript[n, eff], Subscript[α, 1],
Subscript[α, 2], C1, C2, A, ρ, Ez,
Subscript[ϵ, m], MyFun, Pl},
Subscript[n, eff] =
Sqrt[4/(ko^2*((L - z)*Tan[2.2 Degree])^2) (
Subscript[ϵ, d]/Subscript[ϵ, m])/
ProductLog[
Exp[0.577]*Subscript[ϵ, d]/Subscript[ϵ, m]] +
Subscript[ϵ, d]];
Subscript[ϵ, m] =
Subscript[ϵ, ∞] - (ωd^2*ω^2)/(\
ω^4 + γ^2*ω^2) + \!$$\*UnderoverscriptBox[\(∑$$, $$p = 1$$, $$2$$]$$\*SubscriptBox[\(A$$, $$p$$]\ \
\*SubscriptBox[$$Ω$$, $$p$$] $$( \*FractionBox[\(Cos[ \*SubscriptBox[\(ϕ$$, $$p$$]]\ *$$( \*SubscriptBox[\(Ω$$, $$p$$] - ω)\) -
\*SubscriptBox[$$Γ$$, $$p$$]*Sin[
\*SubscriptBox[$$ϕ$$, $$p$$]]\), $$\*SuperscriptBox[ SubscriptBox[\(Ω$$, $$p$$], $$2$$] - 2
\*SubscriptBox[$$Ω$$, $$p$$]*ω +
\*SuperscriptBox[$$ω$$, $$2$$] +
\*SuperscriptBox[
SubscriptBox[$$Γ$$, $$p$$], $$2$$]\)] +
\*FractionBox[$$Cos[ \*SubscriptBox[\(ϕ$$, $$p$$]]\ *$$( \*SubscriptBox[\(Ω$$, $$p$$] + ω)\) -
\*SubscriptBox[$$Γ$$, $$p$$]\ \ *Sin[
\*SubscriptBox[$$ϕ$$, $$p$$]]\), $$\*SuperscriptBox[ SubscriptBox[\(Ω$$, $$p$$], $$2$$] + 2
\*SubscriptBox[$$Ω$$, $$p$$]*ω +
\*SuperscriptBox[$$ω$$, $$2$$] +
\*SuperscriptBox[
SubscriptBox[$$Γ$$, $$p$$], $$2$$]\)])\)\)\);
Subscript[α, 1] =
Sqrt[Subscript[n, eff]^2 - Subscript[ϵ, m]];
Subscript[α, 2] =
Sqrt[Subscript[n, eff]^2 - Subscript[ϵ, d]];
C1 = (Abs[BesselK[1, ko*Subscript[α, 1]*R]])^2 Integrate[
Abs[BesselI[1,
ko*Subscript[α, 1] *ρ]]^2*ρ, {ρ, 0, R}];
C2 = (Abs[BesselI[1, ko*Subscript[α, 1]*R]])^2 Integrate[
Abs[BesselK[1,
ko*Subscript[α, 1] *ρ]]^2*ρ, {ρ,
R, ∞}];
With [{A1 = C1, A2 = C2, Ind = Subscript[n, eff]},
A = (Subscript[n, eff]*c)/2*(
Subscript[ϵ, m]*Subscript[ϵ,
d]*(Abs[BesselK[1, ko*Subscript[α, 1]*R]])^2)/(
Subscript[ϵ, d]*Hold@A1 +
Subscript[ϵ, m]*Hold@A2)];
ReleaseHold[A /. R = (L - z)*Tan[2.2 Degree]] ;
MyFun = Integrate[ko*Subscript[n, eff], z];
Ez = (A*Subscript[α, 1])/Subscript[ϵ, m]
BesselI[0, ko*Subscript[α, 1] *ρ] Cos[MyFun]]


I want to calculate Ez. There is an error about "Local variable specification." even though there are no variables kept within the Module{}.

• Please format your code properly in code blocks. Edit your post by clicking the grey edit button below your post, and click on the grey question mark on the right side of the editing toolbar for formatting help. In addition, it seems like you've just dumped all of your code here. Can you reduce this to a small example that shows the problem you are having that doesn't require us to sort through many lines of code? – march Apr 26 '16 at 16:35
• You have A/.R=(L-z)*Tan[2.2 Degree] and /. is almost always of the form A/.R->replacmentForYourR. You are also doing Hold@A1 and Hold@A2 only to release the holds in the next step. The form of the With and the Hold/ReleaseHold worries me and I wonder if that is the cause of your error message, but I can't understand your purpose clearly enough to write a simpler error-free change. – Bill Apr 26 '16 at 21:00
• When I first started using Mathematica I used subscripts. Eventually I realized this is not a good way to go. I'd recommend switching to unsubscripted variables (like Ω1 instead of Subscript[Ω,1]). – QuantumDot Apr 26 '16 at 21:39
• The error message pretty much answers the question implicit in the subject header. – Daniel Lichtblau Apr 27 '16 at 3:43

A minimum working example that reproduces your problem is:

Module[{Subscript[ϵ, ∞] = 1.1431}, Subscript[ϵ, ∞]]


Module::lvset: Local variable specification {Subscript[ϵ, ∞]=1.1431} contains Subscript[ϵ, ∞]=1.1431, which is an assignment to Subscript[ϵ, ∞]; only assignments to symbols are allowed. >>

The message is very clear. You simply can not use subscripted expressions as local variables in Module expression. The fix is to substitute valid identifiers for subscripted expressions.

Module[{ϵ∞ = 1.1431}, ϵ∞]


1.1431