%
% M451_9_12_12 diary
%
% HW due ? Driscoll Probs. 4.2, 4.3, 4.4, 4.5 (bonus)
%
% Today some remarks from 4.1 and 4.2
%
help funfun
Function functions and ODE solvers.
Optimization and root finding.
fminbnd - Scalar bounded nonlinear function minimization.
fminsearch - Multidimensional unconstrained nonlinear minimization,
by Nelder-Mead direct search method.
fzero - Scalar nonlinear zero finding.
Optimization Option handling
optimset - Create or alter optimization OPTIONS structure.
optimget - Get optimization parameters from OPTIONS structure.
Numerical integration (quadrature).
quad - Numerically evaluate integral, low order method.
quadgk - Numerically evaluate integral, adaptive Gauss-Kronrod quadrature.
quadl - Numerically evaluate integral, higher order method.
quadv - Vectorized QUAD.
quad2d - Numerically evaluate double integral over a planar region.
dblquad - Numerically evaluate double integral over a rectangle.
triplequad - Numerically evaluate triple integral.
Plotting.
ezplot - Easy to use function plotter.
ezplot3 - Easy to use 3-D parametric curve plotter.
ezpolar - Easy to use polar coordinate plotter.
ezcontour - Easy to use contour plotter.
ezcontourf - Easy to use filled contour plotter.
ezmesh - Easy to use 3-D mesh plotter.
ezmeshc - Easy to use combination mesh/contour plotter.
ezsurf - Easy to use 3-D colored surface plotter.
ezsurfc - Easy to use combination surf/contour plotter.
fplot - Plot function.
Inline function object.
inline - Construct INLINE function object.
argnames - Argument names.
formula - Function formula.
char - Convert INLINE object to character array.
Differential equation solvers.
Initial value problem solvers for ODEs. (If unsure about stiffness, try ODE45
first, then ODE15S.)
ode45 - Solve non-stiff differential equations, medium order method.
ode23 - Solve non-stiff differential equations, low order method.
ode113 - Solve non-stiff differential equations, variable order method.
ode23t - Solve moderately stiff ODEs and DAEs Index 1, trapezoidal rule.
ode15s - Solve stiff ODEs and DAEs Index 1, variable order method.
ode23s - Solve stiff differential equations, low order method.
ode23tb - Solve stiff differential equations, low order method.
Initial value problem solver for fully implicit ODEs/DAEs F(t,y,y')=0.
decic - Compute consistent initial conditions.
ode15i - Solve implicit ODEs or DAEs Index 1.
Initial value problem solver for delay differential equations (DDEs).
dde23 - Solve delay differential equations (DDEs) with constant delays.
ddesd - Solve delay differential equations (DDEs) with variable delays.
Boundary value problem solver for ODEs.
bvp4c - Solve boundary value problems by collocation, 3-stage Lobatto formula.
bvp5c - Solve boundary value problems by collocation, 4-stage Lobatto formula.
1D Partial differential equation solver.
pdepe - Solve initial-boundary value problems for parabolic-elliptic PDEs.
Option handling.
odeset - Create/alter ODE OPTIONS structure.
odeget - Get ODE OPTIONS parameters.
ddeset - Create/alter DDE OPTIONS structure.
ddeget - Get DDE OPTIONS parameters.
bvpset - Create/alter BVP OPTIONS structure.
bvpget - Get BVP OPTIONS parameters.
Input and Output functions.
deval - Evaluates the solution of a differential equation problem.
odextend - Extends the solutions of a differential equation problem.
odeplot - Time series ODE output function.
odephas2 - 2-D phase plane ODE output function.
odephas3 - 3-D phase plane ODE output function.
odeprint - Command window printing ODE output function.
bvpinit - Forms the initial guess for BVP4C.
bvpxtend - Forms a guess structure for extending BVP solution.
pdeval - Evaluates by interpolation the solution computed by PDEPE.
odefile - MATLAB v5 ODE file syntax (obsolete).
home
format long
help fzero
FZERO Single-variable nonlinear zero finding.
X = FZERO(FUN,X0) tries to find a zero of the function FUN near X0,
if X0 is a scalar. It first finds an interval containing X0 where the
function values of the interval endpoints differ in sign, then searches
that interval for a zero. FUN is a function handle. FUN accepts real
scalar input X and returns a real scalar function value F, evaluated
at X. The value X returned by FZERO is near a point where FUN changes
sign (if FUN is continuous), or NaN if the search fails.
X = FZERO(FUN,X0), where X0 is a vector of length 2, assumes X0 is a
finite interval where the sign of FUN(X0(1)) differs from the sign of
FUN(X0(2)). An error occurs if this is not true. Calling FZERO with a
finite interval guarantees FZERO will return a value near a point where
FUN changes sign.
X = FZERO(FUN,X0), where X0 is a scalar value, uses X0 as a starting
guess. FZERO looks for an interval containing a sign change for FUN and
containing X0. If no such interval is found, NaN is returned.
In this case, the search terminates when the search interval
is expanded until an Inf, NaN, or complex value is found. Note: if
the option FunValCheck is 'on', then an error will occur if an NaN or
complex value is found.
X = FZERO(FUN,X0,OPTIONS) solves the equation with the default optimization
parameters replaced by values in the structure OPTIONS, an argument
created with the OPTIMSET function. See OPTIMSET for details. Used
options are Display, TolX, FunValCheck, OutputFcn, and PlotFcns.
X = FZERO(PROBLEM) finds the zero of a function defined in PROBLEM.
PROBLEM is a structure with the function FUN in PROBLEM.objective,
the start point in PROBLEM.x0, the options structure in PROBLEM.options,
and solver name 'fzero' in PROBLEM.solver. The structure PROBLEM must have
all the fields.
[X,FVAL]= FZERO(FUN,...) returns the value of the function described
in FUN, at X.
[X,FVAL,EXITFLAG] = FZERO(...) returns an EXITFLAG that describes the
exit condition of FZERO. Possible values of EXITFLAG and the corresponding
exit conditions are
1 FZERO found a zero X.
-1 Algorithm terminated by output function.
-3 NaN or Inf function value encountered during search for an interval
containing a sign change.
-4 Complex function value encountered during search for an interval
containing a sign change.
-5 FZERO may have converged to a singular point.
-6 FZERO can not detect a change in sign of the function.
[X,FVAL,EXITFLAG,OUTPUT] = FZERO(...) returns a structure OUTPUT
with the number of function evaluations in OUTPUT.funcCount, the
algorithm name in OUTPUT.algorithm, the number of iterations to
find an interval (if needed) in OUTPUT.intervaliterations, the
number of zero-finding iterations in OUTPUT.iterations, and the
exit message in OUTPUT.message.
Examples
FUN can be specified using @:
X = fzero(@sin,3)
returns pi.
X = fzero(@sin,3,optimset('Display','iter'))
returns pi, uses the default tolerance and displays iteration information.
FUN can also be an anonymous function:
X = fzero(@(x) sin(3*x),2)
If FUN is parameterized, you can use anonymous functions to capture the
problem-dependent parameters. Suppose you want to solve the equation given
in the function myfun, which is parameterized by its second argument c. Here
myfun is an M-file function such as
function f = myfun(x,c)
f = cos(c*x);
To solve the equation for a specific value of c, first assign the value to c.
Then create a one-argument anonymous function that captures that value of c
and calls myfun with two arguments. Finally, pass this anonymous function to
FZERO:
c = 2; % define parameter first
x = fzero(@(x) myfun(x,c),0.1)
Limitations
X = fzero(@(x) abs(x)+1, 1)
returns NaN since this function does not change sign anywhere on the
real axis (and does not have a zero as well).
X = fzero(@tan,2)
returns X near 1.5708 because the discontinuity of this function near the
point X gives the appearance (numerically) that the function changes sign at X.
See also roots, fminbnd, function_handle.
Reference page in Help browser
doc fzero
fzero(sin,3)
{??? Error using ==> sin
Not enough input arguments.
}
fzero(@sin,3)
ans =
3.141592653589793
pi
ans =
3.141592653589793
fzero(@sin,6)
ans =
6.283185307179586
2*pi
ans =
6.283185307179586
fzero(@sin,1)
ans =
1.548489780738154e-24
quad(@(x) x^2+1,0,1)
{??? Error using ==> mpower
Inputs must be a scalar and a square matrix.
Error in ==> @(x)x^2+1
Error in ==> quad at 77
y = f(x, varargin{:});
}
quad(@(x)x^2+1,0,1)
{??? Error using ==> mpower
Inputs must be a scalar and a square matrix.
Error in ==> @(x)x^2+1
Error in ==> quad at 77
y = f(x, varargin{:});
}
quad(@(x)x.^2+1,0,1)
ans =
1.333333333333333
% recall simple picard iteration for solving x = cos(x):
x=0
x =
0
x=cos(x)
x =
1
x=cos(x)
x =
0.540302305868140
x=cos(x)
x =
0.857553215846393
x=cos(x)
x =
0.654289790497779
x=cos(x)
x =
0.793480358742566
x=cos(x)
x =
0.701368773622757
x=cos(x)
x =
0.763959682900654
x=cos(x)
x =
0.722102425026708
x=cos(x)
x =
0.750417761763761
x=cos(x)
x =
0.731404042422510
x=cos(x)
x =
0.744237354900557
% Instead see function picard.m on my webpage.
% This is a simple example of a function with
% a subfunction call:
picard(0,30)
ans =
0.739082298522402
picard(0,40)
ans =
0.739085078689123
% see script picard_iter.m on my webpage
clear
picard_iter
{??? Error: File: picard_iter.m Line: 9
Column: 1
Function definitions are not permitted in
this context.
}
%
% we can't call a function iside script file so revise:
picard_iter
x =
1
x =
0.540302305868140
x =
0.857553215846393
x =
0.654289790497779
x =
0.793480358742566
x =
0.701368773622757
x =
0.763959682900654
x =
0.722102425026708
x =
0.750417761763761
x =
0.731404042422510
% p. 38 >>f=@(x) bigfun(0.5,x,b_value);
% read material on nested functions
% for numerics of trap rule, Simpson, Newton etc.
% see Math 551 text by Moler online.
%
% Remarks on HW problems:
%
% Prob 4.2. The order of of a quadrature rule is the degree of the lowest degree
% polynomial that an rule will comput exactly, e.g. trapezoidal rule has order = 2
% and Simpson's rule has order = 4. You can check this with some examples.
%
% Prob 4.3 The bisection method works because a continuous function that
% changes sign on an interval [a,b] must have at least one root in the interval
% by the Intermediate Value Theorem. (The proof opf this Theorem is based on
% the completeness the real numbers.)
%
% Prob 4.4 The derivation of Newton's method for finding roots of a differentiable
% function y = f(x): Given a gues x_n near the root find the equation of the
% tangent line to y=f(x) at x=x_n, namely, y = f(x_n) + f'(x_n)*(x - x_n).
% Find x_(n+1) where the tangent line intersect the x-axis, i.e., set y = 0 and
% solve for x=x_(n+1) = x_n - f(x_n)/ f' (x_n). Repeat this until |f(x_n)|
% or |x_(n+1) - x_n| is sufficiently small.
%
%
%
%
% Start writing your trap coed for Prob 4.2
% Not, you can't set a = x(0):
%
x=0:4
x =
0 1 2 3 4
x(0)
{??? Subscript indices must either be real
positive integers or logicals.
}
diary off