Share
Sign In
3️⃣

Ch3. Finding a Roots - The fzero Function

Matlab Funcitions for Root Finding
MATLAB has two primary built-in functions for solving nonlinear equations. The fzero function solves a single nonlinear equation, and the fsolve function solves systems of nonlinear equations.
fzero uses a combination of root finding methods, including the bisection method. Like the bisection method, fzero needs two things to find a root.
1.
a function
2.
general location of the desired root
In the bisection method example, the function was coded inside the algorithm.
For fzero, the function must be passed as an input to fzero using a function handle.
In this chapter, you will learn to create anonymous function handles, pass the function handle and brackets to fzero to find a root, and adapt the process to find multiple roots. fzero
In the next chapter, you will use fsolve in a similar way to solve systems of nonlinear equations.
Finding the Roots of a Polynomial
The fzero function is a broadly applicable MATLAB root finding function. If your root finding problem is a polynomial, you may choose to use the roots function instead. More information on roots can be found in the documentation. roots
Anonymous Functions
An anonymous function is a way to define a simple function and store it as a variable in the workspace.
This syntax creates an anonymous function.
The variable myFun is a "function handle". It is a variable in the workspace that represents the function.
The @ operator follows the equals sign. It constructs the function handle.
The input variables (x) follow the @ operator and are listed in parentheses.
The function definition defines the function in terms of the input variables.
Anonymous functions are useful in root finding problems, and can also be used to evaluate and visualize functions.
This code produces a function handle f that contains the function f(x) =\sin(x)=x^2.
The function handle f can be called with inputs like a function.
Function handles can be passed as inputs to another function.
The fplot function plots f over the specified interval.
Task1 - Plot with Anonymous Functions
Create a function handle fun1 to an anonymous function for f_1(x)=0.5-\sin(x) .
Use the command fplot(fun1,[-1 1]) to plot fun1 from -1 to 1.
Create a function handle fun2 to an anonymous function for f_2(x)=x^2 . Use element-wise operators (.^) in your implementation.
Enter the command hold on so the next plot is added to the existing plot.
Use fplot to plot fun2 from -1 to 1.
You can change the x-axis limits to zoom in on the point of intersection of the two functions. xlim([.3 .4])
The fzero Function
Using the fzero Function
The fzero function takes "a function handle" and "an interval" as inputs, and returns exactly one root in the interval.
Create a function handle representing the function you want to solve.
Define an interval bracketing the desired root.
Pass the function handle and brackets to the fzero function.
The fzero function can also be called using an approximate location of the root, instead of an interval. In this case, fzero first attempts to find an interval bracketing the root, then proceeds as before.
Define an approximate location of the root, often called an initial guess.
Pass the function handle and initial guess to the fzero function.
Note that you should use fzero in this way only when you have a good initial estimate of the root, otherwise fzero may take a long time - or even fail - to find a bracket.
Task 1 - Find a Root with fzero
In this activity, you will use fzero to find where 0.5-\sin(x) = x^2 between -1 and 1.
1.
Create a function handle rootFunction to an anonymous function for f(x)=0.5-\sin(x)-x.
2.
Use fplot to plot rootFunction over the interval -1 to 1.
3.
Enter the command grid on to add grid lines to the plot.
4.
Like the bisection method, fzero requires an interval that brackets the desired root.
Recall that the interval must be a two element vector with values x_{neg} and x_{pos} where
x_{neg} is a value for which f(x_{neg}) < 0, and
x_{pos} is a value for which f(x_{pos}) > 0.
Create a two element vector x0 that defines an interval bracketing the root.
5.
The syntax for calling the fzero function is x = fzero(fhandle,brackets).
Use fzero to find the root of rootFunction in the interval x0. Assign the result to the variable x.
6.
Use rootFunction to evaluate f(x) = 0.5-\sin(x)-x^2 at the root you found. Assign the result to the variable y.
You may want to leave off the semicolon to see the result. (If x is near the root, f(x) should be close to 0.)
7.
Enter hold on so that the next plot is added to the existing plot.
8.
You can plot a point as a circle by using 'o' as a third input to the plot function.
plot(xValue, yValue,'o')
Add a circle to the plot marking the root's location.
Finding Multiple Roots
Task1 - Finding Multiple Roots with fzero
In this activity, you'll use fzero to find both roots of f(x) = 0.5-\sin(x)-x^2 within the interval x \in [-2, 1]. A partially completed script has been provided for you.
The function f(x) = 0.5-\sin(x)-x^2 is stored in the function handle rootfunction .
1.
Find the root near x = -1 by
defining an interval bracket1 that brackets the root, and
calling fzero to find the root.
Assign the result to the variable xRoot1.
2.
Define another interval bracket2 to bracket the root near x=0.5. Use fzero to find the root and assign the result to xRoot2.
Task2 - Project: Electricity Consumption
The variable time is given in years since 1991, and the variable usage contains the electricity usage data in 10^9kilowatt hours per day at each time in time.
It can be easier to visualize when usage reaches 10 by plotting usage - 10 versus time and looking for roots.
1.
Plot usage - 10 as a function of time.
2.
Enter hold on so that the next plot is added to the existing plot.
3.
Finding when the long term trend reaches 10\times10^9 kWh/day is equivalent to solving the equation -0.000537x^3 + 0.00926x^2 +0.1526x+7.418 =10
The function handle elecFun contains the root finding form of this equation.
Use fplot to plot eleFun in the interval [0 20] . Use'--' as the third input to plot the function as a dashed line.
4.
Enter grid on to add grid lines to the plot.
5.
There is a root of elecFun near x=15. Create a two element vector x0 for an interval bracketing this root.
6.
Use fzero to find the root near x=15. Assign the output to the variable x.
Task3 - Project: The n-th Root of a Number
The nth root of a number a can be found by solving the equation
xna=0x^n - a =0
Consider 3\sqrt{8} . In this example, n = 3 and a = 8.
1.
Write an anonymous function for f(x) = x^3-8. Name the function handle fun38.
2.
Plot the function fun38 over the interval [-5 5] using fplot.
3.
Enter grid on to add grid lines to the plot.
4.
The function has only one root. Define a two element vector x0 for an interval bracketing that root.
5.
Solve for 3\sqrt{8} using fzero. Assign the result to root38.
Matlab Funcitions for Root Finding
MATLAB has two primary built-in functions for solving nonlinear equations. The fzero function solves a single nonlinear equation, and the fsolve function solves systems of nonlinear equations.
fzero uses a combination of root finding methods, including the bisection method. Like the bisection method, fzero needs two things to find a root.
1.
a function
2.
general location of the desired root
In the bisection method example, the function was coded inside the algorithm.
For fzero, the function must be passed as an input to fzero using a function handle.
In this chapter, you will learn to create anonymous function handles, pass the function handle and brackets to fzero to find a root, and adapt the process to find multiple roots. fzero
In the next chapter, you will use fsolve in a similar way to solve systems of nonlinear equations.
Finding the Roots of a Polynomial
The fzero function is a broadly applicable MATLAB root finding function. If your root finding problem is a polynomial, you may choose to use the roots function instead. More information on roots can be found in the documentation. roots
Anonymous Functions
An anonymous function is a way to define a simple function and store it as a variable in the workspace.
This syntax creates an anonymous function.
The variable myFun is a "function handle". It is a variable in the workspace that represents the function.
The @ operator follows the equals sign. It constructs the function handle.
The input variables (x) follow the @ operator and are listed in parentheses.
The function definition defines the function in terms of the input variables.
Anonymous functions are useful in root finding problems, and can also be used to evaluate and visualize functions.
This code produces a function handle f that contains the function f(x) =\sin(x)=x^2.
The function handle f can be called with inputs like a function.
Function handles can be passed as inputs to another function.
The fplot function plots f over the specified interval.
Task1 - Plot with Anonymous Functions
Create a function handle fun1 to an anonymous function for f_1(x)=0.5-\sin(x) .
Use the command fplot(fun1,[-1 1]) to plot fun1 from -1 to 1.
Create a function handle fun2 to an anonymous function for f_2(x)=x^2 . Use element-wise operators (.^) in your implementation.
Enter the command hold on so the next plot is added to the existing plot.
Use fplot to plot fun2 from -1 to 1.
You can change the x-axis limits to zoom in on the point of intersection of the two functions. xlim([.3 .4])
The fzero Function
Using the fzero Function
The fzero function takes "a function handle" and "an interval" as inputs, and returns exactly one root in the interval.
Create a function handle representing the function you want to solve.
Define an interval bracketing the desired root.
Pass the function handle and brackets to the fzero function.
The fzero function can also be called using an approximate location of the root, instead of an interval. In this case, fzero first attempts to find an interval bracketing the root, then proceeds as before.
Define an approximate location of the root, often called an initial guess.
Pass the function handle and initial guess to the fzero function.
Note that you should use fzero in this way only when you have a good initial estimate of the root, otherwise fzero may take a long time - or even fail - to find a bracket.
Task 1 - Find a Root with fzero
In this activity, you will use fzero to find where 0.5-\sin(x) = x^2 between -1 and 1.
1.
Create a function handle rootFunction to an anonymous function for f(x)=0.5-\sin(x)-x.
2.
Use fplot to plot rootFunction over the interval -1 to 1.
3.
Enter the command grid on to add grid lines to the plot.
4.
Like the bisection method, fzero requires an interval that brackets the desired root.
Recall that the interval must be a two element vector with values x_{neg} and x_{pos} where
x_{neg} is a value for which f(x_{neg}) < 0, and
x_{pos} is a value for which f(x_{pos}) > 0.
Create a two element vector x0 that defines an interval bracketing the root.
5.
The syntax for calling the fzero function is x = fzero(fhandle,brackets).
Use fzero to find the root of rootFunction in the interval x0. Assign the result to the variable x.
6.
Use rootFunction to evaluate f(x) = 0.5-\sin(x)-x^2 at the root you found. Assign the result to the variable y.
You may want to leave off the semicolon to see the result. (If x is near the root, f(x) should be close to 0.)
7.
Enter hold on so that the next plot is added to the existing plot.
8.
You can plot a point as a circle by using 'o' as a third input to the plot function.
plot(xValue, yValue,'o')
Add a circle to the plot marking the root's location.
Finding Multiple Roots
Task1 - Finding Multiple Roots with fzero
In this activity, you'll use fzero to find both roots of f(x) = 0.5-\sin(x)-x^2 within the interval x \in [-2, 1]. A partially completed script has been provided for you.
The function f(x) = 0.5-\sin(x)-x^2 is stored in the function handle rootfunction .
1.
Find the root near x = -1 by
defining an interval bracket1 that brackets the root, and
calling fzero to find the root.
Assign the result to the variable xRoot1.
2.
Define another interval bracket2 to bracket the root near x=0.5. Use fzero to find the root and assign the result to xRoot2.
Task2 - Project: Electricity Consumption
The variable time is given in years since 1991, and the variable usage contains the electricity usage data in 10^9kilowatt hours per day at each time in time.
It can be easier to visualize when usage reaches 10 by plotting usage - 10 versus time and looking for roots.
1.
Plot usage - 10 as a function of time.
2.
Enter hold on so that the next plot is added to the existing plot.
3.
Finding when the long term trend reaches 10\times10^9 kWh/day is equivalent to solving the equation -0.000537x^3 + 0.00926x^2 +0.1526x+7.418 =10
The function handle elecFun contains the root finding form of this equation.
Use fplot to plot eleFun in the interval [0 20] . Use'--' as the third input to plot the function as a dashed line.
4.
Enter grid on to add grid lines to the plot.
5.
There is a root of elecFun near x=15. Create a two element vector x0 for an interval bracketing this root.
6.
Use fzero to find the root near x=15. Assign the output to the variable x.
Task3 - Project: The n-th Root of a Number
The nth root of a number a can be found by solving the equation
xna=0x^n - a =0
Consider 3\sqrt{8} . In this example, n = 3 and a = 8.
1.
Write an anonymous function for f(x) = x^3-8. Name the function handle fun38.
2.
Plot the function fun38 over the interval [-5 5] using fplot.
3.
Enter grid on to add grid lines to the plot.
4.
The function has only one root. Define a two element vector x0 for an interval bracketing that root.
5.
Solve for 3\sqrt{8} using fzero. Assign the result to root38.
Matlab Funcitions for Root Finding
MATLAB has two primary built-in functions for solving nonlinear equations. The fzero function solves a single nonlinear equation, and the fsolve function solves systems of nonlinear equations.
fzero uses a combination of root finding methods, including the bisection method. Like the bisection method, fzero needs two things to find a root.
1.
a function
2.
general location of the desired root
In the bisection method example, the function was coded inside the algorithm.
For fzero, the function must be passed as an input to fzero using a function handle.
In this chapter, you will learn to create anonymous function handles, pass the function handle and brackets to fzero to find a root, and adapt the process to find multiple roots. fzero
In the next chapter, you will use fsolve in a similar way to solve systems of nonlinear equations.
Finding the Roots of a Polynomial
The fzero function is a broadly applicable MATLAB root finding function. If your root finding problem is a polynomial, you may choose to use the roots function instead. More information on roots can be found in the documentation. roots
Anonymous Functions
An anonymous function is a way to define a simple function and store it as a variable in the workspace.
This syntax creates an anonymous function.
The variable myFun is a "function handle". It is a variable in the workspace that represents the function.
The @ operator follows the equals sign. It constructs the function handle.
The input variables (x) follow the @ operator and are listed in parentheses.