**Single equations in one unknown using MATLAB**

To find the value of x that makes f(x) = 0 in MATLAB we use the fzero function. The command to use this function, in its simplest form, is written

>>fzero('f', x0)

This command solves the following problem for x.

f(x) = 0

starting from an initial guess of x0. Sometimes the function will have more than one solution, and that can be determined only by using the command with a different x0.

Of course you have to define the function, which you do in MATLAB by creating an M-file, called f.m. Here, for example, use

function y = f(x)

y = x*x - 2*x - 8;

and save it as f.m. Then issue the command

>>fzero('f',0)

to get the result:

ans = -2.

We can test the result by saying

>>feval('f',ans)

All this tests is our use of MATLAB, since any errors in the M-file f.m are still there. To test the M-file, we can remove the ';' from the function definition, evaluate the function using the 'feval' command for a particular value of ans and compare the result for each line in the calculation of the function. (Here there was only one line, but in more complicated cases the results can be checked line by line, and checks of a single line are easier than checks of the whole function.)

In all the commands and M-files above, the 'f' can be replaced by other things, say prob1. Just be sure you change it in three places: the name of the M-file, the first line of the M-file, and in the the command. Additional forms of the command are:

>>fzero('function', x0, options)

>>z = fzero('f',x0)

The options vector allows you to set certain quantities, like the tolerance. See how by saying

>>help foptions.

Also see the examples below. In the last option the result is put into the variable z.

In this example, to get the other root, run the program with x0 = 3. Multiple roots can be found only if you search for them starting with different guesses. <