HotelInfantesAgres - Bawat tanong, may sagot. Logo

In Computer Science / Senior High School | 2025-03-18

► Create a running program for a bisection method in any programming languages. The user must have the freedom to enter the required number of iteration, tol, and a function. ► Present the program and discuss the function of each line, by dividing the lines with your groups. Try to input this function: ►X^2-3 ►2x^2+2x-4 ►X^2-1000​

Asked by nu143

Answer (1)

Answer:Let's create a Python program for the bisection method, allowing the user to input the function, number of iterations, and tolerance. python

import mathdef bisection_method(): # Get user inputs func_str = input("Enter the function f(x) (e.g., 'x**2 - 3'): ") # Define the function using eval() def f(x): return eval(func_str) a = float(input("Enter the lower bound 'a': ")) b = float(input("Enter the upper bound 'b': ")) tol = float(input("Enter the tolerance: ")) n_iter = int(input("Enter the maximum number of iterations: ")) # Check if initial interval brackets a root if f(a) * f(b) >= 0: print("The function does not have a root in the given interval.") return # Bisection method loop for i in range(n_iter): c = (a + b) / 2 # Group 1: Calculating the midpoint 'c' and checking for convergence if abs(f(c)) <= tol: print(f"The root is approximately: {c:.6f}") print(f"Number of iterations: {i+1}") return # Group 2: Updating the interval based on the sign of f(c) if f(a) * f(c) < 0: b = c else: a = c # Group 3: Reaching the maximum iterations without convergence print(f"Maximum iterations reached. Root may not have been found within the given tolerance.")if __name__ == "__main__": bisection_method()  Explanation by Groups: Group 1: Initial Setup and Input: -  import math : Imports the math module for potential use (not strictly needed for this example).-  def bisection_method(): : Defines the main function.-  func_str = input("Enter the function f(x) (e.g., 'x**2 - 3'): ") : Prompts the user to enter the function in string format.-  def f(x):\n return eval(func_str) : Defines a nested function 'f' using the  eval()  function to dynamically evaluate the user-input function string.-  a = float(input("Enter the lower bound 'a': ")) : Gets the lower bound of the interval.-  b = float(input("Enter the upper bound 'b': ")) : Gets the upper bound of the interval.-  tol = float(input("Enter the tolerance: ")) : Gets the tolerance for convergence.-  n_iter = int(input("Enter the maximum number of iterations: ")) : Gets the maximum number of iterations.-  if f(a) * f(b) >= 0: : Checks if the function has opposite signs at the interval's endpoints (necessary for the bisection method to work). If not, it prints an error message and returns. Group 2: Bisection Iteration: -  for i in range(n_iter): : Starts a loop for the specified number of iterations.-  c = (a + b) / 2 : Calculates the midpoint of the interval.-  if abs(f(c)) <= tol: : Checks if the function value at the midpoint is close enough to zero (within the tolerance). If so, the root is found, and the program prints the result and returns.-  if f(a) * f(c) < 0: : Checks if the function has opposite signs at the lower bound ('a') and the midpoint ('c'). If so, the root lies in the left half of the interval, so the upper bound ('b') is updated to 'c'.-  else: : If the sign is not opposite, the root lies in the right half of the interval, so the lower bound ('a') is updated to 'c'. Group 3: Reaching Maximum Iterations: -  print(f"Maximum iterations reached. Root may not have been found within the given tolerance.") : If the loop reaches the maximum number of iterations without convergence, this message is printed. Running the Program: Save the code as a Python file (e.g.,  bisection_method.py ).Run it from your terminal:  python bisection_method.py .Input the function, interval bounds, tolerance, and maximum iterations when prompted. **Testing

Answered by shelmariereuyan5 | 2025-03-18