Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Copy paste the python code and send me the screenshot also if it is possible to check the indintation . The language of the below

Copy paste the python code and send me the screenshot also if it is possible to check the indintation . The language of the below code is pythonnewtons method does not work when the input function is cos(x)-x/3, i tried inputting np.cos(x)-x/3, and sym.cos(x)-x/3 and "cos(x)-x/3" and non of them work, they all give me an error.It should print the root that was found for each method (bisection, newtons and secant) and the table of iterations for each This is my code:#Question 1(works)import sympy as symimport numpy as npfrom sympy import symbols, sympify, cosfrom prettytable import PrettyTable def bisection_method(func, a, b, error_accept): def f(x): return eval(func) error = abs(b - a) iterations =[] while error > error_accept: c =(b + a)/2 if f(a)* f(b)>=0: print("No root or multiple roots present, therefore, the bisection method will not work!") quit() elif f(c)* f(a)<0: b = c error = abs(b - a) elif f(c)* f(b)<0: a = c error = abs(b - a) else: print("Something went wrong") quit() iterations.append([len(iterations)+1, a, b, c, f(c)]) print(f"The error is {error}") print(f"The lower boundary, a, is {a}, and the upper boundary, b, is {b}") return iterations #newtons methoddef newtons_method(x1, iterationNumber, func, E): def f(x): return eval(func) x = sym.Symbol('x') sympified_func = sym.sympify(func) # Renamed variable to avoid conflict df = sym.diff(sympified_func, x) f_func = sym.lambdify(x, sympified_func, 'numpy') df_func = sym.lambdify(x, df, 'numpy') x = x1 iterations =[] for i in range(iterationNumber): x_next = x -(f_func(x)/ df_func(x)) residual = np.abs(f_func(x_next)) iterations.append([i +1, x, f_func(x), df_func(x), x_next]) if residual < E: return x_next, iterations x = x_next return None, iterations #Secant methoddef secant_method(func, x0, x1, E, iterationNumber): def f(x): return eval(func) iterations =[] iteration =0 while iteration < iterationNumber: f_x0= f(x0) f_x1= f(x1) if abs(f_x1)< E: iterations.append([iteration+1, x0, x1, x1, f_x1,0]) return x1, iterations x_next = round(x1- f_x1*(x1- x0)/(f_x1- f_x0),6) residual = abs(x_next - x1) iterations.append([iteration+1, x0, x1, x_next, f_x1, residual]) if residual < E: return x_next, iterations x0, x1= x1, x_next iteration +=1 print("Warning: Secant method did not converge within the specified maximum number of iterations.") return None, iterations def main(): func_str = input("Enter the function in terms of x: ") x = symbols('x') x1= float(input("Enter the first initial guess value (x1): ")) x2= float(input("Enter the second initial guess value (x2): ")) E = float(input("Enter the tolerance/error value (E): ")) n= int(input("Enter the maximum number of iterations: ")) # Bisection method table bisection_iterations = bisection_method(func, x1, x2, E) bisection_table = PrettyTable() bisection_table.field_names =["Iteration","a","b","c","f(c)"] for iteration in bisection_iterations: bisection_table.add_row(iteration) print("
Bisection method iterations:") print(bisection_table) # Newton's method solution, newton_iterations = newtons_method(x1, n, func, E) if solution is not None: print(f"Newton's method converged to solution: {solution} with residual: {np.abs(f_func(solution))}") print(f"The root found by Newton's method: {solution}") else: print("Newton's method did not converge within the specified maximum number of iterations.") #newtons table newton_table = PrettyTable() newton_table.field_names =["Iteration","x","f(x)","f'(x)","x_next"] for iteration in newton_iterations: newton_table.add_row(iteration) print("
Newton's method iterations:") print(newton_table) # Secant method table solution, secant_iterations = secant_method(func, x1, x2, E, n) if isinstance(solution, float): print(f"The root found by the secant method: {solution}") secant_table = PrettyTable() secant_table.field_names =["Iteration","x0","x1","x_next", "f(x_next)","x_next - x1"] for iteration in secant_iterations: secant_table.add_row(iteration) print("
Secant method iterations:") print(secant_table) if __name__=="__main__": main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced MySQL 8 Discover The Full Potential Of MySQL And Ensure High Performance Of Your Database

Authors: Eric Vanier ,Birju Shah ,Tejaswi Malepati

1st Edition

1788834445, 978-1788834445

More Books

Students also viewed these Databases questions

Question

What is an SLA?

Answered: 1 week ago