```python
import sympy as sp
```


```python
def Newton_1D_step(x_prev, f_str, var_str):
    '''
    Given x_k, string representation of function, and string representation of variable
    Return the numerical value of x_{k+1}
    '''
    f = sp.sympify(f_str)
    var = sp.symbols(var_str)
    df = sp.diff(f, var)
    x_next = x_prev - f.subs(var, x_prev) / df.subs(var, x_prev)
    return float(x_next)
```


```python
# Test Case: Newton_1D_step generic test
err_bound = 10**-7
expected = 0.113966093691
actual = Newton_1D_step(-2, 'sin(x/5)', 'x')
assert abs(expected - actual) < err_bound
```


```python
def Newton_1D_step_aux(x_prev, f, df, var):
    '''
    Given x_k, function, differentiation of the function, and the variable
    Return the numerical value of x_{k+1}
    Operate as a lighter weighted version of Newton_1D_step
    '''
    return float(x_prev - f.subs(var, x_prev) / df.subs(var, x_prev))

def Newton_1D(x_0, f_str, var_str, err_bound):
    '''
    Given initial value x_0, string representation of function, string representation of variable
    as well as the error bound
    Return the number of iterations it takes to get to the error bound
    And the final value
    '''
    f = sp.sympify(f_str)
    var = sp.symbols(var_str)
    df = sp.diff(f, var)
    x_curr = x_0
    iterations = 0
    while abs(float(f.subs(var, x_curr))) > err_bound:
        x_curr = Newton_1D_step_aux(x_curr, f, df, var)
        iterations += 1
    return iterations, x_curr
```


```python
# Test Case: Newton_1D generic iteration test
err_bound = 10**-7
it, res = Newton_1D(4.4, 'x*E**x', 'x', err_bound)
assert it == 10
```


```python
def secant_1D_step(x_prev0, x_prev1, f_str, var_str):
    '''
    Given x_{k-1}, x_{k}, string representation of function, and string representation of variable
    Return the numerical value of x_{k+1}
    '''
    f = sp.sympify(f_str)
    var = sp.symbols(var_str)
    x_next = x_prev1 - f.subs(var, x_prev1) / ((f.subs(var, x_prev0) - f.subs(var, x_prev1)) / (x_prev0 - x_prev1))
    return float(x_next)
```


```python
# Test Case: secant_1D_step generic test
err_bound = 10**-7
expected = -0.0134679226308
actual = secant_1D_step(1, -2, 'sin(x/5)', 'x')
assert abs(expected - actual) < err_bound
```


```python
def bisection_1D_step(left, right, f_str, var_str):
    f = sp.sympify(f_str)
    var = sp.symbols(var_str)
    mid = (left + right) / 2
    if float(f.subs(var, left) * f.subs(var, mid)) < 0:
        right = mid
    else:
        left = mid
    return left, right
```


```python
expected = (-1.0, 0.5)
temp = bisection_1D_step(-4, 2, 'sin(x/5)', 'x')
actual = bisection_1D_step(temp[0], temp[1], 'sin(x/5)', 'x')
assert expected == actual
```


```python
# T14-HW14.5 variation 1
f_str = 'sin(x/5)'
var_str = 'x'
newton_res = Newton_1D_step(2, f_str, var_str)
print(newton_res)
secant_res = secant_1D_step(1, 2, f_str, var_str)
print(secant_res)
temp = bisection_1D_step(-1, 2, f_str, var_str)
bisection_res = bisection_1D_step(temp[0], temp[1], f_str, var_str)
print(bisection_res)
```

    -0.1139660936908088
    -0.0415222035418395
    (-0.25, 0.5)



```python
# T14-HW14.5 variation 2
f_str = 'sin(x/3)'
var_str = 'x'
newton_res = Newton_1D_step(1, f_str, var_str)
print(newton_res)
secant_res = secant_1D_step(2, 1, f_str, var_str)
print(secant_res)
temp = bisection_1D_step(-1, 4, f_str, var_str)
bisection_res = bisection_1D_step(temp[0], temp[1], f_str, var_str)
print(bisection_res)
```

    -0.03876064853172647
    -0.12370422383815974
    (-1, 0.25)



```python
# T14-HW14.3 variation 1
Newton_1D(4, 'x*E**x', 'x', 10**-5)
```




    (9, 1.5586599258811135e-06)



f6408aba84256f7e5a3a45ab45c098134a7b9621
ipynb
Jupyter Notebook
newton_solve_1d.ipynb
Racso-3141/uiuc-cs357-fa21-scripts
e44f0a1ea4eb657cb77253f1db464d52961bbe5e
MIT
2021-11-02T05:56:10.000Z
2022-03-03T19:25:19.000Z
newton_solve_1d.ipynb
Racso-3141/uiuc-cs357-fa21-scripts
e44f0a1ea4eb657cb77253f1db464d52961bbe5e
MIT
newton_solve_1d.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