# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import scipy as sp
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import sympy as sy
from sympy.solvers import solve
import pandas as pd
from numpy import exp, loadtxt, pi, sqrt
import lmfit as lm
from lmfit import Model
plt.rc('text', usetex=True)
plt.rc('font', family='serif')

df1=pd.DataFrame.from_csv('A2_Schuster.csv',sep=';',encoding='utf-8')
#df1 #Zeigt dir die Tabelle an
df2=df1.sort_values('r in cm')
r=df2['r in m']= df2['r in cm']*1e-2
B=df2['B in T']=df2['B in mT']*1e-3
U=df2['U in V']
df2


def func(x,a,b):
    return a*x+b


# +
u=0;v=9
x=r[u:v]**2*B[u:v]**2;y=2*U[u:v]
gmodel = Model(func)
result = gmodel.fit(y, x=x, a=1.7e11, b=10)
pcov= result.covar
perr = np.sqrt(np.diag(pcov))
#print(result.fit_report())
print(result.best_values)
popt=result.best_values
print(perr)
plt.plot(x, y,'o',color='C0',label=r'Messdaten')
plt.errorbar(x[u:v], y[u:v], yerr=perr[1], fmt='.',color='r',capsize=3, label='Fehler',alpha=0.4)
plt.plot(x, result.best_fit,'-',color='C1',label=r'Fit: y = ax + b')

s2=r'''\begin{tabular}{ c | c | c } %s & Wert & Fehler \\\hline %s & %4.3g & %3.3g \\\hline %s &%3.3g & %3.3g \end{tabular}'''%('','a',popt['a'],perr[0],'b',popt['b'],perr[1])      
plt.text(2.8*1e-9,350,s2,ha="left",va="center",bbox=dict(boxstyle="square",ec='gold',fc='whitesmoke',))
plt.xlabel(r'$r^{2}B^{2}$ in $m^2 T^2$',fontsize=16)
plt.ylabel(r'$2U$ in $V$',fontsize=16)
    
plt.title(r'Schuster mit r=2cm',fontsize=18)
plt.legend(loc='upper left')
#plt.savefig("Schuster_2cm.pdf", bbox_inches='tight', format='pdf')
plt.show()
# -

u=0;v=9;g=2
while v<= len(r):
    
    x=r[u:v]**2*B[u:v]**2;y=2*U[u:v]
    gmodel = Model(func)
    result = gmodel.fit(y, x=x, a=1.7e11, b=10)
    pcov= result.covar
    perr = np.sqrt(np.diag(pcov))
    #print(result.fit_report())
    print(result.best_values)
    popt=result.best_values
    print('Fehler [a,b]:',perr)
    plt.plot(x, y,'o',color='C0',label=r'Messdaten')
    plt.errorbar(x, y, yerr=perr[1], fmt='.',color='r',capsize=3,alpha=0.5)
    plt.plot(x, result.best_fit,'-',color='C1',label=r'Fit: y = ax + b')

    ax=plt.axes()
    s2=r'''\begin{tabular}{ c | c | c } %s & Wert & Fehler \\\hline %s & %4.3g & %3.3g \\\hline %s &%3.3g & %3.3g \end{tabular}'''%('','a',popt['a'],perr[0],'b',popt['b'],perr[1])      
    plt.text(0.75, 0.15,s2,horizontalalignment='center',verticalalignment='center',
             transform = ax.transAxes, bbox=dict(boxstyle="square",ec='gold',fc='whitesmoke'))
    plt.xlabel(r'$r^{2}B^{2}$ in $m^2 T^2$',fontsize=16)
    plt.ylabel(r'$2U$ in $V$',fontsize=16)
    plt.title(r'Schuster mit r='+str(g)+'cm',fontsize=18)
    plt.legend(loc='upper left')
    #plt.savefig("Schuster_"+g+"cm.pdf", bbox_inches='tight', format='pdf')
    plt.show()
    
    u+=9;v+=9;g+=1

