fzero

Root of nonlinear function

Syntax

  • [x,fval,exitflag,output]= fzero(___)
    example

Description

example

x = fzero(fun,x0)
triesto find a point x where fun(x) = 0. This solution is where
fun(x) changessign—fzero cannot find a root of a functionsuch as
x^2.

example

x = fzero(fun,x0,options)
uses options tomodify the solution process.

example

x = fzero(problem)
solvesa root-finding problem specified by problem.

example

[x,fval,exitflag,output]=
fzero(___)
returns
fun(x)
inthe fval output, exitflag encodingthe reason
fzero stopped, and an output structurecontaining information on the solution process.

Examples

collapse all

Root Starting From One Point

Calculate by finding the zero of the sine function near
3.

fun = @sin; % function
x0 = 3; % initial point
x = fzero(fun,x0)
x =

    3.1416

Root Starting From an Interval

Find the zero of cosine between 1 and 2.

fun = @cos; % function
x0 = [1 2]; % initial interval
x = fzero(fun,x0)
x =

    1.5708

Note that and
differ in sign.

Root of a Function Defined by a File

Find a zero of the function f(x) = x3 – 2x – 5.

First, write a file called f.m.

function y = f(x)
y = x.^3 - 2*x - 5;

Save f.m on your MATLAB® path.

Find the zero of f(x)near 2.

fun = @f; % function
x0 = 2; % initial point
z = fzero(fun,x0)
z =
2.0946

Since f(x) is a polynomial, you canfind the same real zero, and a complex conjugate pair of zeros, usingthe
roots command.

roots([1 0 -2 -5])
   ans =
2.0946
-1.0473 + 1.1359i
-1.0473 - 1.1359i

Root of Function with Extra Parameter

Find the root of a function that has an extra parameter.

myfun = @(x,c) cos(c*x);  % parameterized function
c = 2; % parameter
fun = @(x) myfun(x,c); % function of x alone
x = fzero(fun,0.1)
x =

    0.7854

Nondefault Options

Plot the solution process by setting some plot functions.

Define the function and initial point.

fun = @(x)sin(cosh(x));
x0 = 1;

Examine the solution process by setting options that include plot functions.

options = optimset('PlotFcns',{@optimplotx,@optimplotfval});

Run fzero including options.

x = fzero(fun,x0,options)
x =

    1.8115

Solve Problem Structure

Solve a problem that is defined by a problem structure.

Define a structure that encodes a root-finding problem.

problem.objective = @(x)sin(cosh(x));
problem.x0 = 1;
problem.solver = 'fzero'; % a required part of the structure
problem.options = optimset(@fzero); % default options

Solve the problem.

x = fzero(problem)
x =

    1.8115

More Information from Solution

Find the point where exp(-exp(-x)) = x, and display information about the solution process.

fun = @(x) exp(-exp(-x)) - x; % function
x0 = [0 1]; % initial interval
options = optimset('Display','iter'); % show iterations
[x fval exitflag output] = fzero(fun,x0,options)
 Func-count    x          f(x)             Procedure
2 1 -0.307799 initial
3 0.544459 0.0153522 interpolation
4 0.566101 0.00070708 interpolation
5 0.567143 -1.40255e-08 interpolation
6 0.567143 1.50013e-12 interpolation
7 0.567143 0 interpolation Zero found in the interval [0, 1] x = 0.5671 fval = 0 exitflag = 1 output = intervaliterations: 0
iterations: 5
funcCount: 7
algorithm: 'bisection, interpolation'
message: 'Zero found in the interval [0, 1]'

fval = 0 means fun(x) = 0, as desired.

Related Examples

Input Arguments

collapse all

fun — Function to solvefunction handle

Function to solve, specified as a handle to a scalar-valuedfunction. fun accepts a scalar
x andreturns a scalar fun(x).

fzero solves fun(x) = 0. To solve an equation
fun(x) = c(x)
,instead solve fun2(x) = fun(x) - c(x) = 0.

To include extra parameters in your function, see the example Root of Function with Extra Parameter andthe section Parameterizing Functions.

Example: @sin

Example: @myFunction

Example: @(x)(x-a)^5 - 3*x + a - 1

Data Types: function_handle

x0 — Initial valuescalar | 2-element vector

Initial value, specified as a real scalar or a 2-element realvector.

  • Scalar — fzero begins at x0 andtries to locate a point
    x1 where fun(x1) hasthe opposite sign of fun(x0). Then
    fzero iterativelyshrinks the interval where fun changes sign toreach a solution.

  • 2-element vector — fzero checksthat fun(x0(1)) and
    fun(x0(2))
    haveopposite signs, and errors if they do not. It then iteratively shrinksthe interval where
    fun changes sign to reach asolution. An interval x0 must be finite; it cannotcontain ±Inf.

Tip  Calling fzero with an interval (x0 withtwo elements) is often faster than calling it with a scalar
x0.

Example: 3

Example: [2,17]

Data Types: double

options — Options for solution processstructure, typically
created using optimset

Options for solution process, specified as a structure. Createor modify the
options
structure using optimset. fzero usesthese options structure fields.

Display

Level of display:

  • 'off' displays no output.

  • 'iter' displays output at eachiteration.

  • 'final' displays just the finaloutput.

  • 'notify' (default) displays outputonly if the function does not converge.

FunValCheck

Check whether objective functionvalues are valid.

  • 'on' displays an error when theobjective function returns a value that is
    complex, Inf,or NaN.

  • The default, 'off', displays noerror.

OutputFcn

Specify one or more user-definedfunctions that an optimization function calls at each iteration, eitheras a function handle or as a cell array of function handles. The defaultis none ([]). See
Output Functions.

PlotFcns

Plot various measures of progresswhile the algorithm executes. Select from predefined plots or writeyour own. Pass a function handle or a cell array of function handles.The default is none ([]).

  • @optimplotx plots the current point.

  • @optimplotfval plots the functionvalue.

For information on writing a custom plot function,see Plot Functions.

TolX

Termination tolerance on x,a positive scalar. The default is
eps
, 2.2204e–16.

Example: options = optimset('FunValCheck','on')

Data Types: struct

problem — Root-finding problemstructure

Root-finding problem, specified as a structure with all of thefollowing fields.

objective

Objective function

x0

Initial point for x,real scalar or 2-element vector

solver

'fzero'

options

Options structure, typically createdusing optimset

For an example, see Solve Problem Structure.

Data Types: struct

Output Arguments

collapse all

x — Location of root or sign changereal scalar

Location of root or sign change, returned as a scalar.

fval — Function value at
x
real scalar

Function value at x, returned as a scalar.

exitflag — Integer encoding the exit conditioninteger

Integer encoding the exit condition, meaning the reason fsolve stoppedits iterations.

1

Function converged to a solution x.

-1

Algorithm was terminated by the output function or plotfunction.

-3

NaN or Inf functionvalue was encountered while searching for an interval containing asign change.

-4

Complex function value was encountered while searchingfor an interval containing a sign change.

-5

Algorithm might have converged to a singular point.

-6

fzero did not detect a sign change.

output — Information about root-finding processstructure

Information about root-finding process, returned as a structure.The fields of the structure are:

intervaliterations

Number of iterations taken to find an interval containinga root

iterations

Number of zero-finding iterations

funcCount

Number of function evaluations

algorithm

'bisection, interpolation'

message

Exit message

More About

collapse all

Algorithms

The fzero commandis a function file. The algorithm, created by T. Dekker,uses a combination of bisection, secant, and inverse quadratic interpolationmethods. An Algol 60 version,
with some improvements, is given in [1]. A Fortran version, upon which fzero isbased, is in [2].

References

[1] Brent, R., Algorithms forMinimization Without Derivatives, Prentice-Hall, 1973.

[2] Forsythe, G. E., M. A. Malcolm, and C.B. Moler, Computer Methods for Mathematical Computations,Prentice-Hall, 1976.

