Advertisement
Guest User

new

a guest
Apr 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. # Hello World program in Python
  2.    
  3. from __future__ import division
  4. import numpy as np
  5. from numpy import linalg as LA
  6.  
  7. def steep(A, b, x, tol=1e-6, maxit=1e5):
  8.    
  9.     r=b-np.dot(A,x)
  10.     r_norm=LA.norm(r)
  11.     it=0
  12.     while r_norm>=tol and it<maxit:
  13.         it=it+1
  14.         a=np.dot(r,r)/(np.dot(r, np.dot(A,r)))
  15.         x=x+a*r
  16.         r=b-np.dot(A,x)
  17.         r_norm=LA.norm(r)
  18.         if it==maxit and r_norm>=tol:
  19.             print("Method failed.")
  20.     return x
  21.    
  22.     A=np.array([[1e-2,0,0],[0,1,0],[0,0,1e2]])
  23.     b=np.array([1,1,1])
  24.     x=np.array([0,0,0])
  25.     theta=steep(A,b,x,tol=1e-6,maxit=1e5)
  26.     print(theta)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement