1
$\begingroup$

Why does Mathematica not see the c11 and c12 variables in the function PxB1[a10_, b10_, qa0_, qb0_, c11_, c12_]? How to fix it?

ClearAll["Global`*"]
imax = 1; jmax = 2;
Psi[r_, z_, i_, j_] := Exp[-b[j]*z^2]*Exp[-a[i]*r^2];
c[i_, j_] := Symbol["c" <> ToString[i] <> ToString[j]]
a[1] = a1;
b[1] = b1;
Do[a[i] = a[i - 1] qa, {i, 2, imax}];
Do[b[j] = b[j - 1] qb, {j, 2, jmax}];
VB[r_, z_] = -(1/(2*Sqrt[r^2 + z^2]))*Exp[-Sqrt[r^2 + z^2]*1/2];
Px1B1[a10_, b10_, qa0_, qb0_, i1_, j1_, i2_, j2_] := 
  Block[{a1 = a10, b1 = b10, qa = qa0, qb = qb0}, 
   NIntegrate[
    Psi[r, z, i2, j2]*VB[r, z]*Psi[r, z, i1, j1]*r, {r, 
     0, \[Infinity]}, {z, -Infinity, Infinity}]];
(*Px1B1[1.22,0.44,1.40,1.81,1,2,1,1]
-0.18717992373162834`*)
PxB1[a10_, b10_, qa0_, qb0_, c11_, c12_] := 
  2 Pi Sum[c[i1, j1] c[i2, j2] Px1B1[a10, b10, qa0, qb0, i1, j1, i2, 
      j2], {i1, 1, imax}, {i2, 1, imax}, {j1, 1, jmax}, {j2, 1, jmax}];

PxB1[1.22, 0.44, 1.40, 1.81, 1, 2]


Out[1410]= 2 (-0.202823 c11^2 - 0.37436 c11 c12 - 
   0.175471 c12^2) \[Pi]
$\endgroup$

2 Answers 2

2
$\begingroup$

It is much better if you make a small example of the issue. Sometimes when you make a small example, you'll find the problem your self. What I think you are asking is this

p = 3*a + b;
ClearAll[foo];
foo[a_, b_] := Module[{},
  Print[p]
  ]
foo[9, 6]

And you are asking why p do not print as 3*9+6 or 33 Is this correct?

If so, it is simply because Mathematica sees a and b in the arguments as 9 and 6. So by the time the call is made, inside foo, there is only numbers 9 and 6. So the global p still have its a and b as symbols.

To workaround this, you can do

p = 3*a + b;
ClearAll[foo];
foo[aa_, bb_] := Module[{},
  Print[p /. {a -> aa, b -> bb}]
  ]
foo[9, 6]

Mathematica graphics

The arguments passed in a and b are not the same a and b inside your global variable. These are just numbers.


Another possibility is that you could be asking why the following does not work as you expect:

p = 3*a + b;
ClearAll[foo];
foo[a0_, b0_] := Module[{a = a0, b = a0},
  Print[p]
  ]
foo[9, 6]

This still gives 3 a+b and not 33. This is because when you do {a = a0, b = a0}, these do not assign values to the global a,b. These a,b are module local variables and have different context even though on the screen the names look the same.

If this is not what you are asking let me know so I can delete this.

$\endgroup$
1
  • $\begingroup$ Thank you very much! That is, if we now turn to the code, then I need to add one more line? PxB11[a10_, b10_, qa0_, qb0_, c111_, c121_] := Module[{}, PxB1[a10, b10, qa0, qb0] /. {c11 -> c111, c12 -> c121}] $\endgroup$
    – Mam Mam
    May 26, 2023 at 6:49
3
$\begingroup$

Your definition does not evaluate with := inside Sum, because the definition of c involves argument list checking.

To avoid generation of a complete variables list first, replace the definition by giving c a general definition

       c = Symbol[StringJoin[ "c", ToString[##]] &;
$\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.