# +
x=r**2*B**2;y=2*U
gmodel = Model(func)
result = gmodel.fit(y, x=x, a=1.7e11, b=10)
pcov= result.covar
perr = np.sqrt(np.diag(pcov))
print(result.fit_report())
print(result.best_values)
popt=result.best_values
print('Fehler [a,b]:',perr)
plt.plot(x, y,'o',color='C0',label=r'Messdaten')
#plt.errorbar(x, y, yerr=perr[1], fmt='.',color='r',capsize=3, label='Fehler',alpha=0.4)
plt.plot(x, result.best_fit,'-',color='C1',label=r'Fit: y = ax + b')

ax=plt.axes()
    
s2=r'''\begin{tabular}{ c | c | c } %s & Wert & Fehler \\\hline %s & %4.3g & %3.3g \\\hline %s &%3.3g & %3.3g \end{tabular}'''%('','a',popt['a'],perr[0],'b',popt['b'],perr[1])      
plt.text(0.75, 0.15,s2,horizontalalignment='center',verticalalignment='center',
             transform = ax.transAxes, bbox=dict(boxstyle="square",ec='gold',fc='whitesmoke'))
plt.xlabel(r'$r^{2}B^{2}$ in $m^2 T^2$',fontsize=16)
plt.ylabel(r'$2U$ in $V$',fontsize=16)
    
plt.title(r'Schuster',fontsize=18)
plt.legend(loc='upper left')
#plt.savefig("Schuster_ges.pdf", bbox_inches='tight', format='pdf')
plt.show()
# -





# ## Methode nach Busch

df3=pd.DataFrame.from_csv('A2_Busch.csv',sep=';',encoding='utf-8')
df3 #Zeigt dir die Tabelle an
df4=df3.sort_values(['n','U in V'])
n=df4['n']
B=df4['B in T']=df4['B in mT']*1e-3
U=df4['U in V']
df4

# +
u=0;v=7
x=(0.13**2)*B[u:v]**2; y=8*(np.pi**2)*(n[u:v]**2)*U[u:v]
gmodel = Model(func,nan_policy='omit')
result = gmodel.fit(y, x=x, a=1.7e11, b=10)
pcov= result.covar
perr = np.sqrt(np.diag(pcov))
#print(result.fit_report())
print(result.best_values)
popt=result.best_values
print('Fehler [a,b]:',perr)
plt.plot(x, y,'o',color='C0',label=r'Messdaten')
plt.errorbar(x, y, yerr=perr[1], fmt='.',color='r',capsize=3, label='Fehler',alpha=0.4)
plt.plot(x, result.best_fit,'-',color='C1',label=r'Fit: y = ax + b')

ax=plt.axes()
s2=r'''\begin{tabular}{ c | c | c } %s & Wert & Fehler \\\hline %s & %4.3g & %3.3g \\\hline %s &%3.3g & %3.3g \end{tabular}'''%('','a',popt['a'],perr[0],'b',popt['b'],perr[1])      
plt.text(0.75, 0.15,s2,horizontalalignment='center',verticalalignment='center',
             transform = ax.transAxes, bbox=dict(boxstyle="square",ec='gold',fc='whitesmoke'))
plt.xlabel(r'$s^{2}B^{2}$ in $m^2 T^2$',fontsize=16)
plt.ylabel(r'$8 \pi^{2} n^{2} U$ in $V$',fontsize=16)
plt.title(r'Busch mit n='+str(1),fontsize=18)
plt.legend(loc='upper left')
#plt.savefig("Schuster_"+g+"cm.pdf", bbox_inches='tight', format='pdf')
plt.show()

u=7;v=14
x=(0.13**2)*B[u:v]**2; y=8*(np.pi**2)*(n[u:v]**2)*U[u:v]
gmodel = Model(func,nan_policy='omit')
result = gmodel.fit(y, x=x, a=1.7e11, b=10)
pcov= result.covar
perr = np.sqrt(np.diag(pcov))
#print(result.fit_report())
print(result.best_values)
popt=result.best_values
print('Fehler [a,b]:',perr)
plt.plot(x, y,'o',color='C0',label=r'Messdaten')
plt.errorbar(x, y, yerr=perr[1], fmt='.',color='r',capsize=3, label='Fehler',alpha=0.4)
plt.plot(x, result.best_fit,'-',color='C1',label=r'Fit: y = ax + b')

