0
0

More than 5 years have passed since last update.

2変数のニュートン法の実装

Posted at

うまくいかなかったコード。
自分で見直すように残しておく。

class MultiNewton(object):
... def init(self,f,dx0f,dx1f,grad_f,hesse):
... self.f=f
... self.dx0f=dx0f
... self.dx1f=dx1f
... self.grad_f=grad_f
... self.hesse=hesse
... def _compute_dx(self,bar_x):
... grad=self.grad_f(bar_x)
... hesse=self.hesse(bar_x)
... dx=np.linalg.solve(hesse,-grad)
... return dx
... def solve(self,init_x,n_iter,tol,step_width):
... bar_x=init_x
... for i in range(n_iter):
... dx=self._compute_dx(bar_x)
... x=bar_x+dx*step_width
... bar_x=x
... norm_dx=np.linalg.norm(dx)
... if norm_dx ... break
... return x
...
multinewton=MultiNewton(f=f,dx0f=dx0f,dx1f=dx1f,grad_f=grad_f,hesse=hesse)
multinewton.solve(init_x=np.array([10.0,8.0]),n_iter=100,tol=0.01,step_width=1.0)
Traceback (most recent call last):
File "", line 1, in
File "", line 16, in solve
File "", line 11, in _compute_dx
TypeError: bad operand type for unary -: 'function'

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0