import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve, root
F = lambda x: [x[0]**2 + x[1]**2 - 4, x[0]**2 - x[1] - 1]
x1 = np.linspace(-2.5, 2.5, 400)
y1_pos = np.sqrt(4 - x1**2)
y1_neg = -np.sqrt(4 - x1**2)
y2 = x1**2 - 1
plt.figure()
plt.plot(x1, y1_pos, 'b-', x1, y1_neg, 'b-', x1, y2, 'r-', linewidth=2)
plt.grid(True)
plt.xlabel('x1')
plt.ylabel('x2')
plt.legend(['Окружность +', 'Окружность -', 'Парабола'])
x0 = [1.5, 1.5]
sol1 = fsolve(F, x0, full_output=True)
x1_sol, info1 = sol1[0], sol1[2]
print(f'Решатель 1 (fsolve): x1 = {x1_sol[0]:.6f}, x2 = {x1_sol[1]:.6f}, итераций: {info1["nfev"]}')
sol2 = root(F, x0, method='lm')
x2_sol = sol2.x
print(f'Решатель 2 (LM): x1 = {x2_sol[0]:.6f}, x2 = {x2_sol[1]:.6f}, итераций: {sol2.nfev}')
plt.plot(x1_sol[0], x1_sol[1], 'ko', markersize=10, markerfacecolor='green')
plt.legend(['Окружность +', 'Окружность -', 'Парабола', 'Корень'])
plt.show()