Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. # Aufgabe 7
  2.  
  3. def graddesc( A, x0, step=0.1 ):
  4.     xi = x0
  5.    
  6.     # create the result matrix and save the initial value
  7.     results = np.empty([50, 2])
  8.     results[0, 0] = xi[0]
  9.     results[0, 1] = xi[1]
  10.    
  11.     # complete the matrix with the given formula
  12.     for i in range(1, 50):
  13.         xi = xi + step * (b - np.matmul(A, xi))
  14.         results[i, 0] = xi[0]
  15.         results[i, 1] = xi[1]
  16.        
  17.     return results
  18.  
  19. # generate the values with the same intial value as in the lecture
  20. values = graddesc(A, np.array([-4, -4]))
  21.  
  22. plt.plot(values[:, 0], values[:, 1])
  23. plot_f( plt.gca() )
  24.  
  25. # Aufgabe 8, keine Ahnung ob das richtig ist, sieht aber so aus
  26.  
  27. xvalues = np.arange(1, len(history) + 1)  # ascending array from 1 to 307
  28. yvalues = np.empty(len(history))
  29. for i in range(len(history)):
  30.     yvalues[i] = np.linalg.norm(A@history[i] - f)  # add the residual norm in this iteration
  31. plt.semilogy(xvalues, yvalues)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement