The C05 chapter - Finding the root of an equation
We start with the calculation of the zeros of a continuous function. The C05 chapter contains several routines for doing this (as well as other routines which solve a set of nonlinear equations in several unknowns). In our first example, we solve a quadratic equation.
25x2 - 10x + 1 = 0
Of course, it is trivial to determine analytically that both roots are at x = 0.2 (which provides a handy check on our numerical result - see below) but the C05 routines can be used to find the zeros of any function, as long as it is continuous. Here, we use routine c05ax; this evaluates the function via reverse communication, which allows us to display the intermediate results on the way to finding the root.
Here's the code, including some plotting commands:
|
and the results are shown in this picture:
Figure 1: Finding the root of a quadratic.
As a second example, we can use the same code to find the root of a different function (for example, one that is harder to determine analytically) by changing the first few lines of the code above to:
|
giving the results shown in Figure 2.
Figure 2: Finding the root of a transcendental function.