Advertisement
Pug_coder

Untitled

Jun 6th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. import numpy as np
  2.  
  3.  
  4. def system_equations(variables):
  5. x, y = variables
  6. equation1 = np.sin(y) + 2 * x - 2
  7. equation2 = np.cos(x - 1) + y - 0.7
  8. return np.array([equation1, equation2])
  9.  
  10.  
  11. def jacobian_matrix(variables):
  12. x, y = variables
  13. # Находим производные
  14. jacobian = np.array([[2, np.cos(y)], [-np.sin(x - 1), 1]])
  15. return jacobian
  16.  
  17.  
  18. def newton_method(initial_guess, tolerance=1e-3, max_iterations=100):
  19. variables = initial_guess
  20. for iteration in range(max_iterations):
  21. equations = system_equations(variables)
  22. jacobian = jacobian_matrix(variables)
  23.  
  24. delta = np.linalg.solve(jacobian, -equations)
  25. variables += delta
  26. # if err < eps
  27. if np.max(np.abs(delta)) < tolerance:
  28. return variables
  29. raise ValueError("The method did not converge")
  30.  
  31.  
  32. def main():
  33. # Solve the system of equations
  34. initial_guess = np.array([0, 0], dtype=float)
  35. solution = newton_method(initial_guess)
  36. print("Solution: x =", solution[0], "y =", solution[1])
  37.  
  38.  
  39. if __name__ == '__main__':
  40. main()
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement