2
$\begingroup$

The integrand includes ku, Sin[x], and Tuu. ku is constant, x is one of the variables, and Tuu is functions of x and y.

According to the nice answer of KraZug, I have updated the program. I find that the program will give the errors "Integrand is not numerical \ at {x,y} =...", but at the given values {x,y} the integrand ku*Sin[x]*Tuu[x, y] can be easily evaluated. Why? How to solve the problem? Many thanks!

The updated codes are as follows.

Clear["`*"]
m = 2;
d = 2.106*(m + 1);
vh = 4;
mu = 11;
delta = 8;
HBAR = 1.05457266`14*10^(-34);
ME = 9.1093897`14*10^(-31);
ELEC = 1.60217733`14*10^(-19);
Kh = 2.106`14;
vKh[1] = {0, 0, 0};
vKh[2] = {Kh, 0, 0};
vKh[3] = {-Kh, 0, 0};
vKh[4] = {0, Kh, 0};
vKh[5] = {0, -Kh, 0};
vKh[6] = {Kh, Kh, 0};
vKh[7] = {-Kh, Kh, 0};
vKh[8] = {-Kh, -Kh, 0};
vKh[9] = {Kh, -Kh, 0};
kc = Sqrt[2*ME*ELEC/HBAR^2]*10^(-10);
ku = kc*Sqrt[mu + delta];
kd = kc*Sqrt[mu - delta];
a3 = {Pi/Kh, Pi/Kh, Sqrt[2]*Pi/Kh};
kuu = {-SetPrecision[ku*Sin[x]*Cos[y], 12], -SetPrecision[
     ku*Sin[x]*Sin[y], 12], kz};
fuu[i_, i_] := Total[(kuu + vKh[i])^2] - ku^2;
fuu[i_, j_] := 
 If[i == j, fuu[i, i], 
  kc^2*vh*Total[
    Table[Exp[I*n*Total[(vKh[j] - vKh[i])*a3]], {n, 0, m}]]]
