Unit 1: Programming Basics

Lesson 5: For Loops - Pre-activity

Scientific Context: Numerical Analysis

Suppose you have a mathematical model and you want to understand its behavior. That is, you want to find a solution to the set of equations. We say we have an analytic solution if we can solve the equation explicitly in terms of a bounded expression of elementary functions. For example, the roots of any quadratic equation can be solved for using a closed form expression in terms of addition, subtraction, multiplication, division, and square root.

A different approach to this problem is to solve for the roots numerically. A numerical method for determining the roots of an equation begins with an initial guess of the solution and systematically uses successive approximations that ultimately converge to the “exact” solution.

As you can imagine, computers are excellent at numerical solutions, and less good at analytic solutions. For example, how can a computer integrate a function? Turns out computers are often using numerical methods.

Fundamental Principles: Bisection Method

For any function, the approximate location of roots can be determined graphically, by seeing where the function crosses the x-axis. For example, consider stretching and compressing a spring from its equilibrium (rest) position, xeq. The solutions (roots) to the following equation, determine the maximum and minimum positions to which a spring, with force constant, k, can stretch or compress for a given potential energy, V:

$$V=\frac{1}{2}k(x-x_{eq})^2 \hspace{30 mm} (1)$$

This function can be represented graphically, for k = 1.00 N/m, xeq = 3.25 m, and V = 4.00 J as follows:

The roots of this equation are the X values where the equation equals zero.

You can get a better approximation for the root (where the function crosses the x-axis) by graphing a smaller interval and “zooming” in on the area where the graph crosses the x-axis. One of the two roots is shown zoomed in below:

How do we estimate the root using numerical methods?

The bisection method follows a similar approach to estimating the root as the graph above, without actually graphing the function. It starts with two initial estimates of the root, a and b such that f(a) and f(b) lie on either side of the actual root. Thus f(a) × f(b) < 0. A closer estimate to the actual root is found by bisecting (dividing into 2 equal parts) the interval between the two original guesses:

$$root\_guess=\frac{(a+b)}{2} \hspace{30 mm} (2)$$

If f(a) × f(root_guess) < 0, then the actual root must be between a and root_guess. To determine the next estimate for the root, the old value of b is replaced by the value of root_guess. Otherwise the root lies between b and root_guess and the old value of a is replaced by the value of root_guess. This procedure is repeated bisecting the new range until the interval has been reduced to a small value ε (epsilon), such that: $$\varepsilon = \frac{|b-a|}{2}$$ where ε (epsilon) defines the magnitude of the error in the final root estimate, root_guess.

Pre-Activity Questions

1. If a root lies between a and b, explain why f(a) × f(b) < 0.

2. Consider the equation:

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

Assume that you know that it has a root is between 1 and 2 (for example by graphing it). Determine a numerical estimate for the root of $f(x)$, called root_guess in the table below, by bisection (Equation 2) and its error (epsilon). Next identify the appropriate interval for the next estimate based on the result of f(a) × f(root_guess). Complete 4 iterations (repetitions) of the bisection method below.

iteration a b root_guess epsilon f(a) x f(root_guess)
1 1 2
2
3
4