Seeking help in Mathematica syntax

  • Thread starter Thread starter dmriser
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting syntax errors in a Mathematica program designed to analyze a quantum system. Participants are focused on resolving issues related to the structure of a Module and the proper definition of functions within it.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The initial poster describes a problem with creating a function in Mathematica that takes six parameters and returns a result, mentioning specific error messages encountered.
  • One participant points out a misplaced comma that may be causing the Module to interpret the number of arguments incorrectly.
  • The initial poster acknowledges the correction but encounters new errors related to local variable specifications and the use of patterns in function definitions.
  • Another participant expresses confusion over the use of Pattern in the output, noting it is not documented in a familiar way.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the correct use of Pattern in Mathematica, and the discussion remains unresolved regarding the proper syntax and structure of the code.

Contextual Notes

Errors mentioned include issues with local variable declarations, the interpretation of function parameters, and the matching of delimiters. The discussion highlights the complexity of Mathematica syntax and the challenges faced by those new to the language.

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.