Skip to main content Skip to navigation

Code to find the median eigenvalue of a 3x3 matrix

program eigenvalues

implicit none

cccccc DECLARATIONS ccccccccccccccccccccccccccccccccccccccccccccccccccccccc

complex :: L1,L2,L3, z1,z2, alpha,beta

complex :: x11,x12,x13,x21,x22,x23,x31,x32,x33

real :: R1,R2,R3, median, a,b,c,c1,c2, third


a=1

b=3

third=a/b

cccccc INPUT cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

print*, 'x11: '
read*,x11

print*, 'x12: '
read*,x12

print*, 'x13: '
read*,x13

print*, 'x21: '
read*,x21

print*, 'x22: '
read*,x22

print*, 'x23: '
read*,x23

print*, 'x31: '
read*,x31

print*, 'x32: '
read*,x32

print*, 'x33: '
read*,x33



cccccc solution to eigenvector equation ccccccccccccccccccccccccccccc

a= -1*(x11+x22+x33)

b= x11*x22 + x11*x33 - x12*x21 - x13*x31 + x22*x33 - x23*x32

c1= x11*x22*x33- x11*x23*x32- x12*x21*x33

c2= x12*x23*x31+ x13*x21*x32- x13*x22*x31

c= -1*(c1+c2)

print*, a, ', ', b, ', ', c


cccccc Lagrange resolvent solution to the cubic ccccccccccccccccccccccccccc



alpha= 9*a*b-2*a**3-27*c

beta= (3*b-a**2)**3


z1= (alpha+((alpha)**2+4*beta)**0.5)/2

z2= (alpha- ((alpha)**2+4*beta)**0.5)/2


L1= (-1*a+z1**(third)+z2**(third))/3


cccccc remaining roots cccccccccccccccccccccccccccccccccccccccccccccccccccc

L2= (-(a+L1)+((a+L1)**2-4*(b+a*L1+L1**2))**0.5)/2

L3= (-(a+L1)-((a+L1)**2-4*(b+a*L1+L1**2))**0.5)/2

print*,L1,', ',L2,', ',L3

cccccc selection of median eigenvalue ccccccccccccccccccccccccccccccccccccc
R1=real(L1)
R2=real(L2)
R3=real(L3)


if (R1==R2) then
median=R2

else if (R1==R3.or.R2==R3) then
median=R3

else if (R1>R2.and.R1>R3) then
if (R2>R3) then
median=R2
else
median=R3
endif

else if (R2>R1.and.R2>R3) then
if (R1>R2) then
median=R1
else
median=R3
endif

else if (R2>R1) then
median=R2

else
median= R1

endif

print*, median


end program eigenvalues