Seeking help in Mathematica syntax

  • Thread starter dmriser
  • Start date
  • Tags
    Mathematica
In summary: What was the point of using it there? I'm guessing you mean the use of "Pattern[1,_]"? That just means "1, any expression". Pattern is a way to match expressions, so in this case it's just a way to make the output look a little cleaner.
  • #1
dmriser
50
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
  • #2
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?
 
  • #3
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.
 
  • #4
Never seen that use of Pattern before; can't find anything similar in the documentation.
 
  • #5




I understand the importance of properly coding and analyzing data in order to accurately interpret results. In this case, it seems like you are encountering some syntax errors in your Mathematica code. The error messages you have provided indicate that there may be issues with the number of arguments you are passing to your functions, or with using protected functions. I would suggest checking your code for any typos or missing symbols, and also referring to the Mathematica documentation for guidance on proper syntax for functions and modules. Additionally, you may want to seek help from more experienced Mathematica users or consult online forums for assistance with troubleshooting your code. With some careful debugging and adjustments, I am confident you will be able to successfully create your program to analyze the quantum system. Good luck!
 

FAQ: Seeking help in Mathematica syntax

What is Mathematica syntax?

Mathematica syntax refers to the specific set of rules and symbols used in the Mathematica programming language. This includes the use of square brackets for function arguments, the use of equal signs to assign values, and the use of semicolons to separate multiple expressions.

How can I improve my understanding of Mathematica syntax?

One of the best ways to improve your understanding of Mathematica syntax is by practicing and experimenting with the language. You can also refer to the official Mathematica documentation, which provides detailed explanations and examples of syntax usage.

Can I use other programming languages in Mathematica?

Yes, Mathematica has the ability to integrate with other programming languages like Python and Java. You can use Mathematica as a front-end interface to these languages, allowing you to use their functionality within your Mathematica code.

How do I troubleshoot errors in my Mathematica syntax?

If you encounter errors in your Mathematica code, you can use the built-in debugger or the "Check" function to identify and fix the issues. You can also refer to the documentation or online forums for common syntax errors and their solutions.

Is there a limit to the complexity of Mathematica syntax?

There is no inherent limit to the complexity of Mathematica syntax. However, as with any programming language, it is important to keep your code organized and efficient to avoid confusion and potential errors.

Back
Top