Help with loops in Mathematica (Hamiltonian)

In summary, the solution for generating a 7x7 matrix for the Hamiltonian (H) of the Schrodinger equation involves using a 2D table and the KroneckerDelta function to determine the value of each element. This can be done in Mathematica by creating a table that checks for the conditions of i=j, i-j=1, and i-j=-1, and assigns the appropriate values using the parameters g, μB, B, and Ωrf.
  • #1
vankoks
1
0

Homework Statement



I have an assignment for my thesis to make Hamiltonian for Schrodinger equation. I won't go into physics part of it, because that is well understood.

I need to somehow generate a specific matrix for Hamiltonian (H).

Please see the attached file of what I need to get.

Homework Equations



See attached file

The Attempt at a Solution



I have thought of solution for this in C, but I don't know how to syntax this in Mathematica.

The solution for this in C would be to generate 7x7 matrix where elements (i,j) would be made by:

If
i=j
ΔE
Else
If
i-j=1
Ω(rf) complex conjugate
i-j=-1
Ω(rf)
Else
0
 

Attachments

  • hamiltonian.png
    hamiltonian.png
    6.6 KB · Views: 425
Physics news on Phys.org
  • #2
The best way I know is to create a 2D table and use KroneckerDelta to decide the value of each element in the table.
 
  • #3
In[1]:= H = Table[
Which[
i == j, g*\[Mu]B*ToExpression["m" <> ToString]*B,
i - j == 1, Conjugate[\[CapitalOmega]rf],
i - j == -1, \[CapitalOmega]rf,
True, 0
], {i, 7}, {j, 7}
]

Out[1]= {{B g m1 \[Mu]B, \[CapitalOmega]rf, 0, 0, 0, 0, 0},
{Conjugate[\[CapitalOmega]rf], B g m2 \[Mu]B, \[CapitalOmega]rf, 0, 0, 0, 0},
{0, Conjugate[\[CapitalOmega]rf], B g m3 \[Mu]B, \[CapitalOmega]rf, 0, 0, 0},
{0, 0, Conjugate[\[CapitalOmega]rf], B g m4 \[Mu]B, \[CapitalOmega]rf, 0, 0},
{0, 0, 0, Conjugate[\[CapitalOmega]rf], B g m5 \[Mu]B, \[CapitalOmega]rf, 0},
{0, 0, 0, 0, Conjugate[\[CapitalOmega]rf], B g m6 \[Mu]B, \[CapitalOmega]rf},
{0, 0, 0, 0, 0, Conjugate[\[CapitalOmega]rf], B g m7 \[Mu]B}}
 

Related to Help with loops in Mathematica (Hamiltonian)

1. How do I create a loop in Mathematica to iterate through a list of values?

To create a loop in Mathematica, you can use the Do[ ] or For[ ] functions. These functions allow you to specify the start and end values of the loop, as well as the increment or decrement. For example, Do[i=i+1, {i, 1, 10, 2}] will loop through the values of i from 1 to 10, incrementing by 2 each time.

2. How can I break out of a loop in Mathematica?

You can use the Break[] function to exit a loop in Mathematica. This function will immediately stop the iteration and return to the outermost level of the loop. You can also use Return[ ] to exit a loop and return a specific value.

3. How do I access the current iteration index within a loop in Mathematica?

You can use the Table[ ] function to create a list of values based on a loop, and Map[ ] or Apply[ ] functions to apply a function to each iteration. These functions automatically assign an index to each iteration, which you can access using # or Slot[ ] notation. For example, Table[2#&, {i, 1, 5}] will create a list of multiples of 2, where # represents the current iteration index.

4. How do I use nested loops in Mathematica?

You can use multiple Do[ ] or For[ ] functions to create nested loops in Mathematica. The inner loop will iterate through all values for each iteration of the outer loop. It is important to keep track of the index variables and ensure they are properly incremented or decremented to avoid infinite loops.

5. Can I use conditional statements within a loop in Mathematica?

Yes, you can use If[ ] or Switch[ ] statements within a loop in Mathematica to add conditional logic. These statements will be evaluated for each iteration, and the loop will continue or break based on the condition. You can also use Continue[ ] to skip to the next iteration without exiting the loop.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Atomic and Condensed Matter
5
Replies
156
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Special and General Relativity
Replies
1
Views
691
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
Back
Top