1
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?

zhk
  • 11,939
  • 1
  • 22
  • 38

1 Answers1

4

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 {}.

xzczd
  • 65,995
  • 9
  • 163
  • 468
  • 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 May 29 '20 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. – xzczd May 29 '20 at 11:18
  • @zhk Please edit your question to correct all of the mistakes. – xzczd May 29 '20 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 May 29 '20 at 14:13
  • 1
    @zhk Please calculate ArcTanh[0.2] and think about what's wrong. – xzczd May 29 '20 at 14:31
  • ArcTanh[0.2]>0.2 is the reason I am getting this error. – zhk May 29 '20 at 15:02
  • @zhk Yes, and you'll see the General::ivar warning again, but this time the reason is slightly different: it's because the y is noticed by Plot. There're many possible solutions, here are two: 1. Control the evaluation again: Plot[{EG[0.21, y, 0.2], EG[0.31, y, 0.3]} // Evaluate, {y, 0, 180 Degree}, PlotRange -> All, PlotPoints -> 200, MaxRecursion -> 0] 2. Use another variable name in Plot: Plot[{EG[0.21, yy, 0.2], EG[0.31, yy, 0.3]}, {yy, 0, 180 Degree}, PlotRange -> All, PlotPoints -> 50, MaxRecursion -> 0] – xzczd May 30 '20 at 01:07
  • One last thing, how to avoid the error in this NN[x2_] := NIntegrate[EG[x, y, x2], {x, x2, xf}, {y, 0, 180 Degree}] – zhk May 30 '20 at 01:32
  • @zhk Once again, use Evaluate, as I've done for Plot. – xzczd May 30 '20 at 01:48