def solve(self, diff: DifferentialEq, precision=4) -> Tuple[Function]: ''' Returns tuple of (method-derived function, LTE function, GTE function) ''' # getting result function and lte result, lte, _ = self.__execute_method( diff.f, diff.solution, diff.x_0, diff.y_0, diff.x_n, diff.n, precision=precision, ) # calculate gte for all n from `n_start` to `n_end` gte = Function() for n in range(diff.n_start, diff.n_end + 1): _, _, gte_values = self.__execute_method( diff.f, diff.solution, diff.x_0, diff.y_0, diff.x_n, n, precision=precision, ) gte.append(n, max(gte_values.Y)) return result, lte, gte
def __execute_method(self, f, y_solution_func, x_0, y_0, x_n, n, precision=4) -> Tuple[Function]: ''' Basically execute iteration method ''' h = (x_n - x_0) / n assert (h > 0, 'x_n should be greater than x_0') logging.debug( f"Solving eq with steps {h}. y_0: {y_0} x_n: {x_n}, x_0: {x_0}, n: {n}" ) result_function = Function([], []) lte_function = Function([], []) gte_function = Function([], []) x_i = x_0 y_i = y_0 LTE = 0 GTE = 0 for i in range(1, n + 2): # add values to result functions result_function.append(x_i, y_i) lte_function.append(x_i, LTE) gte_function.append(x_i, GTE) # execute method y_i = self.get_next_value(f, x_i, y_i, h) # errors LTE = abs(y_solution_func(x_i + h) - \ self.get_next_value( f, x_i, y_solution_func(x_i), h ) ) GTE = abs(y_solution_func(x_i + h) - y_i) # step x_i = round(x_0 + i * h, precision) y_init = y_solution_func(x_i) return (result_function, lte_function, gte_function)