# How To Use Fzero In Matlab

This tutorial is about how to use fzero in Matlab. Before moving towards the ‘fzero’ command, let’s first understand what functions are, types of functions and why do we need ‘fzero’ command. The function is basically an expression that defines the relationship between the dependent and independent variables. It maps a set of inputs to a set of possible outputs where each input is related to exactly one output. Functions can be classified into linear and nonlinear functions.

## use of fzero in linear equations

A linear function is a polynomial function with degree 0 or 1 and when plotted gives a straight-line graph. On the other hand, nonlinear functions are all other functions with the highest exponent greater than 2. Although it is a polynomial function, it does not follow a straight line. Figure (1) shows an example of linear and non-linear functions.

The plot of y = x in figure 1(a) is a linear function whereas the graph shown in figure 1(b) is a parabola which is not a straight line and therefore it is a non-linear function. The linear equations can be solved manually with a pencil, but it is difficult to solve non-linear equations. Consider the following non-linear equation:

f(x) = x – Sin(x)

x – Sin(x) = 0

In order to find the value of x, we have to find the value of x for f(x)=0. The value of x for which f(x) is equal to 0 is known as the root. MATLAB provides ‘fzero’ function which is used to find the roots of the nonlinear function of the single variable. It is very easy to use and has a lot of applications, especially in physics and calculus.  The ‘fzero’ command doesn’t really find a zero. It approximates the roots of the equation by finding a point where the objective function changes its sign. In order to understand how to use fzero in Matlab? Let’s see its syntax.

## ‘Fzero’ Functions along with their syntax

The fzero command returns a point based on the nature of a function i.e., continuous, or discontinuous. The general syntax of the fzero command is given as follows:

``var = fzero(funct, x0)``

Here, ‘funct’ represents the input function to be solved. It can be a name of the function or can also be specified as a function handle which provides a means of calling a function indirectly i.e., fhandle = @functionname. Whereas x0 is the point of your guess. It can be a real scalar number or a real two-element vector. This command tries to find a zero of ‘funct’ near x0. Here, zero represents the point where the function crosses the x-axis. Therefore, the above command will find the point, near the x0, where it crosses the x-axis. If the search fails, it will return NaN.

###### Other syntaxes:
``````var = fzero(funct,x0,options)

var = fzero(problem)

[var,fval,exitflag,output] = fzero(___)``````

## How to guess x0 in fzero in matlab

The selection of X0 is very important. As mentioned earlier, X0 can be either a single scalar value or a real two-element vector. If it is a real scalar number, then fzero starts at “x0” point and decreases the interval of iteration until it reaches a point where the ‘funct’ is equal to zero or funct changes its sign. Similarly, if the x0 is specified as a real vector consisting of two elements, then ‘fzero’ command will first check the value of ‘funct’ on these two points.

If the results obtained on these two values have opposite signs, then it decreases the level of iteration to reach the approximate solution. On the other hand, if the ‘funct’ value on both the points has the same sign then that means there is no such point in the interval where the sign of objective function changes and the command will give an error. This command usually behaves faster when given a vector as an input instead of a scalar.

Whatever you choose, the answer must be close to the true root. If the initial guess is too far from the root, then the fzero will diverge and give an error. The next question that comes to your mind is what should be a reasonable value for X0? There’s no set answer for this. Sometimes, you have to deal with larger complicated functions where the function crosses the x-axis at multiple points. In such cases, it’s difficult to come up with a reasonable X0 point. In such cases, you can estimate your guess by visualizing the plot of the function. Visually inspecting the plot of function will minimize the chances of fzero diverging and give an error.

Let’s take a look at an example for a better understanding.

##### example 1:

Let’s say, we have a function y = x^(3). If we look closely at its graph, we see that it is an intersection x-axis at zero. So, we should expect the same output from fzero Matlab functions. Let’s say our guess is also zero.

``````f = @(x) x.^3
zero = fzero(f, 0)``````

Output:

Our output. is the same as expected which means our results are correct.

Example 2:

Let’s say, we have our function y = x^(3) + 3 . Although we know from the graph that it’s crossing x-axis at somewhere close to -1.5 but let’s choose our guess as zero.

``````f = @(x) x.^(3) + 3
zero = fzero(f, 0)``````

Output:

So, we can see clearly that our guess was correct and it’s touching the x-axis at -1.4422.

## Limitation of ‘fzero’ command matlab

The fzero command approximates the root value by first finding an interval on which the function changes sign and then decreases the interval of iteration to get close to this x-value. But there are some functions that do not cross the cross x-axis instead only touch it. Such points where function only touches the x-axis but doesn’t cross it are not valid zeros. For example, in figure 1(b), the graph of the parabola shown touches the x-axis at 0 but never crosses it. For such functions, the interval won’t be found and the fzero commands execute until Inf, NaN, or a complex value is returned.

Hope we have answered your query “How to use Fzero in Matlab”. For any further queries related to fzero function Matlab, feel free to let us Know. Leave your valuable feedback below in the comments. Thanks.

Automated page speed optimizations for fast site performance