Fzreo matlab的更多相关文章

  1. Matlab 绘制三维立体图(以地质异常体为例)

    前言:在地球物理勘探,流体空间分布等多种场景中,定位空间点P(x,y,x)的物理属性值Q,并绘制三维空间分布图,对我们洞察空间场景有十分重要的意义. 1. 三维立体图的基本要件: 全空间网格化 网格节 ...

  2. Matlab slice方法和包络法绘制三维立体图

    前言:在地球物理勘探,流体空间分布等多种场景中,定位空间点P(x,y,x)的物理属性值Q,并绘制三维空间分布图,对我们洞察空间场景有十分重要的意义. 1. 三维立体图的基本要件: 全空间网格化 网格节 ...

  3. Matlab 高斯_拉普拉斯滤波器处理医学图像

    前言:本程序是我去年实现论文算法时所做.主要功能为标记切割肝脏区域.时间有点久,很多细节已经模糊加上代码做了很多注释,因此在博客中不再详述. NOTE: 程序分几大段功能模块,仔细阅读,对解决医学图像 ...

  4. MATLAB中绘制质点轨迹动图并保存成GIF

    工作需要在MATLAB中绘制质点轨迹并保存成GIF以便展示. 绘制质点轨迹动图可用comet和comet3命令,使用例子如下: t = 0:.01:2*pi;x = cos(2*t).*(cos(t) ...

  5. linux下配置matlab运行环境(MCR)

    在安装好的matlab下有MCR(MatlabCompilerRuntime)在matlab2011/toolbox/compiler/deploy/glnxa64下找到MCRInstaller.zi ...

  6. EMD分析 Matlab 精华总结 附开源工具箱(全)

    前言: 本贴写于2016年12与15日,UK.最近在学习EMD(Empirical Mode Decomposition)和HHT(Hilbert-Huang Transform)多分辨信号处理,FQ ...

  7. Atitit MATLAB 图像处理 经典书籍attilax总结

    Atitit MATLAB 图像处理 经典书籍attilax总结 1.1. MATLAB数字图像处理1 1.2. <MATLAB实用教程(第二版)>((美)穆尔 著)[简介_书评_在线阅读 ...

  8. Atitit MATLAB 图像处理attilax总结

    Atitit MATLAB 图像处理attilax总结 1.1. 下载 Matlab7.0官方下载_Matlab2012 v7.0 官方简体中文版-办公软件-系统大全.html1 1.2. Matla ...

  9. Atitit java c# php c++ js跨语言调用matlab实现边缘检测等功能attilax总结

    Atitit java c# php c++ js跨语言调用matlab实现边缘检测等功能attilax总结 1.1. 边缘检测的基本方法Canny最常用了1 1.2. 编写matlab边缘检测代码, ...

随机推荐

  1. Openvswitch手册(2): OpenFlow Controller

         我们这一节主要来看Controller Controller有两种: Primary Controller: 真正控制vswitch的flow table,vswitch会保持和contro ...

  2. Photon自定义加载Resource之外的资源

    PhotonNetwork.cs 结尾添加如下代码: #region >>> Photon自定义异步加载GameObject public delegate void CustomL ...

  3. BCrypt实现密码的加密

    这里设计到一个新的知识点,下来准备找找资料学习一下:Spring Security 我们都知道,密码这种东西存到数据库是不能以明文直接存入的,而是要经过加密,而且加密还颇多讲究 比如以前的 MD5加密 ...

  4. 一篇入门 -- Scala 反射

    本篇文章主要让大家理解什么是Scala的反射, 以及反射的分类, 反射的一些术语概念和一些简单的反射例子. 什么是反射 我们知道, Scala是基于JVM的语言, Scala编译器会将Scala代码编 ...

  5. 1.ActionBar

    ActionBar低版本和高版本用法不同 低版本:1. 引用v7-appcompat2. Activity继承ActionBarActivity3. android:theme="@styl ...

  6. select count(*) 底层究竟做了什么?

    阅读本文大概需要 6.6 分钟. SELECT COUNT( * ) FROM t是个再常见不过的 SQL 需求了.在 MySQL 的使用规范中,我们一般使用事务引擎 InnoDB 作为(一般业务)表 ...

  7. 每天学点SpringCloud(十二):Zipkin全链路监控

    Zipkin是SpringCloud官方推荐的一款分布式链路监控的组件,使用它我们可以得知每一个请求所经过的节点以及耗时等信息,并且它对代码无任何侵入,我们先来看一下Zipkin给我们提供的UI界面都 ...

  8. [Vuejs] svg-sprite-loader实现加载svg自定义组件

    1.安装 svg-sprite-loader npm install svg-sprite-loader -D 或者 npm install svg-sprite-loader --save-dev ...

  9. 一些能体现个人水平的SQL语句[总结篇]

    作为一名小小的开发人员,刚入门的时候觉得很难,过了一段时间之后,发现很简单,很快就可以搞定很bug了.然而这并不能说明你就已经很牛掰了,只能说,你不了解其他太多的东西.应该说,数据库有几个共同的命令, ...

  10. python中 __init__.py的例程

    __init__.py一般是为空,用在一个python目录中,标识该目录是一个python的模块包 先上来看一个例子: .: test1 test2 test_init.py ./test1: tim ...