0
$\begingroup$

I would like to find the minimum of the function EER[a, v] using parallel computing.

If I write the code like this, it works:

ClearAll["Global`*"]

Kx[a_?NumericQ] := 
  NIntegrate[(E^(-((2 r)/a)) (-2 a + r))/(
    a^2 r NIntegrate[Exp[-(r/a)]^2 r^2, {r, 0, \[Infinity]}])*
    r^2, {r, 0, \[Infinity]}];

Ko[v_?NumericQ] := ((-1.05 + 0.69* v)* v)/(2.27 + v *(-3.02 + 1.1* v));

EER[a_, v_] := Ko[v]*Kx[a];

Exx = 
 ParallelTable[{v, 
   FindMinimum[{Etmp = EER[a, v], (0.1 < a < 30)}, {{a, 1}}, 
    EvaluationMonitor :> {Pause[0.1], 
      Print[" Current a=", a, " E=", Etmp]}]}, {v, {7.5, 10, 12.5, 
    15}}]

Now I want to denote part of the expression in Kx[a_?NumericQ] like K[a_?NumericQ] but in the case of such entry, the code stops working. Why doesn't the code work?

ClearAll["Global`*"]

K[a_?NumericQ] := (E^(-((2 r)/a)) (-2 a + r))/(
 a^2 r NIntegrate[Exp[-(r/a)]^2 r^2, {r, 0, \[Infinity]}])

Kx[a_?NumericQ] := NIntegrate[K[a]*r^2, {r, 0, \[Infinity]}];

Ko[v_?NumericQ] := ((-1.05 + 0.69* v)* v)/(2.27 + v *(-3.02 + 1.1* v));

EER[a_, v_] := Ko[v]*Kx[a];

Exx = 
 ParallelTable[{v, 
   FindMinimum[{Etmp = EER[a, v], (0.1 < a < 30)}, {{a, 1}}, 
    EvaluationMonitor :> {Pause[0.1], 
      Print[" Current a=", a, " E=", Etmp]}]}, {v, {7.5, 10, 12.5, 
    15}}]
$\endgroup$
4
  • 4
    $\begingroup$ Short answer: Do not use K, rename to K2 for example. Longer answer: K is a built-in symbol, in particular Context[K] is System` . This means that the definition you have given for K is not automatically distributed to other kernels, only definitions for symbols in Global` are. $\endgroup$
    – user293787
    Dec 21, 2022 at 19:24
  • $\begingroup$ @user293787, thank you very much! $\endgroup$
    – Mam Mam
    Dec 21, 2022 at 19:35
  • 2
    $\begingroup$ use small k, always in Mathematica avoid using capitals, because they are mostly reserved for predefined functions $\endgroup$
    – nufaie
    Dec 21, 2022 at 20:25
  • $\begingroup$ The list of capital letters that are built in mathematica.stackexchange.com/q/117877/86543 $\endgroup$ Dec 21, 2022 at 21:52

1 Answer 1

0
$\begingroup$
Clear["Global`*"]

Your functions can be evaluated symbolically

Kx[a_] = Integrate[(E^(-((2 r)/a)) (-2 a + r))/
    (a^2 r Integrate[Exp[-(r/a)]^2 r^2, {r, 0, ∞}])*
   r^2, {r, 0, ∞}]

enter image description here

Ko[v_] =
 ((-1.05 + 0.69*v)*v)/(2.27 + v*(-3.02 + 1.1*v)) //
   Rationalize // FullSimplify

enter image description here

EER[a_, v_] = Ko[v]*Kx[a]

enter image description here

min[v_] = MinValue[{EER[a, v] // Normal,
   1/10 <= a <= 30}, a]

enter image description here

arg[v_] := ArgMin[{EER[a, v] // Normal,
   1/10 <= a <= 30}, a]

Off[ArgMin::wksol]

Show[
 Plot3D[EER[a, v], {v, 15/2, 15}, {a, 1/10, 1},
  AxesLabel -> (Style[#, 14] & /@ {v, a, EER}),
  ClippingStyle -> None,
  PlotRange -> All],
 Graphics3D[{Red, AbsolutePointSize[4],
   Point[{#, arg[#], min[#]} & /@
     Range[15/2, 15, 1/2]]}]]

enter image description here

Prepend[
  N@{#, arg[#], min[#]} & /@ Range[15/2, 15, 1/2],
  {v, a, "E"}] // Grid[#, Frame -> All] &

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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