MATH/CMSC 206 - Introduction to Matlab

Announcements Syllabus Tutorial Projects Submitting

Solving Equations Numerically

Contents

What are Numerical Methods?

Some equations can't be solved symbolically. For these, we can still ask Matlab to try to find very close approximations using numerical methods. Later, we'll show some more advanced examples, but for now, let's try an easy one. Suppose we want to find the solution to the equation log(x) + x + 1 = 0. (Remember that Matlab uses log for natural log!) We can try to solve this symbolically as shown below:

syms x
solve(log(x) + x + 1)
 
ans =
 
1/(exp(1)*exp(lambertw(0, 1/exp(1))))
 

Although this answer is ugly, it is 100% exact. [By the way, exp(1) is the constant e=2.7183... The problem with this answer is that unless you are already familiar with the Lambert W (Omega) function, this ugly formula will not be very useful for you. Even if you do know what Lambert's W function is, you still have to do more work to figure out what actual numerical value the above formula represents. Sometimes you just want to see a number, even if it isn't exactly precise!

Solving an Equation Numerically using fzero

Instead of having Matlab solve the equation symbolically, let's try to do it numerically. We'll employ a command fzero which will use some sophisticated numerical techniques to find the zeros of any equation. To use the fzero command we have to first come up with an estimate of the correct answer. The fzero command will take our estimate and repeatedly apply an algorithm that will obtain a better and better approximation. Let's say our starting estimate is the value 2. We would call fzero like this:

syms x
fzero('log(x) + x + 1', 2)
ans =

    0.2785

If you'd like to learn more about how fzero works and what some of its limitiations are, consult the Matlab helpfile on fzero or wait until a bit later in the tutorial.

We'll get more practice with numerical methods later, after we've developed proper notation for representing functions in Matlab.

Self-Test

  1. First, use solve to solve the following equation symbolically: sin(x) = 0.5. Note that Matlab finds two solutions!
  2. Now try to use fzero to solve this same equation numerically. See if you can find initial estimates that will yield each of the two solutions you found in part 1.
  3. Which solutions are more precise, those found symbolically, or those found numerically?
  4. Which can be used to solve a wider spectrum of problems: symbolic methods or numerical methods?

Answers to Self-Test

Next Topic: Script M-Files