1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 3 11:53:24 2025
@author: christian
"""
from scipy.optimize import fsolve, root, minimize
from sympy import symbols, Eq, nsolve
import numpy as np
### fsolve ###
def equations(vars):
x, y = vars
eq1 = x**2 + y**2 - 25
eq2 = x**2 - y
return [eq1, eq2]
initial_guess = [1, 1]
solution_fsolve= fsolve(equations, initial_guess)
### root ###
def equations(vars):
x, y = vars
eq1 = x**2 + y**2 - 25
eq2 = x**2 - y
return [eq1, eq2]
initial_guess = [1, 1]
solution_root = root(equations, initial_guess)
### minimize ###
# Define the equations
def equation1(x, y):
return x**2 + y**2 - 25
def equation2(x, y):
return x**2 - y
# Define the objective function for optimization
def objective(xy):
x, y = xy
return equation1(x, y)**2 + equation2(x, y)**2
# Initial guess
initial_guess = [1, 1]
# Perform optimization
result = minimize(objective, initial_guess)
solution_optimization = result.x
### nsolve ###
# Define the variables
x, y = symbols('x y')
# Define the equations
eq1 = Eq(x**2 + y**2, 25)
eq2 = Eq(x - y, 0)
# Initial guess for the solution
initial_guess = [1, 1]
# Use nsolve to find the solution
solution_nsolve = nsolve([eq1, eq2], [x, y], initial_guess)
### newton_method ###
def equations(vars):
x, y = vars
eq1 = x**2 + y**2 - 25
eq2 = x**2 - y
return np.array([eq1, eq2])
def newton_method(initial_guess, tolerance=1e-6, max_iter=100):
vars = np.array(initial_guess, dtype=float)
for _ in range(max_iter):
J = np.array([[2 * vars[0], 2 * vars[1]], [2 * vars[0], -1]])
F = equations(vars)
delta = np.linalg.solve(J, -F)
vars += delta
if np.linalg.norm(delta) < tolerance:
return vars
initial_guess = [1, 1]
solution_newton = newton_method(initial_guess)
|