Upload
mihir-ashar
View
216
Download
0
Embed Size (px)
DESCRIPTION
algebra
Citation preview
Using fzero and Applications
Azar Shakoori
UOIT
MATH 2070U
Using fzero and Applications
Using MATLABs fzero
Solving nonlinear equations with parameters
Other solvers
fzero
I MATLAB has practical zero-finding routine fzeroI Can specify initial interval I(0) = [a(0), b(0)] bracketing zero
f = @(x) x-cos(x) % Function to find zeros
I0 = [0.5, 1.0]; % Initial interval
x = fzero( f, I0 )
I Alternatively, fzero accepts a single value x(0) as initialiteratef = @(x) x-cos(x) % Function to find zeros
x0 = 1.0; % Initial iterate
x = fzero( f, x0 )
fzerogui: Illustration of algorithm for fzero
I Graphical interface to fzeroI Can select iterates with mouseI Iterates generated from either
I bisection methodI secant methodI inverse quadratic interpolation
(IQI)I Some examples to try:
I x3 2x 5 on [0, 3]I ln(x+ 2/3) on [0, 1]
Syntax for fzero
x = fzero(fun,x0)
x = fzero(fun,x0,options)
[x,fval] = fzero(...)
[x,fval,exitflag] = fzero(...)
[x,fval,exitflag,output] = fzero(...)
I options is a structure whose fields can specify, displaypreferences, tolerances, maximum number of iterations,etc.
I fzero is a function function: expects function handle funas input
fzero options
I Options may be passed to fzero as a third input argumentI The options are a data structure created by the optimset
commandI options = optimset(par1, val1, par2, val2,)
I parn is the name of the parameter to be setI valn is the value to which to set that parameterI The parameters commonly used with fzero are:I display: when set to iter displays a detailed record of all
the iterationsI tolx: A positive scalar that sets a termination tolerance on
x.
Class activity
Find (any) solutions of the following equation with fzero:
3+ 6t2 = 4t+ 8 (1)
Strategy for equation (1)
I Using an anonymous function in MATLAB define yournonlinear function f
I Use fplot to plot f from t = 2 to t = 6I Use the information from the plot to find out how many
roots are there and in what neighborhood they are located.I Use fzero to find the roots.I Compute the residuals to verify the zeros are correct.
Class activity contd
Find (any) solutions of the following equation with fzero:
x3ex +32= 0 (2)
Strategy for equation (2)
I Again, using anonymous function in MATLAB define yournonlinear function g
I Generate a plot from t = 2 to t = 0. The plot revealsinformation about the roots.
I Use the information from the plot and fzero to find theroots.
Class activity contd
The following MATLAB transcript is used to find the threesmallest positive solutions of the nonlinear equation
x = cot x for = 1.
What is wrong with our solutions?
Nonlinear functions with parameters
I Parametrised families of nonlinear equations are common
e.g., sin x+ ex2/2 = 0 where , R
I Possible approaches to solve parametrised equations1. use extra parameters as global variables in function m-files2. define functions to accept extra parameters & use solvers
that accept variable length lists of inputs (c.f. bisection.m,newton)
3. use anonymous functions to encapsulate functions withfixed parameter values
2 1.5 1 0.5 0 0.5 1 1.5 21
0.5
0
0.5
1
1.5
2
2.5y = sin x+ex
2/2, = 1, = 2
x
y
1 0.5 0 0.5 11.5
1
0.5
0
0.5
1
1.5y = sin x+ex
2/2, = 3, = 1
x
y
Example: using global variablesSolve a sin x+ bex2/2 = 0 when a = 1, b = 2 & when a = 3,b = 1
Example: using functions with extra parameters
Example: using anonymous functionsSolve a sin x+ bex2/2 = 0 when a = 1, b = 2 & when a = 3,b = 1
Other solvers
ROOTS
I Polynomial equations: use roots in MATLABI Based on eigenvalue solver, companion matrixI Requires MATLAB convention for polynomials:
Example
>> p = [1, -2, 0, 3, -5]; % p= x^4-2x^3+3x-5
>> roots(p)
>> ans =
-1.3934
1.8984
0.7475 + 1.1539i
0.7475 - 1.1539i
Using Matlab's fzero Solving nonlinear equations with parametersOther solvers