Translating Matlab to EMT

Here is an example of the troubles you might face when you translate a Matlab code to Euler Math Toolbox. I found the following code in a script.

programm eigs.m
01 n=1:99;m=sqrt(n);
02 aminus = diag(m,1);
03 aplus = diag(m,-1);
04 x= 1/sqrt(2)*(aminus + aplus);
05 p=-i/sqrt(2)*(aminus - aplus);
06 H0 = 0.5*(x^2 + p^2);
07 H0(1:5,1:5)
08 pause
09 H1 = 0.5*p^2 + 0.25* x^4;
10 E = eig(H1);
11 E = sort(E);
12 E(1:5)

The code is documented after the script line by line, so that we can make sense of it. There are some easy syntactic translations such as the missing space in the first line, the line numbers (which are not accepted by Matlab itself), the round brackets to index the matrices. From the documentation, you will find that the diag() function works differently, and that „i“ is „I“ or „1i“ in EMT. The power of matrices is not computed by x^2 in EMT, which implements the matrix language strictly. Instead, we can use matrix repeated multiplication or the function matrixpower().

There is a deeper problem. It seems that H0 and H1 automatically become real matrices in Matlab. This is strange and it not clear what imaginary parts get cancelled automatically in Matlab. In fact, Euler cannot find the eigenvalues of a 100×100 complex matrix. For real matrices, it uses effective methods, however. In the end, here is a possible translation.

>N=100; n=1:N-1; m=sqrt(n);
>aminus = diag(N,N,1,m);
>aplus = diag(N,N,-1,m);
>x= 1/sqrt(2)*(aminus + aplus);
>p=-I/sqrt(2)*(aminus - aplus);
>H0 = 0.5*(x.x+p.p);
>real(H0[1:5,1:5])
           0.5             0             0             0             0 
             0           1.5             0             0             0 
             0             0           2.5             0             0 
             0             0             0           3.5             0 
             0             0             0             0           4.5 
>H1 = 0.5*p.p+0.25*x.x.x.x;
>E = jacobi(real(H1));
>E = sort(real(E));
>E[1:5]
 [0.420805,  1.5079,  2.9588,  4.62122,  6.45351]

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.