tuu = Array[fuu, {9, 9}];
sluu[xx_, yy_] := 
 sluu[xx, yy] = 
  Select[kz /. NSolve[Det[tuu /. x -> xx /. y -> yy] == 0, kz], 
   Re[#] >= 0 && Im[#] >= 0 &]
tduu[xx_, yy_, l_] := 
  tduu[xx, yy, l] = 
   tuu /. x -> xx /. y -> yy /. kz -> sluu[xx, yy][[l]];
nuu[x_, y_, o_] := nuu[x, y, o] = Flatten[NullSpace[tduu[x, y, o]]];
piuu[x_, y_] := piuu[x, y] = Transpose[Table[nuu[x, y, p], {p, 9}]];
pei = Table[If[q == 1, 1, 0], {q, 9}];
psiuu[x_, y_, u_] := 
  psiuu[x, y, u] = 
   Total[Table[
     LinearSolve[piuu[x, y], pei][[s]]*nuu[x, y, s][[u]]*
      Exp[I*sluu[x, y][[s]]*dd], {s, 9}]];
Tuu[x_?NumericQ, y_?NumericQ] := 
  Im[Total[Table[
     Conjugate[psiuu[x, y, u]]*(Dt[psiuu[x, y, u], dd]) /. 
      dd -> d, {u, 9}]]];
Guu := NIntegrate[ku*Sin[x]*Tuu[x, y], {x, 0.01, Pi/2}, {y, 0, Pi/4}, 
   WorkingPrecision -> 6];
Guu
$\endgroup$
  • 1
    $\begingroup$ One of the advantages of MMA is the ability to calculate symbolically. Your use of SetPrecision is dangerous , I think. Try to give exact parameters for example HBAR=Rationalize[1.05457266*10^(-34), 0] . On MMA 11.0.1. Your function ` Tuuu[...] doesn't evaluate in finite time... $\endgroup$ – Ulrich Neumann Aug 24 '18 at 9:47
  • 1
    $\begingroup$ The code contains errors. What are you trying to figure out? Which one do you solve the problem? $\endgroup$ – Alex Trounev Aug 24 '18 at 11:29
  • 2
    $\begingroup$ Use = instead of := almost everywhere will help speed everything up. But currently fuu[i,i] is a vector of three elements instead of a value (like fuu[i,j]). $\endgroup$ – KraZug Aug 24 '18 at 12:57
  • $\begingroup$ @UlrichNeumann When I give the values of x and y, Tuu can be evaluated within about 4 seconds. I have tried Rationalze, but there will be errors. $\endgroup$ – user59546 Aug 25 '18 at 1:48
  • $\begingroup$ @AlexTrounev I do not find any errors, and the MMA also do not give any errors. Can you tell me where are the errors? $\endgroup$ – user59546 Aug 25 '18 at 1:49
1
$\begingroup$

I believe this does what you want. I have removed as many of the delayed evaluations (:=) as I could, used memoization to store the values of the others so they aren't calculated repeatedly and explicitly included $x$ and $y$ in the other definitions. Also, for some values of $x$ and $y$ the Nullspace wasn't working, giving errors of non-defined, so I decreased the tolerance there. Now my NIntegrate command is actually working, giving 2.11061 but with a warning about converging too slowly. Strangely, the value of Tuu doesn't seem to depend on $y$ very much at all - this comes back to Det[tuu] not seeming to have a strong dependence on $y$ either. If that is unexpected, then check what is going on.

Could certainly be optimized further.

Clear["`*"]
m = 3;
d = 2.106*(m + 1);
vh = 4;
mu = 11;
delta = 8;
HBAR = Rationalize[1.05457266`14*10^(-34), 0];
ME = Rationalize[9.1093897`14*10^(-31), 0];
ELEC = Rationalize[1.60217733`14*10^(-19), 0];
Kh = Rationalize[2.106`14, 0];
vKh[1] = {0, 0, 0};
vKh[2] = {Kh, 0, 0};
vKh[3] = {-Kh, 0, 0};
vKh[4] = {0, Kh, 0};
vKh[5] = {0, -Kh, 0};
vKh[6] = {Kh, Kh, 0};
vKh[7] = {-Kh, Kh, 0};
vKh[8] = {-Kh, -Kh, 0};
vKh[9] = {Kh, -Kh, 0};
kc = Sqrt[2*ME*ELEC/HBAR^2]*10^(-10);
ku = kc*Sqrt[mu + delta];
kd = kc*Sqrt[mu - delta];
a3 = {Pi/Kh, Pi/Kh, Sqrt[2]*Pi/Kh};
kuu = {-ku*Sin[x]*Cos[y], -ku*Sin[x]*Sin[y], kz};
fuu[i_, i_] := Total[(kuu + vKh[i])^2] - ku^2;
fuu[i_, j_] := 
 If[i == j, fuu[i, i], 
  kc^2*vh*Total[
    Table[Exp[I*n*Total[(vKh[j] - vKh[i])*a3]], {n, 0, m}]]]
tuu = Array[fuu, {9, 9}];
sluu[xx_, yy_] := 
 sluu[xx, yy] = 
  Select[kz /. NSolve[Det[tuu /. x -> xx /. y -> yy] == 0, kz], 
   Re[#] >= 0 && Im[#] >= 0 &]
tduu[xx_, yy_, l_] := 
  tduu[xx, yy, l] = 
   tuu /. x -> xx /. y -> yy /. kz -> sluu[xx, yy][[l]];
nuu[x_, y_, o_] := 
  nuu[x, y, o] = 
   Flatten[NullSpace[tduu[x, y, o], Tolerance -> 10^(-12)]];
piuu[x_, y_] := piuu[x, y] = Transpose[Table[nuu[x, y, p], {p, 9}]];
pei = Table[If[q == 1, 1, 0], {q, 9}];
psiuu[x_, y_, u_] := 
  psiuu[x, y, u] = 
   Total[Table[
     LinearSolve[piuu[x, y], pei][[s]]*nuu[x, y, s][[u]]*
      Exp[I*sluu[x, y][[s]]*dd], {s, 9}]];
Tuu[x_?NumericQ, y_?NumericQ] := 
  Im[Total[Table[
     Conjugate[psiuu[x, y, u]]*(Dt[psiuu[x, y, u], dd]) /. 
      dd -> d, {u, 9}]]];
Guu := Monitor[
   NIntegrate[ku*Sin[x]*Tuu[x, y], {x, 0.01, Pi/2}, {y, 0, Pi/4}], {x,
     y}];
Guu
| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you very much! I have further optimized the program according to your version. However, I find another problem. The errors say that Integrand 2.2331400000000 Sin[x] Tuu[x,y] is not numerical at {x,y} = {1.53075,0.392699}. But when I set x and y through copy the values, I find Tuu[x,y] can be evaluated. Why? How to solve this problem. The new codes have been updated as above. $\endgroup$ – user59546 Aug 26 '18 at 8:46
  • $\begingroup$ Did you copy the full values that Mathematica gave (I.e. to more decimal places than the 5 given there)? The error is the same as I fixed by setting the tolerance of the nullspace, so I would start there. $\endgroup$ – KraZug Aug 26 '18 at 19:37
  • $\begingroup$ I copy the full values. The error is because that the nullspace[tduu] gives {} due to the not enough precision. But now, nullspace[tduu] and Tuu give a valid result. I do not know why the NIntegrate give the error. $\endgroup$ – user59546 Aug 27 '18 at 2:13
  • $\begingroup$ Using the same codes as your above answers, if set m=3, it also can not give a result with the same error " Integrand 2.2331400000000 Sin[x] Tuu[x,y] is not numerical at {x,y} = ....". Why? $\endgroup$ – user59546 Aug 27 '18 at 3:54
  • $\begingroup$ @user59546, the code you have put into the question doesn't have the Tolerance on the Nullspace. $\endgroup$ – KraZug Aug 27 '18 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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