# General::ivar: is not a valid variable [duplicate]

pde = 1/Cosh[x]*D[Cosh[x]*D[T2[x, y], x], x] + 1/Sin[y]*D[Sin[y]*D[T2[x, y], y], y] == 0

sol[x0_] := NDSolve[{pde, T2[ArcTanh[x0], y] == 1, T2[xf, y] == 0}, T2[x, y], {x, ArcTanh[x0], 40}, {y, 0, 180 Degree}];

EG[x_, y_, x1_] := 1/(Cosh[x]^2 - Sin[y]^2)*((D[T2[x, y] /. sol[x1], x])^2 + (D[T2[x, y] /. sol[x1], y])^2)

EG[1, 1, 0.2]


General::ivar: 5 is not a valid variable.

I can see the issue in EG is that its unable to take derivative first and then sub the value. How can I overcome this?

• Why aren't you using different variables in NDSolve[]? Or, making them private with e.g. Module[]? Commented May 29, 2020 at 8:27

As you've noticed, this is a matter of evaluation order. The following is one relatively natural way to control the evaluation:

sol[x0_] :=
NDSolve[{pde, T2[ArcTanh[x0], y] == 1, T2[xf, y] == 0},
T2, {x, ArcTanh[x0], xf}, {y, 0, 180 Degree}][[1]]

expr[x_, y_] = 1/(Cosh[x]^2 - Sin[y]^2) #.# &@Grad[T2[x, y], {x, y}]

EG[x_, y_, x1_] := expr[x, y] /. sol[x1]


Note I've used = in definition of expr, T2 instead of T2[x, y] as 2nd argument of NDSolve, and added [[1]] to remove a pair of {}.

• I am getting an error when I try this "Plot[{EG[0.2, y, 0.2], EG[0.3, y, 0.3]}, {y, 0, 180 Degree}]"
– zhk
Commented May 29, 2020 at 11:11
• @zhk Please show a complete code sample that reproduces the error. Currently xf is missing, and a Degree is probably missing inside NDSolve, too. Commented May 29, 2020 at 11:18
• @zhk Please edit your question to correct all of the mistakes. Commented May 29, 2020 at 11:28
• I am not doing anything extra with your code. I am just plotting using the above command I mentioned earlier along with xf=40, which returns InterpolatingFunction::femdmval:
– zhk
Commented May 29, 2020 at 14:13
• @zhk Please calculate ArcTanh[0.2] and think about what's wrong. Commented May 29, 2020 at 14:31