Seeking help in Mathematica syntax

  • Thread starter Thread starter dmriser
  • Start date Start date
  • Tags Tags
    Mathematica
AI Thread Summary
The discussion revolves around troubleshooting Mathematica syntax issues while creating a program for analyzing a quantum system. The user is attempting to define a function, CWF, that utilizes a Module to encapsulate various calculations but encounters multiple errors related to argument specifications and local variable declarations. Key errors include incorrect argument passing to the Module and issues with variable definitions leading to unexpected outputs. After some corrections, the user still faces challenges with the integration process and the use of the Pattern function, which is unfamiliar. The conversation highlights the importance of syntax accuracy and understanding Mathematica's unique function definitions.
dmriser
Messages
49
Reaction score
0

Homework Statement



I am creating a program to help me analyze a quantum system. I am having a problem creating a large function that I can pass 6 initial values to and get the result out (IT).

I am new to this language but have been asked to code in it.


Homework Equations


I am trying to use a Module to encapsulate my code in, however I cannot succeed.


The Attempt at a Solution




CWF[a1_, a2_, r1_, r2_, s1_, s2_] = Module[{w1_, w2_, nw1_,
nw2_, f1_, f2_, nf1_, nf2_, I1_, I2_, IT_},
w1[r1_, a1_] := r1 Exp[-a1 r1]; (* DEFINE
THE W OPERATOR AS 2 PARTS, SEPERATE FUNCTIONS OF R1/A1 AND R2/A2 *)
w2[r2, a2] := r2 Exp[-a2 r2] ;

nw1[r1_, a1_] := w1[r1, a1] / (Sqrt[
Integrate[w1[r1, a1]^2, {r1, 0, Infinity}]]) ;(* NORMALIZE BOTH \
FUNCTIONS AND CREATE A NEW FUNCTION FOR EACH *)
nw2[r2_, a2_] := w2[r2, a2] / (Sqrt[
Integrate[w2[r2, a2]^2, {r2, 0, Infinity}]]);

f1[r1_, s1_] := r1 Exp[-s1 r1]; (* CREATE THE FUNCTIONS F1 AND F2 *)
f2[r2_, s2_] := r2 Exp[-s2 r2];

nf1[r1_, s1_] := f1[
r1, s1] / (Sqrt[Integrate[f1[r1, s1]^2; {r1, 0, Infinity}]]), (*
NORMALIZE BOTH FUNCTIONS AND CREATE A NEW FUNCTION FOR EACH *)
nf2[r2_, s2_] := f2[r2, s2] / (Sqrt[Integrate[f2[r2, s2]^2; {r2, 0,
Infinity}]]),

whhw[r1_, r2_, a1_, a2_, s1_, s2_] := \
(* CONSTRUCT THE INTEGRAND *)
((nw1 * nw2) * (- D[(nf1*nf2), {r1, 2}] - D[(nf1*nf2), {r2, 2}]))
+ D[(nw1*nw2*nf1*nf2), {r1, 2}] +
D[(nw1*nw2*nf1*nf2), {r2, 2}];

I1[r1_, r2_, a1_, a2_, s1_, s2_] := (* INTEGRATE OVER R1 *)
Integrate[whhw, {r1, 0, Infinity}];

I2[r1_, r2_, a1_, a2_, s1_, s2_] := (* INTEGRATE OVER R2 *)
Integrate[whhw, {r2, 0, Infinity}];

IT[r1_, r2_, a1_, a2_, s1_, s2_] := (* OUTPUT THE SUM OF
THOSE INTEGRALS OVER R1 AND R2 *)
I1[r1, r2, a1, a2, s1, s2] + I2[r1, r2, a1, a2, s1, s2];

IT
]


The error message is:
1. Module::argrx: Module called with 4 arguments; 2 arguments are expected.
2. Set::write: Tag Function in Function[{a1_, a2_, r1_, r2_, s1_, s2_}, \
\[LeftSkeleton]1\[RightSkeleton]][a1_, a2_, r1_, r2_, s1_, s2_] is Protected.

Then the output is a long mess that is my code. When I try to call the function with some values (for example CWF[1,1,1,1,1,1]) the error is as follows:

1. Function::flpar: Parameter specification {a1_, a2_, r1_, r2_, s1_, s2_} in \
Function[{a1_, a2_, r1_, r2_, s1_, s2_}, \[LeftSkeleton]1\[RightSkeleton]] \
should be a symbol or a list of symbols


