Nov-23-2022, 06:48 PM
Hello, I am making a program that looks for the root of a polynomial by different methods (bisection, false position, raphson and secant).
I have the code of the individual programs where each one asks the user to enter the values necessary to perform the method. How can I implement all the programs in one and create a menu to select the program that the user wants to use?(that is for university course no problem use high level coding)
for example
Root find method
choose option
1
2
3
option#
***** Method
insert the funtion x**2+x+3
insert initial variable #
error#
(each program separate by #--------------------)
Also how to modify these codes to be able to enter a function instead of a fixed one.
I have the code of the individual programs where each one asks the user to enter the values necessary to perform the method. How can I implement all the programs in one and create a menu to select the program that the user wants to use?(that is for university course no problem use high level coding)
for example
Root find method
choose option
1
2
3
option#
***** Method
insert the funtion x**2+x+3
insert initial variable #
error#
(each program separate by #--------------------)
Also how to modify these codes to be able to enter a function instead of a fixed one.
#biseccion def f(x): return x**3-5*x-9 # Implementing Bisection Method def bisection(x0,x1,e): step = 1 print('\n\n*** BISECTION METHOD IMPLEMENTATION ***') condition = True while condition: x2 = (x0 + x1)/2 print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2))) if f(x0) * f(x2) < 0: x1 = x2 else: x0 = x2 step = step + 1 condition = abs(f(x2)) > e print('\nRequired Root is : %0.8f' % x2) # Input Section x0 = input('First Guess: ') x1 = input('Second Guess: ') e = input('Tolerable Error: ') # Converting input to float x0 = float(x0) x1 = float(x1) e = float(e) # Checking Correctness of initial guess values and bisecting if f(x0) * f(x1) > 0.0: print('Given guess values do not bracket the root.') print('Try Again with different guess values.') else: bisection(x0,x1,e) #false--------------------------------------- def f(x): return x**3-5*x-9 # Implementing False Position Method def falsePosition(x0,x1,e): step = 1 print('\n\n*** FALSE POSITION METHOD IMPLEMENTATION ***') condition = True while condition: x2 = x0 - (x1-x0) * f(x0)/( f(x1) - f(x0) ) print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2))) if f(x0) * f(x2) < 0: x1 = x2 else: x0 = x2 step = step + 1 condition = abs(f(x2)) > e print('\nRequired root is: %0.8f' % x2) # Input Section x0 = input('First Guess: ') x1 = input('Second Guess: ') e = input('Tolerable Error: ') # Converting input to float x0 = float(x0) x1 = float(x1) e = float(e) #Note: You can combine above two section like this # x0 = float(input('First Guess: ')) # x1 = float(input('Second Guess: ')) # e = float(input('Tolerable Error: ')) # Checking Correctness of initial guess values and false positioning if f(x0) * f(x1) > 0.0: print('Given guess values do not bracket the root.') print('Try Again with different guess values.') else: falsePosition(x0,x1,e) #------------------------------------------------------- def f(x): return x**3 - 5*x - 9 # Defining derivative of function def g(x): return 3*x**2 - 5 # Implementing Newton Raphson Method def newtonRaphson(x0,e,N): print('\n\n*** NEWTON RAPHSON METHOD IMPLEMENTATION ***') step = 1 flag = 1 condition = True while condition: if g(x0) == 0.0: print('Divide by zero error!') break x1 = x0 - f(x0)/g(x0) print('Iteration-%d, x1 = %0.6f and f(x1) = %0.6f' % (step, x1, f(x1))) x0 = x1 step = step + 1 if step > N: flag = 0 break condition = abs(f(x1)) > e if flag==1: print('\nRequired root is: %0.8f' % x1) else: print('\nNot Convergent.') # Input Section x0 = input('Enter Guess: ') e = input('Tolerable Error: ') N = input('Maximum Step: ') # Converting x0 and e to float x0 = float(x0) e = float(e) # Converting N to integer N = int(N) # Starting Newton Raphson Method newtonRaphson(x0,e,N) #------------------------------------------------------- def f(x): return x**3 - 5*x - 9 # Implementing Secant Method def secant(x0,x1,e,N): print('\n\n*** SECANT METHOD IMPLEMENTATION ***') step = 1 condition = True while condition: if f(x0) == f(x1): print('Divide by zero error!') break x2 = x0 - (x1-x0)*f(x0)/( f(x1) - f(x0) ) print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2))) x0 = x1 x1 = x2 step = step + 1 if step > N: print('Not Convergent!') break condition = abs(f(x2)) > e print('\n Required root is: %0.8f' % x2) # Input Section x0 = input('Enter First Guess: ') x1 = input('Enter Second Guess: ') e = input('Tolerable Error: ') N = input('Maximum Step: ') # Converting x0 and e to float x0 = float(x0) x1 = float(x1) e = float(e) # Converting N to integer N = int(N) # Starting Secant Method secant(x0,x1,e,N)