Problem bei Darstellung eines Eigenwerts. vielleicht falscher Datentyp?

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
gulo
User
Beiträge: 1
Registriert: Montag 13. September 2021, 13:34

hallo :)
ich bin noch ein Anfänger, was Python angeht. Deshalb brauche ich eure Hilfe.
Ich wollte nur einen Eigenwert des Parameters h darstellen, aber mir wird folgende Fehlermeldung angezeigt

ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Ich habe auch schon das Internet durchforstet, aber bekomme das Problem nicht gelöst

import numpy as np
from scipy.spatial.transform import Rotation as R
import uncertainties.unumpy as unp
import matplotlib.pyplot as plt
import scipy.constants as const
#import pandas as pd
from scipy.stats import stats
from sklearn.preprocessing import normalize
from math import cos, sin, sqrt
from numpy import linalg
import cmath
from matplotlib.widgets import Slider, Button, RadioButtons

#Drehmatrix um Richtung (axis) um Winkel phi
def rotation(axis,phi,vec):
return (R.from_rotvec(np.radians(phi)*axis)).apply(vec)


zcrys=np.array([1,-1,0])
ycrys=np.array([1,1,0])
xcrys=np.array([0,0,1])
#Vektoren normieren
normalized_zcrys = zcrys/np.linalg.norm(zcrys)
normalized_ycrys = ycrys/np.linalg.norm(ycrys)
normalized_xcrys = xcrys/np.linalg.norm(xcrys)

k=normalized_zcrys
B=normalized_xcrys
#ε = 12.120 meV
ε=12100 #*10**(-22)*1.602
i=complex(0,1)
a=92.5
b=47.7
delta3=0
delta5=0
x=1/sqrt(2)
y=-1/sqrt(2)
z=0
B=normalized_xcrys
def H(h):
return ([ [-ε ,i*a*h*B[0],i*a*h*B[1],i*a*h*B[2]],
[-i*a*h*B[0],0,-i*b*h*B[2],i*b*h*B[1]],
[-i*a*h*B[1],i*b*h*B[2],0,-i*b*h*B[0]],
[-i*a*h*B[2],-i*b*h*B[1],i*b*h*B[0],0]]) #(4,4)
#H=H(h)
M1=delta3*np.matrix([[0,0,0,0],
[0,0,x*y,x*z],
[0,x*y,0,y*z],
[0,x*z,y*z,0]])
M2=delta5*np.matrix([[0,0,0,0],
[0,2*x*x-y*y-z*z,0,0],
[0,0,2*y*y-x*x-z*z,0],
[0,0,0,2*z*z-x*x-y*y]])
#M=H+M1+M2

def EW(h):
return (linalg.eig(H(h))[0])

#print(np.round(EW(0.1),3))
#print(EW(0.1).shape)
#print(np.round(EV[1],3))

xlist=np.linspace(0,10,10)


plt.plot(xlist,EW(xlist)[0],color='red')

plt.show()
rogerb
User
Beiträge: 878
Registriert: Dienstag 26. November 2019, 23:24

Hallo gulo,

die Matrixberechnung (siehe Kommentar im Code) erstellt eine zweidimensionale Liste aus Vektoren und Skalaren vermischt. Das kann man nicht an die Funktion np.linalg.eig() übergeben.
Da ich nicht weiß was du da eigentlich berechnen willst kann ich das auch nicht ändern.

Wenn man seinen Code mit anderen teilt und besonders wenn man Fehler hat, lohnt es sich erstmal aufzuräumen:

Du verwendest einige ungenutzte Importe das macht den Code schwerer zu lesen.
Mal verwendest du "linalg" und mal verwendest du "np.linalg"
Funktionsnamen sollten klein geschrieben werden und einen aussagekräftigen Namen haben. Bei H(h) weißt du vielleicht was gemeint ist, aber andere sind verwirrt.
"B=normalized_xcrys" und "k=normalized_zcrys" kommt mehrmals vor
Die Funktion "rotation" wird nicht verwendet
Die Matrizen M1 und M2 werden auch nicht verwendet
Fast alle Funktions- und Variablennamen haben keine Aussagekraft.
Ich habe es etwas aufgeräumt. Ich würde dir aber empfehlen auch die anderen Namen zu ändern.

Code: Alles auswählen

import numpy as np
import matplotlib.pyplot as plt


def eigenwerte(h):
    return np.linalg.eig(h_funktion(h))[0]


def h_funktion(h):
    ε = 12100  # *10**(-22)*1.602
    i = complex(0, 1)
    a = 92.5
    b = 47.7
    xcrys = np.array([0, 0, 1])
    normalized_xcrys = xcrys / np.linalg.norm(xcrys)
    bx, by, bz = normalized_xcrys
    return [
        [-ε, i * a * h * bx, i * a * h * by, i * a * h * bz], # <--- ε ist ein Skalar, das --> i * a * h * bx <-- ist aber ein Vektor 
        [-i * a * h * bx, 0, -i * b * h * bz, i * b * h * by],
        [-i * a * h * by, i * b * h * bz, 0, -i * b * h * bx],
        [-i * a * h * bz, -i * b * h * by, i * b * h * bx, 0],
    ]  # (4,4)


def main():
    xlist = np.linspace(0, 10, 10)

    plt.plot(xlist, eigenwerte(xlist)[0], color="red")
    plt.show()


if __name__ == "__main__":
    main()
Antworten