Section 7.1 Motivation for solving equations numerically
We often want to solve an equation of the form \(f(x)=0\text{,}\) given a real-valued function \(f\) of one real variable \(x\text{.}\) Some such equations admit a symbolic solution, obtained by some algebraic manipulations. For example, the equation \(2e^x - 3 = 0\) has the solution \(x=\log(3/2)\text{.}\) But if we put together two or more unrelated functions, for example \(2e^x + x^3 - 1 = 0\text{,}\) algebra does not help.
For this reason, Matlab has a built-in root-finding function fzero
. Its first argument is the function which should be equated to zero, the second argument is our initial guess at where the solution may be (put 0 if you have no idea). For example,
fzero(@(x) 2*exp(x) + x^3 - 1, 0)
finds the solution -0.5439
. But even though we have fzero
, we still have to understand the methods used for finding roots, because
-
fzero
only gives one solution, there may be others; - the output of
fzero
may be wrong.
An example of wrong answer:
fzero(@(x) exp(-x^2), 0)
returns x=-28.9631
. In reality, \(e^{-x^2} = 0\) has no solutions, and there is nothing special about the number produced by fzero
: this is just where its algorithm decided to stop, thinking “close enough”.