Haevelin |
In Python ein Tupel verrechnen
Hallo, folgender Code funktioniert nicht:
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
|
# -*- coding: utf-8 -*- """ Created on Mon Jun 22 09:23:15 2015 @author: rk480158 """
from sympy import *
import numpy as np
import math from scipy
import optimize # aus dem SciPy-Paket wird nur das optimize-Modul eingebunden
import matplotlib.pyplot as plt # einbinden der Plot-Funktionalitaet von Matplotlib zur graphischen Funktionsdarstellung
from numpy import * # from numarray import argmax, abs, dot, zeros, Float64, array # import swap # import error
import numpy as np import numpy.linalg as LA from pylab
import * from scipy.integrate
import quad
def funktion(x1, x2):
f=np.ones((2,1))
f[0,0]=sin(x1)-x2
f[1,0]=math.e**(-x2) - x1
return f
def funktion_der(x1,x2):
f_der=np.ones((2,2))
f_der[0,0]=cos(x1)
f_der[1,0]= -1.0
f_der[0,1]= -1.0
f_der[1,1]= -math.e**(-x2)
return np.linalg.pinv(f_der)
# print funktion(5.0,2.0)
# print funktion_der(5.0,2.0)
x_k=np.ones((2,1));
x_k[0,0]=-2.0
x_k[1,0]=-2.0
for i in range(10):
x_k=x_k - np.dot(funktion_der(x_k[0,0], x_k[1,0]),funktion(x_k[0,0], x_k[1,0])) print x_k
|
|
Während folgendes nicht funktioniert:
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
|
# -*- coding: utf-8 -*- """ Created on Mon Jun 22 09:23:15 2015 @author: rk480158 """
from sympy import *
import numpy as np
import math from scipy
import optimize # aus dem SciPy-Paket wird nur das optimize-Modul eingebunden
import matplotlib.pyplot as plt # einbinden der Plot-Funktionalitaet von Matplotlib zur graphischen Funktionsdarstellung
from numpy import * # from numarray import argmax, abs, dot, zeros, Float64, array # import swap # import error
import numpy as np import numpy.linalg as LA from pylab
import * from scipy.integrate
import quad
def funktion(x1, x2):
f=np.ones((2,1))
f[0,0]=sin(x1)-x2
f[1,0]=math.e**(-x2) - x1
return f
def funktion_der(x1,x2):
f_der=np.ones((2,2))
f_der[0,0]=cos(x1)
f_der[1,0]= -1.0
f_der[0,1]= -1.0
f_der[1,1]= -math.e**(-x2)
return np.linalg.pinv(f_der)
# print funktion(5.0,2.0)
# print funktion_der(5.0,2.0)
x_k=np.ones((2,1));
x_k[0,0]=-2.0
x_k[1,0]=-2.0
for i in range(10):
x_k[0:2,0]=x_k[0:2,0] - np.dot(funktion_der(x_k[0,0], x_k[1,0]),funktion(x_k[0,0], x_k[1,0])) print x_k
|
|
Wie weißt man einem Array einen Wert für jede Komponente zu? |
eulerscheZahl |
Wie wär's damit?
code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
|
import math
import numpy as np
import matplotlib.pyplot as plt # einbinden der Plot-Funktionalitaet von Matplotlib zur graphischen Funktionsdarstellung
import numpy.linalg as LA
def funktion(x1, x2):
f=np.ones((2,1))
f[0,0]=math.sin(x1)-x2
f[1,0]=math.e**(-x2) - x1
return f
def funktion_der(x1,x2):
f_der=np.ones((2,2))
f_der[0,0]=math.cos(x1)
f_der[1,0]= -1.0
f_der[0,1]= -1.0
f_der[1,1]= -math.e**(-x2)
return np.linalg.pinv(f_der)
# print funktion(5.0,2.0)
# print funktion_der(5.0,2.0)
x_k=np.ones((2,1));
x_k[0,0]=-2.0
x_k[1,0]=-2.0
for i in range(10):
x_k[0:2,0:2]=x_k[0:2,0:2] - np.dot(funktion_der(x_k[0,0], x_k[1,0]),funktion(x_k[0,0], x_k[1,0]))
print x_k |
|
|
Haevelin |
Ich verstehe nicht; x_k wurde als 2*1 Matrix festgelegt, während bei deiner Verwendung eine 2*2 Matrix entsteht.
Vor allen Dingen weiß ich nicht warum mein Code funktioniert, wo nur x_k ohne range steht. |
eulerscheZahl |
funktion_der liefert dir eine 2x2 Matrix. Da wird das andere dann implizit auch der 2x2 Matrix. Das geht aber nicht, wenn du explizit sagst, dass es das eben nicht ist. |