```python
import numpy as np
import numpy.linalg as la
import sympy as sp
```


```python
def subs_all(formula, variables, values):
    '''
    You know what, it's getting to the point where this function
    is necessary
    '''
    result = formula
    for i in range(len(values)):
        result = result.subs(variables[i], values[i])
    return float(result.evalf())
```


```python
# Generic Test Case 1 for subs_all
formula = sp.sympify('5*x**2+6*y**2+4*(z/2)**2')
variables = sp.symbols('x y z')
values = [1, 1, 1]
expected = 12
actual = subs_all(formula, variables, values)
assert expected==actual
```


```python
def calculated_jacobian(fs, variables, x_k):
    '''
    fs need to EQUAL TO ZERO!
    '''
    size = len(fs)
    result = np.zeros((size, size))
    for i in range(size):
        for j in range(size):
            result[i][j] = subs_all(sp.diff(fs[i], variables[j]), variables, x_k)
    return result    
def newton_nd_step(x_prev, f_strs, var_str):
    '''
    Given x_k, string of functions equal to 0, and string for
    variables, calculate the parameter for the next iteration
    of Newton's method for solving ND non-linear system of equations
    '''
    fs = [sp.sympify(f_str) for f_str in f_strs]
    variables = sp.symbols(var_str)
    jac = calculated_jacobian(fs, variables, x_prev)
    f_vals = np.array([subs_all(f, variables, x_prev) for f in fs])
    s = la.solve(jac, -1*f_vals)
    return jac, x_prev+s
```


```python
# Generic Test Case 1 for newton_nd_step
expected = np.array([-3/2, -1/6])
jac, actual = newton_nd_step([-2, 0], ['3*x*y-1', 'x**3+y**2+2'], 'x y')
tol = 10**-7
assert la.norm(expected - actual, 2) < tol
```


```python
# Generic Test Case 2 for newton_nd_step
expected_jac = np.array([[8, -10], [12, 3]])
expected = np.array([1.25694444, -0.69444444])
jac, actual = newton_nd_step([1, -1], ['2*x**4+5*y**2-6', '4*x**3+3*y-5'], 'x y')
tol = 10**-7
assert la.norm(expected_jac - jac) < tol
assert la.norm(expected - actual) < tol
```


```python
# Workspace
f_strs = ['4*x*y-3', 'x**3+y**2+2']
x_prev = [1, 0]
var_str = 'x y'
newton_nd_step(x_prev, f_strs, var_str)
```




    (array([[0., 4.],
            [3., 0.]]),
     array([0.  , 0.75]))




```python

```

8ec641b39d0fad4d2a45c4ab34b825d31b9dfdcf
ipynb
Jupyter Notebook
newton_solve_nd.ipynb
Racso-3141/uiuc-cs357-fa21-scripts
e44f0a1ea4eb657cb77253f1db464d52961bbe5e
MIT
2021-11-02T05:56:10.000Z
2022-03-03T19:25:19.000Z
newton_solve_nd.ipynb
Racso-3141/uiuc-cs357-fa21-scripts
e44f0a1ea4eb657cb77253f1db464d52961bbe5e
MIT
newton_solve_nd.ipynb
Racso-3141/uiuc-cs357-fa21-scripts
e44f0a1ea4eb657cb77253f1db464d52961bbe5e
MIT
2021-10-30T15:18:01.000Z
2021-12-10T11:26:43.000Z
Qwen/Qwen-72B
1. YES
2. YES