1
$\begingroup$
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?

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

1 Answer 1

4
$\begingroup$

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

$\endgroup$
9
  • $\begingroup$ 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}]" $\endgroup$
    – zhk
    May 29, 2020 at 11:11
  • $\begingroup$ @zhk Please show a complete code sample that reproduces the error. Currently xf is missing, and a Degree is probably missing inside NDSolve, too. $\endgroup$
    – xzczd
    May 29, 2020 at 11:18
  • $\begingroup$ @zhk Please edit your question to correct all of the mistakes. $\endgroup$
    – xzczd
    May 29, 2020 at 11:28
  • $\begingroup$ 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: $\endgroup$
    – zhk
    May 29, 2020 at 14:13
  • 1
    $\begingroup$ @zhk Please calculate ArcTanh[0.2] and think about what's wrong. $\endgroup$
    – xzczd
    May 29, 2020 at 14:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.