MATLAB:Fzero/Examples
The following will be a series of more examples using the fzero
command:
Contents
Very Simple
Assume there is some function
and you are trying to determine the value of \(x\) for which \(f(x)=0\). There are many different ways to use MATLAB and fzero to find the solution:
Put the calculation in the fzero command
If the function only requires one line of code to write, you can put the anonymous function that does the calculation directly in the fzero command. fzero will know it has control only over the dummy variable given in the argument list after the @ symbol. You will also need to give either an initial guess, for example\[x=2\]
[xVal, fVal] = fzero(@(xD) xD+1, 2)
or a valid initial bracket (valid meaning the sign of the function changes from one side to the other):
[xVal, fVal] = fzero(@(xD) xD+1, [-2 2])
If you give an invalid initial bracket, such as
[xVal, fVal] = fzero(@(xD) xD+1, [0 5])
where in this case the values of the function are positive at both \(f(0)\) and \(f(5)\), fzero will not run. Note in the remaining examples below that the initial guess case is generally used; in all those cases, you can substitute in a valid initial bracket.
Create a named anonymous function and then call it
You can also create the anonymous function in advance and then call it in the fzero command. This is useful if you need to use the function again later, say, for a plot:
MyFun = @(x) x+1
[xVal, fVal] = fzero(@(xD) MyFun(xD), 0)
or, again, you can use an initial bracket as long as it is valid.There's extra code here, but again, if you need to use the function for more than just finding zeros, this is convenient.
Put the function in a function file and then call it
If you have a calculation that requires more than one line of code, you can create your own function and then have fzero call it. For example, you could create a file called MyFunFile.m
that contains
function out = MyFunFile(in)
out = in + 1;
abd then, in the command window or in your script, you can write <course lang=matlab> [xVal, fVal] = fzero(@xD) MyFunFile(xD), 0) </course> or use an initial valid bracket.
More complicated - multiple possible answers =
The fzero command can only find one root at a time, so you may need to run it several times to calculate all the answers. As an example, if there is some function:
and you are trying to determine the value(s) of \(y\) for which \(g(y)=0\) you will need to run fzero twice. You can try to give initial guesses close to the root you are looking for:
g = @(y) (y+1).*(y-2)
[yVal1, gVal1] = fzero(@(yD) g(yD), 0)
[yVal2, gVal2] = fzero(@(yD) g(yD), 3)
ends up working. Typically, however, you will want to graph the equation, look for the sign changes, and then use a valid bracket to make sure you are getting the right root. That means first writing something on the order of:
g = @(y) (y+1).*(y-2)
y = linspace(-4, 4, 100);
plot(y, g(y), 'k-')
to see the values or
plot(y, sign(g(y)), 'k-')
to see the sign changes followed by fzero commands that bracket the individual roots. The actual independent values for the bracket may vary; here are some examples:
[yVal1, gVal1] = fzero(@(yD) g(yD), [-2 0])
[yVal2, gVal2] = fzero(@(yD) g(yD), [1 4])