Assume that you have done a preliminary investigation (for instance, you used softwareto graph), and determined that the equationf (x) = 0.25√x − (sin(x/3))2 + 0.12 = 0has 2 distinct roots in the interval [0, 10]. (Note that there is no way to solve this equationexactly using pencil and paper.)(a)[1] Write a function to calculate the values of f (x), and use it to determine the intervalswhere the two roots lie. (This means you need to identify two intervals, so that eachinterval contains one root. Of course, there are many correct choices.)(b)[2] Use the bisection method (look at the template for the values of the parameters) tolocate the two solutions, with the tolerance as given in the template. Start the bisectionwith the intervals that you identified in (a). Output: for each solution, print the intervalwhich contains it.
Question
Assume that you have done a preliminary investigation (for instance, you used softwareto graph), and determined that the equationf (x) = 0.25√x − (sin(x/3))2 + 0.12 = 0has 2 distinct roots in the interval [0, 10]. (Note that there is no way to solve this equationexactly using pencil and paper.)(a)[1] Write a function to calculate the values of f (x), and use it to determine the intervalswhere the two roots lie. (This means you need to identify two intervals, so that eachinterval contains one root. Of course, there are many correct choices.)(b)[2] Use the bisection method (look at the template for the values of the parameters) tolocate the two solutions, with the tolerance as given in the template. Start the bisectionwith the intervals that you identified in (a). Output: for each solution, print the intervalwhich contains it.
Solution
(a) First, we need to define the function f(x) in Python:
import numpy as np
def f(x):
return 0.25*np.sqrt(x) - (np.sin(x/3))**2 + 0.12
Next, we can use a loop to evaluate the function at various points in the interval [0, 10] and identify where the function changes sign. This will give us the intervals where the roots lie.
intervals = []
for x in np.arange(0, 10, 0.1):
if f(x) * f(x + 0.1) < 0:
intervals.append((x, x + 0.1))
(b) Now we can use the bisection method to find the roots. The bisection method works by repeatedly dividing the interval in half and selecting the subinterval where the function changes sign.
def bisection(a, b, tol):
if f(a) * f(b) > 0:
print("Bisection method fails.")
return None
else:
c = a
while ((b-a) >= tol):
c = (a+b)/2
if f(c) == 0.0:
break
elif f(c)*f(a) < 0:
b = c
else:
a = c
return c
roots = [bisection(a, b, 0.0001) for a, b in intervals]
Finally, we can print the intervals and the roots:
for i in range(len(roots)):
print(f"Root {i+1} lies in the interval {intervals[i]} and is approximately {roots[i]}")
This will give you the intervals where the roots lie and the approximate values of the roots.
Similar Questions
onsider finding (approximately) the root x⋆ of the equationex + x − 2023 = 0.(a) (i) Using Matlab, or otherwise, plot the function f (x) = ex + x − 2023 for 0 ≤ x ≤ 10.Sketch the plot in your answer. We won’t collect and assess any files from yourcomputer.
Consider finding (approximately) the root x⋆ of the equationex + x − 2023 = 0.(a) (i) Using Matlab, or otherwise, plot the function f (x) = ex + x − 2023 for 0 ≤ x ≤ 10.Sketch the plot in your answer. We won’t collect and assess any files from yourcomputer. [3 marks](ii) Using the plot, find an interval [a, b] ⊂ [0, 10], such that a, b are consecutive integers(a, b ∈ Z, b = a + 1), and the root x⋆ of the function f (x) is in the interval, x⋆ ∈ [a, b].State the interval [a, b] in your answer
y= f(x)=x^2-x-1 y = = f(x)=x^3-x^2-2x+1 Implement Bisection Method to find all the possible roots of these 2 given functions and verify it with built-in functions scipy.optimize.root() in the SciPy library. follow this given template # Root Finding Method import math import numpy as np import scipy as sp import matplotlib.pyplot as plt def plot_function(func, a, b): """ This function plot the graph of the input func within the given interval [a,b). """ # Your code goes here def bisection_method(func, a, b, tol=1e-6, max_iter=100): """ Bisection method to find the root of a function within a given interval. Parameters: - func: The function for which the root is to be found. - a, b: Interval [a, b] within which the root is searched for. - tol: Tolerance level for checking convergence of the method. - max_iter: Maximum number of iterations. Returns: - root: Approximation of the root. Example -------- >>> fun = lambda x: x**2 - x - 1 >>> root = bisection_method(fun, 1, 2, max_iter=20) """ # Check if the interval is valid (signs of f(a) and f(b) are different) # Your code goes here # Main loop starts here iter_count = 1 while iter_count <= max_iter: # your code goes here iter_count += 1 print("Warning! Exceeded the maximum number of iterations.") return root # Example usage: if __name__ == "__main__": # Define the function for which the root is to be found func = lambda x: x**2 - x - 1 # First Function # Uncomment the below line to use the Second Function # func = lambda x: x**3 - x**2 - 2*x + 1 # Second Function # Call plot_function to plot graph of the function # Your code goes here # Set the interval [a, b] for the search a_1 = 0; b_1 = 0; # For first root (change the values as required) a_2 = 0; b_2 = 0; # For second root (change the values as required) # Call the bisection method our_root_1 = # your code goes here our_root_2 = # your code goes here # Call SciPy method root, which we consider as a reference method. x0 = (a_1 + b_1)/2 sp_result_1 = sp.optimize.root(func, x0) sp_root_1 = sp_result_1.x.item() x0 = (a_2 + b_2)/2 sp_result_2 = sp.optimize.root(func, x0) sp_root_2 = sp_result_2.x.item() # Print the result print("1st root found by Bisection Method = {:0.8f}.".format(our_root_1)) print("1st root found by SciPy = {:0.8f}".format(sp_root_1)) print("2nd root found by Bisection Method = {:0.8f}.".format(our_root_2)) print("2nd root found by SciPy = {:0.8f}".format(sp_root_2))
We cannot algebraically solve 2x = 3x but we can locate the solutions using the Intermediate Value Theorem. Consider the continuous function f(x) = 2x − 3x. The roots of are exactly the solutions to our equation. Consider the following intervals.(0, 1)(1, 2)(2, 3)(3, 4)On which of the above intervals, does the Intermediate Value Theorem guarantee a solution to the equation 2x = 3x?1 and 42 and 4 1, 2, and 41 and 3
Graphs of sin x and sin 2x intersect at x = _______ in the interval (0, π)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.