scipy Umsetzung einer math. Formel in Code

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
littleg
User
Beiträge: 1
Registriert: Mittwoch 26. Februar 2014, 15:10

Hallo,

ich versuche einige mathematische Formeln in Python Code umzusetzen.
Leider bekomme ich eine Reihe Fehlermeldungen ...

Code: Alles auswählen

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
from scipy.integrate import quad
n = 3
x = np.array([2.1,2.2,2.3,2.4])
y = np.array([0.9,2.1,3.2,17.9])
def func(x, a, b):
    b1 = n*quad(x[n]*np.log(y[n]), 1, n) - quad(x[n], 1, n)*quad(np.log(y[n]), 1, n)
    b2 = n*quad(x[n]**2, 1, n) - (quad(x[n], 1, n))**2
    b = b1 / b2
    a = np.exp(1/n+(quad(np.log(y[n]), 1, n) - b(quad(x[n], 1, n))))
    return a*np.exp(b*x)
popt, pcov = curve_fit(func, x, y)
print popt
plt.plot(x, y)
plt.grid(True)
plt.show()
Jemand eine Idee/Ansatz wo mein Fehler liegt?

Fehlermeldungen:
Traceback (most recent call last):
File "F:\py-IAT\Laktat.py", line 20, in <module>
popt, pcov = curve_fit(func, x, y)
File "C:\Python27\lib\site-packages\scipy\optimize\minpack.py", line 506, in curve_fit
res = leastsq(func, p0, args=args, full_output=1, **kw)
File "C:\Python27\lib\site-packages\scipy\optimize\minpack.py", line 348, in leastsq
m = _check_func('leastsq', 'func', func, x0, args, n)[0]
File "C:\Python27\lib\site-packages\scipy\optimize\minpack.py", line 14, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
File "C:\Python27\lib\site-packages\scipy\optimize\minpack.py", line 418, in _general_function
return function(xdata, *params) - ydata
File "F:\py-IAT\Laktat.py", line 15, in func
b1 = n*quad(x[n]*np.log(y[n]), 1, n) - quad(x[n], 1, n)*quad(np.log(y[n]), 1, n)
File "C:\Python27\lib\site-packages\scipy\integrate\quadpack.py", line 247, in quad
retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
File "C:\Python27\lib\site-packages\scipy\integrate\quadpack.py", line 312, in _quad
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
quadpack.error: quad: first argument is not callable

Danke für jegliche Hinweise!
Zuletzt geändert von littleg am Mittwoch 26. Februar 2014, 15:38, insgesamt 1-mal geändert.
Benutzeravatar
pillmuncher
User
Beiträge: 1484
Registriert: Samstag 21. März 2009, 22:59
Wohnort: Pfaffenwinkel

littleg hat geschrieben:Leider bekomme ich eine Reihe Fehlermeldungen ...
Welche?
In specifications, Murphy's Law supersedes Ohm's.
BlackJack

@littleg: Die Fehlermeldung ist doch recht eindeutig: Das erste Argument das Du `quad()` übergibst ist nicht aufrufbar. Und wenn man in die Dokumentation von `quad()` schaut, sieht man dass das erste Argument eine Funktion sein muss. Du hast als erstes Argument aber den Ausdruck ``x[n]*np.log(y[n])``, der halt als Ergebnis keine Funktion hat, sondern wahrscheinlich eine Zahl oder ein Array, abhängig von den Elementen von `x` und `y`.
Antworten