0
$\begingroup$

Hi everybody: I have a 4x4 matrix M see below, where for each value of h I have corresponding a,b,c,d values (please see the link below). I need to evaluate the matrix M for each value of h and corresponding a,b,c,d to determine the eigenvalue and eigenvectors and save them in .txt file. So far, I have tried following way, where I can compute the said matrix by specifying the values separately but I couldn't set up a loop. I would really appreciate any help.

In this file, for each value of h we have a, b, c and d values.

Thanks in advance.

`

Mode1 = Import["CF.txt", "Table"];
M = {{h, 0, a, c}, {0, h, d, b}, {a, c, h/2, 0}, {b, d, 0, h/2}};
{eigs, vecs} = Eigensystem[M];
list1 = Partition[Riffle[eigs, Chop[vecs]], 2];
list2 = Sort[list1, #1[[1]] < #2[[1]] &];
list2 // MatrixForm

`

$\endgroup$
2
  • $\begingroup$ Try this f[v_]:=Sort[v,N[#1[[1]]]<N[#2[[1]]]&]; Mode1=Import["CF.txt","Table"]; Map[({h,a,b,c,d}=#; M={{h,0,a,c},{0,h,d,b},{a,c,h/2,0},{b,d,0,h/2}}; {eigs,vecs}= Eigensystem[M]; list1=Partition[Riffle[eigs,Chop[vecs]],2]; list2=f[list1]; list2 // MatrixForm)&, Mode1] and verify it is doing the assignment correctly for you $\endgroup$
    – Bill
    May 3, 2020 at 4:09
  • $\begingroup$ Hi @Bill , first of all thank you very much I verified the results and its working. But I am trying to export the results using Export["CF.dat", list2] within a Map, it will generate multiple files equivalent to the values of h, when I introduce outside of Map function it will generate only last eigenvalues and eigenvectors. Is it possible, that we can write all the values corresponding to each h in one file. $\endgroup$
    – Aman
    May 3, 2020 at 14:00

1 Answer 1

1
$\begingroup$

If you want to export all the results from Map then this seems very simple to do:

f[v_]:=Sort[v,N[#1[[1]]]<N[#2[[1]]]&];
Mode1=Import["CF.txt","Table"];
listall=Map[(
  {h,a,b,c,d}=#;
  M={{h,0,a,c},{0,h,d,b},{a,c,h/2,0},{b,d,0,h/2}};
  {eigs,vecs}= Eigensystem[M];
  list1=Partition[Riffle[eigs,Chop[vecs]],2];
  f[list1])&, Mode1
];
Export["CF.dat", listall]
$\endgroup$
1
  • $\begingroup$ Thank you very much @Bill. Its working. $\endgroup$
    – Aman
    May 3, 2020 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.