Answered step by step
Verified Expert Solution
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 cosxx i tried inputting npcosxx and symcosxx and "cosxx 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 worksimport sympy as symimport numpy as npfrom sympy import symbols, sympify, cosfrom prettytable import PrettyTable def bisectionmethodfunc a b erroraccept: def fx: return evalfunc error absb a iterations while error erroraccept: c b a if fa fb: printNo root or multiple roots present, therefore, the bisection method will not work!" quit elif fc fa: b c error absb a elif fc fb: a c error absb a else: printSomething went wrong" quit iterations.appendleniterations a b c fc printfThe error is error printfThe lower boundary, a is a and the upper boundary, b is b return iterations #newtons methoddef newtonsmethodx iterationNumber, func, E: def fx: return evalfunc x symSymbolx sympifiedfunc symsympifyfunc # Renamed variable to avoid conflict df symdiffsympifiedfunc, x ffunc symlambdifyx sympifiedfunc, 'numpy' dffunc symlambdifyx df 'numpy' x x iterations for i in rangeiterationNumber: xnext x ffuncx dffuncx residual npabsffuncxnext iterations.appendi x ffuncx dffuncx xnext if residual E: return xnext, iterations x xnext return None, iterations #Secant methoddef secantmethodfunc x x E iterationNumber: def fx: return evalfunc iterations iteration while iteration iterationNumber: fx fx fx fx if absfx E: iterations.appenditeration x x x fx return x iterations xnext roundx fxx xfx fx residual absxnext x iterations.appenditeration x x xnext, fx residual if residual E: return xnext, iterations x x x xnext iteration printWarning: Secant method did not converge within the specified maximum number of iterations." return None, iterations def main: funcstr inputEnter the function in terms of x: x symbolsx x floatinputEnter the first initial guess value x: x floatinputEnter the second initial guess value x: E floatinputEnter the toleranceerror value E: n intinputEnter the maximum number of iterations: # Bisection method table bisectioniterations bisectionmethodfunc x x E bisectiontable PrettyTable bisectiontable.fieldnames Iterationabcfc for iteration in bisectioniterations: bisectiontable.addrowiteration print
Bisection method iterations:" printbisectiontable # Newton's method solution, newtoniterations newtonsmethodx n func, E if solution is not None: printfNewtons method converged to solution: solution with residual: npabsffuncsolution printfThe root found by Newton's method: solution else: printNewtons method did not converge within the specified maximum number of iterations." #newtons table newtontable PrettyTable newtontable.fieldnames Iterationxfxfxxnext" for iteration in newtoniterations: newtontable.addrowiteration print
Newton's method iterations:" printnewtontable # Secant method table solution, secantiterations secantmethodfunc x x E n if isinstancesolution float: printfThe root found by the secant method: solution secanttable PrettyTable secanttable.fieldnames Iterationxxxnext", fxnextxnext x for iteration in secantiterations: secanttable.addrowiteration print
Secant method iterations:" printsecanttable if namemain: main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started