import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import linregress
from scipy.optimize import curve_fit

oberwelle3, amplitude3 = np.genfromtxt('Rohdaten/dreieckspannung.txt',unpack=True)
plt.plot(oberwelle3, amplitude3,'k.',label="Messdaten")
#plt.legend(loc='best')
plt.grid()
#plt.xlim(0,1.5)
plt.xscale('log')
plt.yscale('log')
plt.xlabel(r'Oberwellen')
plt.ylabel(r'$\mathrm{U}/V$')
plt.tight_layout()
#plt.show()
#plt.savefig('build/50ohm.pdf')

####################
newX = np.logspace(-4, 1, base=10)  # Makes a nice domain for the fitted curves.

                                   # This avoids the sorting and the swarm of lines.

# Let's fit an exponential function.
# This looks like a line on a lof-log plot.
def myExpFunc(x, a, b):
    return a * np.power(x, b)
popt, pcov = curve_fit(myExpFunc, oberwelle3, amplitude3)
plt.plot(newX, myExpFunc(newX, *popt), 'r-',
         label="Fit".format(*popt))
plt.xlim(10**-2, 10**1)
plt.ylim(10**-0.5, 10**1)
print('Exponential Fit: y = (a*(x**b))')
print('\ta = popt[0] = {0}\n\tb = popt[1] = {1}'.format(*popt))
####################


plt.legend(loc='best')
#plt.show()
plt.savefig('build/dreieckspannung.pdf')
