Lesson 5 Individual Assignment

Individual means that you do it yourself. You won't learn to code if you don't struggle for yourself and write your own code. Remember that while you can discuss the general (algorithmic) way to solve a problem, you should not even be looking at anyone else's code or showing anyone else your code for an individual assignment. Ask an instructor if you are really stuck and having very specific code problems.
Review the Group Work guidelines on Cavas and/or ask an instructor if you have any questions.

Background info

Look back at the information in the Lesson 5 Pre-Activity. You will need a lot of information from that pre-activity for this assignment.

Programming Practice

Be sure to spell all function names correctly - misspelled functions will lose points (and often break anyway since no one is sure what to type to call it). If you prefer showing your earlier, scratch work as you figure out what you are doing, please be sure that you make a final, complete, correct last function in its own cell that you then call several times to test. In other words, separate your thought process/working versions from the final one (a comment that tells us which is the final version would be lovely).

Every function should have at least a docstring at the start that states generally what it does (see Lesson3 team if you need a reminder). Make other comments as necessary.

A. Define an equation3 function that takes one parameter – the root estimate, x, and returns f(x) given by Equation 3 from the Lesson 5 Pre-Activity.

$$f(x) = x^2 - sin(x)-1=0 \hspace{30 mm} (3)$$

In [ ]:

Test your equation3 function on the inputs

  • x = 0
  • x = 1
  • x = 2

Make sure to comment on the answers, are they correct? how do you know that they are correct?


In [ ]:

B. Now define a function function that takes one parameter – the root estimate, x, and returns one value. This function should call equation3 with its parameter and return the return value from equation3. The advantage of this function is that it will allow us to write a single bisection program and modify which equation the function function calls.

C. Define a bisection function that takes a single parameter - a variable defined as a tuple (or a list) containing the initial values of a and b representing the interval for the root. This function should return the root estimate x based on Equation 2 from the Lesson 5 Pre-Activity $x=\frac{(a+b)}{2}$. Before returning x, your bisection function should calculate and print the error, for this root estimate based on values of a and b. If you need more info on tuples, check this link.

Test your bisection function based on your answers to Pre-Activity Question 2. At this point, your output should match the first line of the your table (iteration 1). Make sure to comment on the answers, are they correct? how do you know that they are correct?

D. Modify your bisection function so it calculates the product of $f(a) \times f(x)$ and evaluates wherther or not $f(a) \times f(x) <0$. You should include two calls to the function function to evaluate: 1. $f(a)$ and 2. $f(x)$. Based on the sign (is it $<0$ or not), the bisection function should determine a new range, new root estimate, and maximum error for this current root estimate. Print the new range. Note: Your function should still print the error (now based on the new range) and return a new estimate, x (which should match iteration 2).

Test your bisection function multiple times and compare your results to your answers from the Pre-Activity. You’ll find you’ll need to make multiple calls to the bisection function. Make sure to comment on the answers, are they correct? how do you know that they are correct? There must be a better way of doing this!

E. Modify your bisection function so that it completes ten iterations of the bisection method, prints the final maximum error and returns the final root estimate.

While you are working on your function, you’ll find it is useful to continue to print the range (a and b) each loop iteration. After you are sure your function is working, you can comment out this print statement.

After ten iterations, your bisection function should have a small maximum error. But what happens if the initial interval is 0 to 100, instead of 1 to 2? Try it and explain the results (both the estimate and the error)!

F. Scientists typically will have a maximum error (i.e. value) that is acceptable for a particular application. The following relationship can be used to compute the number of bisections required, $n$, to reach a prescribed $\epsilon$.

$$n = \frac{\ln\left(\frac{|b-a|}{\epsilon}\right)}{\ln 2} \hspace{30 mm} (4)$$

Add a third variable to the parameter list for the bisection function. The value of the third element of the list should be the maximum error acceptable. Your function should calculate the number of iterations required (by using equation 4) and then complete that number of iterations of the bisection method.

Test your program by examining the printed error, but after you know it works, comment out the print command inside the bisection function. Make sure to comment on the answers, are they correct? how do you know that they are correct?

G. Make a new equation1 function which should take one parameter and return a value (from Equation 1 in the pre-acticity). Modify the function function so it calls equation1 instead of equation3. Use your program to determine the position for the minimum compression and maximum expansion for the spring explained and plotted using the values in the pre-activity. The acceptable error is 0.01 m. Make sure that you execute a function call that produces the correct answers (2 roots - compressed and expanded). Make sure to comment on the answer, is it correct? how do you know that it is correct?

Thanks to the function function, you shouldn’t need to modify the bisection function at all!


In [ ]: