Procedure:
1. We will discard the other interval without the root.
2. First, evaluate fp.
3. If f(p)>0, replace a such that the new interval is (p,b). Redefine a as p.
4. If f(p)<0, replace b such that the new interval is (a,p). Redefine b as p.
5. Note that after the bisection, the interval is half the width of the previous interval.
Based upon Dr. Keyser's Original Revised Summer 2019 TLF Lab Assignment #12b
Writing the Python Code to Plot a Cubic Polynomial:
You will want to plot your cubic polynomial so that you can determine the interval (a,b) for implementing the bisection method discussed above. There are plenty of web-based programs that will plot an equation, but since we know all about matplotlib, we should be able to write a program of our own.
The code to plot a cubic polynomial should perform the following tasks:
1. Print a message indicating the purpose of the program and simple instructions.
2. Read the coefficients of the cubic polynomial from the keyboard.
- Use one input statement for retrieving all four coefficients.
- Create a Python list with the coefficients as elements of the list.
3. Use numpy functions and a user-defined function to populate numpy arrays for x-values and y-values.
4. Create a user-defined function in your code to evaluate f(x) for the cubic polynomial given a value of x.
- def evalCubic(coeffs, xvalue):
- When calling the function, send the name of the coefficient list and the x value of interest to the function as parameters.
- The return value should be f(x) or a y value.
5. Choose an appropriate interval for your plot.
- This interval consists of two x values which determine the left and right limits of your plot.
- (Note: this interval is for plotting, not for the bisection method)
6. Use numpy.linspace to create an array of x-values for the plot.
- Use enough values so that you can accurately choose the interval (a,b) for the bisection method.
7. Create a numpy array of values using the array of x-values and your user-defined function.
8. Use matplotlib functions to create a plot of your cubic polynomial.
- Include title, x-axis label, and y-axis label.