ax=plt.axes()
s2=r'''\begin{tabular}{ c | c | c } %s & Wert & Fehler \\\hline %s & %4.3g & %3.3g \\\hline %s &%3.3g & %3.3g \end{tabular}'''%('','a',popt['a'],perr[0],'b',popt['b'],perr[1])      
plt.text(0.75, 0.15,s2,horizontalalignment='center',verticalalignment='center',
             transform = ax.transAxes, bbox=dict(boxstyle="square",ec='gold',fc='whitesmoke'))
plt.xlabel(r'$s^{2}B^{2}$ in $m^2 T^2$',fontsize=16)
plt.ylabel(r'$8 \pi^{2} n^{2} U$ in $V$',fontsize=16)
plt.title(r'Busch mit n='+str(2),fontsize=18)
plt.legend(loc='upper left')
#plt.savefig("Schuster_"+g+"cm.pdf", bbox_inches='tight', format='pdf')
plt.show()

u=14;v=20
x=(0.13**2)*B[u:v]**2; y=8*(np.pi**2)*(n[u:v]**2)*U[u:v]
gmodel = Model(func,nan_policy='omit')
result = gmodel.fit(y, x=x, a=1.7e11, b=10)
pcov= result.covar
perr = np.sqrt(np.diag(pcov))
#print(result.fit_report())
print(result.best_values)
popt=result.best_values
print('Fehler [a,b]:',perr)
plt.plot(x, y,'o',color='C0',label=r'Messdaten')
plt.errorbar(x, y, yerr=perr[1], fmt='.',color='r',capsize=3, label='Fehler',alpha=0.4)
plt.plot(x, result.best_fit,'-',color='C1',label=r'Fit: y = ax + b')

ax=plt.axes()
s2=r'''\begin{tabular}{ c | c | c } %s & Wert & Fehler \\\hline %s & %4.3g & %3.3g \\\hline %s &%3.3g & %3.3g \end{tabular}'''%('','a',popt['a'],perr[0],'b',popt['b'],perr[1])      
plt.text(0.75, 0.15,s2,horizontalalignment='center',verticalalignment='center',
             transform = ax.transAxes, bbox=dict(boxstyle="square",ec='gold',fc='whitesmoke'))
plt.xlabel(r'$s^{2}B^{2}$ in $m^2 T^2$',fontsize=16)
plt.ylabel(r'$8 \pi^{2} n^{2} U$ in $V$',fontsize=16)
plt.title(r'Busch mit n='+str(3),fontsize=18)
plt.legend(loc='upper left')
#plt.savefig("Schuster_"+g+"cm.pdf", bbox_inches='tight', format='pdf')
plt.show()

# +
x=(0.13**2)*B**2; y=8*(np.pi**2)*(n**2)*U
gmodel = Model(func,nan_policy='propagate')
result = gmodel.fit(y, x=x, a=1.7e11, b=10)
pcov= result.covar
perr = np.sqrt(np.diag(pcov))
#print(result.fit_report())
print(result.best_values)
popt=result.best_values
print('Fehler [a,b]:',perr)
plt.plot(x, y,'o',color='C0',label=r'Messdaten')
plt.errorbar(x, y, yerr=perr[1], fmt='.',color='r',capsize=3, label='Fehler',alpha=0.4)
plt.plot(x, result.best_fit,'-',color='C1',label=r'Fit: y = ax + b')

ax=plt.axes()
s2=r'''\begin{tabular}{ c | c | c } %s & Wert & Fehler \\\hline %s & %4.3g & %3.3g \\\hline %s &%3.3g & %3.3g \end{tabular}'''%('','a',popt['a'],perr[0],'b',popt['b'],perr[1])      
plt.text(0.75, 0.15,s2,horizontalalignment='center',verticalalignment='center',
             transform = ax.transAxes, bbox=dict(boxstyle="square",ec='gold',fc='whitesmoke'))
plt.xlabel(r'$s^{2}B^{2}$ in $m^2 T^2$',fontsize=16)
plt.ylabel(r'$8 \pi^{2} n^{2} U$ in $V$',fontsize=16)
plt.title(r'Busch mit n='+str(g),fontsize=18)
plt.legend(loc='upper left')
#plt.savefig("Schuster_"+g+"cm.pdf", bbox_inches='tight', format='pdf')
plt.show()
# -





StarcoderdataJupyterScriptsDedupFiltered