This HTML version of the book is provided as a convenience, but some math equations are not translated correctly. The PDF version is more reliable. ## Chapter 6 Zero-finding## 6.1 Why functions?The previous chapter explained some of the benefits of functions, including - Each function has its own workspace, so using functions helps avoid name collisions.
- Functions lend themselves to incremental development: you can debug the body of the function first (as a script), then encapsulate it as a function, and then generalize it by adding input variables.
- Functions allow you to divide a large problem into small pieces, work on the pieces one at a time, and then assemble a complete solution.
- Once you have a function working, you can forget about the details of how it works and concentrate on what it does. This process of abstraction is an important tool for managing the complexity of large programs.
Another reason you should consider using functions is that many of the
tools provided by MATLAB require you to write functions. For example,
in this chapter we will use ## 6.2 MapsIn mathematics, a You can think of a sequence as a map from positive integers
to elements. You can think of a vector
as a map from indices to elements. In these cases the maps
are You can also think of a function as a map from inputs to outputs, but
in this case the range is ## 6.3 A note on notationIn this chapter I need to start talking about mathematical functions, and I am going to use a notation you might not have seen before. If you have studied functions in a math class, you have probably seen something like
which is supposed to mean that
which means “f is the function that maps from
function res = error_func(x) res = x^2 - 2*x -3; end I’ll explain soon why this function is called ## 6.4 Nonlinear equationsWhat does it mean to “solve” an equation? That may seem like an
obvious question, but I want to take a minute to think about it,
starting with a simple example: let’s say that we want to know the
value of a variable, If you have taken algebra, you probably know how to “solve” this
equation: you take the square root of both sides and get
But what have you really done? The relationship you derived is
equivalent to the relationship you started with—they contain the
same information about There are two reasons. One is that the relationship is now “explicit
in The other reason is that the recipe is written in terms of operations
we know how to perform. Assuming that we know how to compute square
roots, we can compute the value of When people talk about solving an equation, what they usually mean
is something like “finding an equivalent relationship that is
explicit in one of the variables.” In the context of this book,
that’s what I will call an To demonstrate a numerical solution, consider the equation This equation is not explicit, since Here’s what would happen: >> x = 4; >> x = sqrt(2*x+3) x = 3.3166 >> x = sqrt(2*x+3) x = 3.1037 >> x = sqrt(2*x+3) x = 3.0344 >> x = sqrt(2*x+3) x = 3.0114 >> x = sqrt(2*x+3) x = 3.0038 After each iteration, Techniques that generate numerical solutions are called
## 6.5 Zero-findingA nonlinear equation like To address this limitation, it is useful to rewrite non-linear equations as zero-finding problems: - The first step is to define
an “error function” that computes how far
a given value of
*x*is from being a solution.In this example, the error function is *f*:*x*→*x*^{2}− 2*x*−3Any value of *x*that makes*f*(*x*) = 0 is also a solution of the original equation. - The next step is to find values of
*x*that make*f*(*x*) = 0. These values are called**zeros of the function**, or sometimes roots.
Zero-finding lends itself to numerical solution because we can
use the values of For example, if we can find two values Here’s what this scenario might look like on a graph: If this was all you knew about If you said, “I would connect the dots with a straight line and compute the zero of the line,” then congratulations! You just invented the secant method! And if you said, “I would evaluate Finally, if you said, “I would use a built-in MATLAB function that combines the best features of several efficient and robust numerical methods,” then you are ready to go on to the next section. ## 6.6 |

32 x^{6} − 48 x^{4} + 18 x^{2} − 1
(1) |

*The density of a duck, ρ, is 0.3 g / cm^{3} (0.3 times the
density of water).*

*The volume of a sphere ^{2} with radius r is 4/3 π r^{3}.*

*If a sphere with radius r is submerged in water to a depth d, the
volume of the sphere below the water line is *

volume = |
| (3r d^{2} − d^{3})
as long as d < 2 r |

*An object floats at the level where the weight of the displaced water
equals the total weight of the object.*

*Assuming that a duck is a sphere with radius 10 cm, at what depth does
a duck float?*

*Here are some suggestions about how to proceed:*

*Write an equation relating ρ,**d*and*r*.*Rearrange the equation so the right-hand side is zero. Our goal is to find values of**d*that are roots of this equation.*Write a MATLAB function that evaluates this function. Test it, then make it a quiet function.**Make a guess about the value of**d*_{0}to use as a starting place.*Use*`fzero`to find a root near*d*_{0}.*Check to make sure the result makes sense. In particular, check that**d*< 2*r*, because otherwise the volume equation doesn’t work!*Try different values of ρ and**r*and see if you get the effect you expect. What happens as ρ increases? Goes to infinity? Goes to zero? What happens as*r*increases? Goes to infinity? Goes to zero?

- 1
- Well, ok, there are exceptions, including
`find_triples`. Functions that don’t return a value are sometimes called “commands,” because they do something (like display values or generate a figure) but either don’t have an output variable or don’t make an assignment to it. - 2
- This example is adapted from Gerald
and Wheatley,
*Applied Numerical Analysis*, Fourth Edition, Addison-Wesley, 1989.