I suspect that the problem may actually not be that complex but a few simple syntax errors. I hope that I have posted this in the correct place.
 
Physics news on Phys.org
First problem: the comma before "(*NORMALIZE BOTH FUNCTIONS..." is misplaced. You probably have another misplaced comma later on; that's why M thinks you're passing 4 arguments into Module.

Do all your delimiters match up?
 
Thank you for pointing that out. After correcting that comma and another one that was further down in the code I received a few new errors.

1. Module::lvsym: Local variable specification {w1_, w2_, nw1_, nw2_, f1_, f2_, \
nf1_, nf2_, I1_, I2_, \[LeftSkeleton]1\[RightSkeleton]} contains w1_ which is \
not a symbol or an assignment to a symbol.

- I am guessing that this error has something to do with my declaration of local variables inside of the module, perhaps they don't need to be defined at the top.

2. Rule::rhs: Pattern r1_ appears on the right-hand side of rule \
CWF[a1_, a2_, r1_, r2_, s1_, s2_]->Module[{w1_, w2_, nw1_, nw2_, \
f1_, f2_, nf1_, nf2_, I1_, I2_, \[LeftSkeleton]1\[RightSkeleton]},\
\[LeftSkeleton]1\[RightSkeleton]]

- I'm at a loss as to what this error means.

When I run the program now, it progresses more and actually takes some of the values and uses them but still fails to complete the calculation. That output contains lots of this.. "Pattern[1,_]"

\!\(Module[{w1_, w2_,
nw1_, nw2_, f1_, f2_, nf1_, nf2_,
I1_, I2_, IT_}, w1[Pattern[1, _], Pattern[0.1`, _]] :=
1\ \[ExponentialE]\^\(-0.1`\); w2[1, 0.1`] := 1\ \
\[ExponentialE]\^\(-0.1`\); nw1[Pattern[1, _], Pattern[
0.1`, _]] := w1[1, 0.1`]\/\@\(∫\_0\%∞\( w1[1, 0.1`]\^2\) \
\[DifferentialD]1\); nw2[Pattern[1, _], Pattern[0.1`, _]] := w2[1, \
0.1`]\/\@\(∫\_0\%∞\( w2[1, 0.1`]\^2\) \[DifferentialD]1\); f1[Pattern[1, _], \
Pattern[0.02`, _]] := 1\ \[ExponentialE]\^\(-0.02`\); f2[Pattern[1, _], \
Pattern[0.05`, _]] := 1\ \[ExponentialE]\^\(-0.05`\); nf1[Pattern[1, _], \
Pattern[0.02`, _]] := f1[
1, 0.02`]\/\@Integrate[f1[1,
0.02`]\^2; {1, 0, ∞}]; nf2[Pattern[1, _], Pattern[
0.05`, _]] := f2[1, 0.05`]\/\@Integrate[f2[1, 0.05`]\^2; {1, 0, ∞}]; \
whhw[Pattern[1, _], Pattern[1, _], Pattern[0.1`, _], Pattern[
0.1`, _], Pattern[0.02`, _], Pattern[
0.05`, _]] := \((\((nw1\ nw2)\)\ \((\(-∂\_{
1, 2}\((nf1\ nf2)\)\) - ∂\_{1, 2}\((nf1\ nf2)\))\))\)\ \((\(+∂\_{
1, 2}\((nw1\ nw2\ nf1\
nf2)\)\) + ∂\_{1, 2}\((nw1\ nw2\ nf1\ nf2)\))\); I1[Pattern[1, _], \
Pattern[1, _], Pattern[0.1`, _], Pattern[0.1`, _],
Pattern[0.02`, _], Pattern[0.05`, _]] := ∫\_0\%∞
whhw \[DifferentialD]1;
I2[Pattern[1, _], Pattern[1, _], Pattern[0.1`, _], Pattern[
0.1`, _], Pattern[0.02`, _], Pattern[0.05`, _]] := ∫\_0\%∞ whhw \
\[DifferentialD]1; IT[Pattern[1, _], Pattern[1, _], Pattern[0.1`, _], \
Pattern[0.1`, _], Pattern[0.02`, _], Pattern[0.05`, _]] := I1[1, 1, 0.1`,
0.1`, 0.02`, 0.05`] + I2[1, 1, 0.1`, 0.1`, 0.02`, 0.05`]; IT]\)

I believe that now my delimiters are all matching yes. Thank's for the help and the close look to catch the comma.
 
Never seen that use of Pattern before; can't find anything similar in the documentation.
 
Back
Top