I calculate the root search function in the dichotomy()
equation using the dichotomy method and then output the values using the print_dichotomy()
decorator. I believe that in the end I will see the columns root - the value of the function at the root - error - convergence. However, in fact, I get it so that only the root and the value of the function are expected to be output from the columns, and the other two columns are incorrect. In them, the error is represented by only one value (the remaining rows are NaN
), and the convergence - all its values were in one row.
I did not describe the essence of the dichotomy()
function, placing lists of pseudo-values for Pandas columns in it.
What is the mistake?
My code:
import numpy as np
import pandas as pd
def print_dichotomy(funcs):
def wrapper(a, b, eps_func, eps_arg):
*res, func, eps_list, prev_roots = list(funcs(a, b, eps_func, eps_arg))
func = list(map(lambda x: f'{f(x):.10f}', res))
result_table = pd.DataFrame()
result_table['Roots'] = pd.Series(res)
result_table['Function values'] = pd.Series(func)
result_table['Calculation error'] = pd.Series(eps_list)
result_table['Convergence'] = pd.Series(prev_roots)
print(result_table)
return wrapper
def f(x):
return 1.2 - np.log(x) - 4 * np.cos(2 * x)
@print_dichotomy
def dichotomy(a, b, eps_func, eps_arg):
# 1. creating a list variable to store the root values
prev_roots = [0.01, 0.01, 0.01, 0.01, 0.01, 0.01]
# 2. let's create a list for storing calculation errors
eps_list = [0.02, 0.02, 0.02, 0.02, 0.02, 0.02]
# 3. create a list to store the roots
roots = [0.03, 0.03, 0.03, 0.03, 0.03, 0.03]
for root in roots:
yield root
yield eps_list, prev_roots
# 5. we return the value of the error in calculating the root, convergence
yield eps_list, prev_roots
dichotomy(0.0001, 50, 0.000001, 0.000001)