Fehler beim Lösen einer Matrixgleichung mit numpy
Ich schreibe meinen eigenen Newton-Raphson-Algorithmus in Python mitsympy
undnumpy
.
Der Code ist unten, aber Sie können ihn ignorieren und mit dem Fehler fortfahren:
COD
def newtonRhapson(fncList, varz, x0):
jacob = []
for fnc in fncList:
vec = []
for var in varz:
res = fnc.diff(var)
for i in range(len(varz)):
res = res.subs(varz[i], x0[i])
vec.append(res)
jacob.append(numpy.array(vec, dtype='float64'))
fx0=[]
for fnc in fncList:
res2 = fnc
for i in range(len(varz)):
res2 = res2.subs(varz[i], x0[i])
fx0.append(res2)
j = jacob
f = fx0
print j
print ''
print f
print numpy.linalg.solve(j,f).tolist()
Die Argumente für die Funktion sind:
fncList
- eine Python-Liste von Funktionen mitSympy
Symbole
varz
- eine Liste mit diesen Symbolen (Variablen)
x0
- anfängliche Vermutung
ERRO
is zu dem Punkt, an dem wprint
j
undf
es funktioniert einwandfrei und gibt Folgendes aus:
[array([-9.13378682, -5.91269838]), array([ 4.84401379, 1.01980286])]
[-5.15598620617611, 5.13378681611922]
wenn ich laufe:
newtonRhapson([5*cos(a)+6*cos(a+b)-10, 5*sin(a)+6*sin(a+b)-4], [a,b], [0.7,0.7])
Aber beim Laufen der Linie:
print numpy.linalg.solve(j,f).tolist()
Ich erhalte den Fehler:
File "/Users/me/anaconda/lib/python2.7/site- packages/numpy/linalg/linalg.py", line 384, in solve
r = gufunc(a, b, signature=signature, extobj=extobj)
TypeError: No loop matching the specified signature and casting was found for ufunc solve1