#!/usr/bin/python3 # Plot einer nichtlinearen Ausgleichsrechnung (fit) from sys import exit import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt from time import strftime # Messwerte, Listen mit den x- und y-Koordinaten d = "BAT43" # Name der Diode (für Beschriftung) T = 25 # Temperatur in Grad Celsius Ut = 1.380649*(T+273.15)/(10.0*1.602177) x = [123.6, 167.5, 263.7, 288.7, 313.1] # mV y = [7.9, 33.1, 512.2, 983.0, 2134.1] # uA if len(x) != len(y): print("Fehler 1") ; exit(1) # Bildschirm-Ausgabe print("\nStart: Programm shockley,",strftime("\ %d. %B %Y, %H Uhr %M und %S s")) # Definition der Modellfunktion f def f(x,Is,n): return Is*(np.exp(x/(n*Ut))-1) # Fit der Modellfunktion an die Messwerte p,c = curve_fit(f,x,y) # Fit (ergibt Parameter) mx = np.linspace(100,400,num=50) # Modell-x my = (p[0]*(np.exp(mx/(p[1]*Ut))-1)) # Modell-y # Bildschirm-Ausgabe print("Diode: ",d) print("Temperatur T =",str(T),"\u00b0C,",\ "Temperaturspannung U_T =",round(Ut,3),"mV") print("Shockley-Gleichung:\n Saettigungs\ sperrstrom Is =",round(p[0],4),"\u03bcA","\n \ Idealitaetsfaktor n =",round(p[1],3)) # Plot von Messwerten und Modellfunktion, Labels plt.yscale('log') ; plt.grid(c='y',linewidth=0.2) plt.plot(x, y, 'o', color='g', markersize=8,\ label='n = '+str(len(x))+' Punkte') plt.plot(mx, my, '-', color='tab:brown',\ label="Shockley-Gleichung") # Labels für drei theoretische Kennlinienpunkte x10 = p[1]*Ut*np.log(10/p[0]+1) # U (I = 10 uA) u10 = "U("+str(round(x10,1)).replace(".",",")+\ " V) = 10 \u03bcA" plt.plot([],[],' ',label=u10) # Zeile in Legende x100 = p[1]*Ut*np.log(100/p[0]+1) # U (I = 100 uA) u100 = "U("+str(round(x100,1)).replace(".",",")+\ " V) = 100 \u03bcA" plt.plot([],[],' ',label=u100) # Zeile in Legende x1000 = p[1]*Ut*np.log(1000/p[0]+1) # U (I = 1 mA) u1000 = "U("+str(round(x1000,1)).replace(".",",")+\ " V) = 1 mA" plt.plot([],[],' ',label=u1000) # Zeile in Legende # Titelzeile, Achsenbeschriftungen und Legende l='$T$ = '+str(T)+' \u00b0C, $I$ = '+\ str(round(p[0],3)).replace(".",",")+\ ' \u03bcA \u00b7 (exp('+' $U$ / ('+\ str(round(p[1],2)).replace(".",",")+\ ' \u00b7 $U_T$) - 1))'+', $U_T$ = '+\ str(round(Ut,1)).replace(".",",")+' mV' fd={'fontsize':'medium','fontweight':'normal'} plt.title('Kennlinie einer Diode '+d+', '+\ strftime("%d. %B %Y, %H Uhr %M")+"\n"+l\ ,fontdict=fd) plt.xlabel('Spannung $U$ [mV]') plt.ylabel('Strom $I$ [\u03bcA]') plt.legend(loc='upper left') # Speicherung der Graphik in einer Bilddatei datei = strftime("fit_"+"%y%m%d%H%M%S"+".png") md = {'Title':'Diodenkennlinie','Author':\ 'AHG','Description':'Shockley-Fit, Diode: '+d} plt.savefig(fname=datei, dpi=100, metadata=md) # Bildschirm-Ausgabe print(" Stop: Programm shockley,",strftime("\ %d. %B %Y, %H Uhr %M und %S s")+"\n")