Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.
-
Upload
buddy-higgins -
Category
Documents
-
view
215 -
download
0
Transcript of Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.
![Page 1: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/1.jpg)
Root Finding
The solution of nonlinear equations and systems
Vageli Coutsias, UNM, Fall ‘02
![Page 2: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/2.jpg)
0x1x
0xf 0' xf 0001 '/ xfxfxx
The Newton-Raphson iteration for locating zeros
![Page 3: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/3.jpg)
Example: finding the square root
xxf
axxf
2'
2
000
0
20
01 2
1
2 x
axx
x
axxx
Details: initial iterate must be ‘close’ to solution for method to deliver its promise of quadratic convergence (the number of correct bits doubling with every step)
![Page 4: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/4.jpg)
Square root by Newton-Raphson
% find square root of A x = (1+2*A)/3; maxiter = 20;for k = 1:maxiter x = (x+ (A/x))/2; end
![Page 5: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/5.jpg)
function x = sqrt1(A)if A < 0 break;endif A == 0; x = 0;else TwoPower = 1; m = A; while m >= 1, m = m/4; TwoPower = 2*TwoPower ;end while m < .25, m = m*4; TwoPower = TwoPower/2;end x = (1+2*m)/3; for k = 1:4 x = (x+ (m/x))/2; end x = x*TwoPower;end
![Page 6: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/6.jpg)
function z = newton(func,z0,tol,maxiter,varargin) hopt = 2*sqrt(eps); z = z0; value = feval(func,z,varargin{:}); iter = 1;while abs(value) >=tol deriv = (feval(func,z+hopt,varargin{:}) - val)/hopt; z = z - value/deriv; value = feval(func,z,varargin{:}); iter = iter+1; if iter >= maxiter fprintf('maxiter exceeded, no convergence') break endend
![Page 7: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/7.jpg)
function [z,varargout] = ... newton(func,z0,tol,maxiter,varargin) hopt = 2*sqrt(eps); z = z0; iter = 1 value = feval(func,z,varargin{:});while abs(value) >=tol if iter >= maxiter fprintf('maxiter exceeded, no convergence'); break end deriv = (feval(func,z+hopt,varargin{:}) - val)/hopt; z = z - value/deriv; value = feval(func,z, varargin{:}); iter = iter+1;endif nargout >= 2 ; varargout{1} = val; endif nargout >= 3 ; varargout{2} = iter; end
![Page 8: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/8.jpg)
z = -0.30958376444732val = 4.462736e-006iter = 4
function zerofinder()clear; close alla=10; b = 3; x = linspace(-20,20,100); y = func(x,a,b); zz =zeros(100); plot(x,y,x,zz)[z,val,iter] = newton(@func,1,.0001,40,a,b) function z = func(y,a,b)z=y.^2+a*y+b;
![Page 9: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/9.jpg)
clc; close all; clear all; format long e; fname = 'func'; a = input('Enter a value:'); b = input('Enter b value:');xc = input('Enter starting value:'); xmax = xc; xmin = xc;fc = feval(fname,xc,a,b); delta = .0001;fpc = (feval(fname,xc+delta,a,b)-fc)/delta;k=0; disp(sprintf('k x fval fpval '))
while input('Newton step? (0=no, 1=yes)') k=k+1; x(k) = xc; y(k) = fc; xnew = xc – fc / fpc; xc = xnew; fc = feval(fname,xc,a,b); fpc = (feval(fname,xc+delta,a,b)-fc)/delta;disp(sprintf('%2.0f %20.15f %20.15f %20.15f',k,xc,fc,fpc)) if xmax <= x(k); xmax = x(k); end if xmin >= x(k); xmin = x(k); endendx0 = linspace(xmin,xmax,201); y0 = feval(fname,x0,a,b); plot(x0,y0,'r-',x,y,'*')
![Page 10: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/10.jpg)
Enter a value:.1Enter b value:.5Enter starting value:0k x fval
Newton step? (0=no, 1=yes)11 -0.454545455922915 0.028917256044793
2 -0.486015721951951 0.000349982958035 3 -0.486406070359515 0.0000000687753154 -0.486406147091071 0.000000000002760
5 -0.486406147094150 0.000000000000000 Newton step? (0=no, 1=yes)0
![Page 11: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/11.jpg)
function f = func(x,a,b) % trap near x = 0% for interesting behavior, use a = .1, b = .0001, x0 = .4 f = .5*x.^2+ 100*x.^8 - a*x.^16 + b;
![Page 12: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/12.jpg)
![Page 13: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/13.jpg)
% script zeroin% uses matlab builtin rootfinder "FZERO"% to find a zero of a function 'fname'close all; clear all; format long%fname = 'func'; % user defined function-can also give as @func%fpname='dfunc'; % user def. derivative-not required by FZEROdel = .0001; % function value limiting tolerance
a = input('Enter a value:'); b = input('Enter b value:');x0 = linspace(-10,10,201); y0 = feval(@func,x0,a,b);xc = input('Enter starting value:');%root = fzero(@func,xc,.0001,1,a,b); % grandfathered formatOPTIONS=optimset('MaxIter',100,'TolFun',del,'TolX',del^2);root = fzero(@func,xc,OPTIONS,a,b)y = feval(@func,root,a,b)plot(x0,y0,root,y,'r*')
![Page 14: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/14.jpg)
Planar 4-R linkage
4a
1a
2a
3a
M
A B
CD
x
y
![Page 15: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/15.jpg)
coscos2cos2
sinsincoscos
sinsincoscos2
1
sincos
sincos
0
4214224
22
21
23
242
2421
23
42421
44
221
1
aaaaaaaaa
aaaaaa
aaaaaADACDC
ADACAM
aaAD
aaaAC
aAB
The planar 4-bar linkage can assume various configurations. For each value of the angle there are two possible values of and vice versa. The mid-point of the bar CD executes a complex motion.
![Page 16: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/16.jpg)
function linkage()close all; a1=10;a2=5;a3=7;a4=2; psi0=30; A=[0,0]; B=[a1,0];axis equalfor i = 1:60 phi(i)=i*pi/30; psi(i) = fzero(@bar4,psi0,[],phi(i),a1,a2,a3,a4); C=[a1+a2*cos(psi(i)),a2*sin(psi(i))]; D= [a4*cos(phi(i)),a4*sin(phi(i))]; XX=[A,B,C,D,A]; plot(XX(:,1),XX(:,2),'k-') hold on x = (a1+a2*cos(psi)+a4*cos(phi))/2; y = (a2*sin(psi)+a4*sin(phi))/2; plot(x,y,'ro'); pause(.1) plot(XX(:,1),XX(:,2), 'y-')endplot(XX(:,1),XX(:,2),'k-')
![Page 17: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/17.jpg)
3241 aaaa
![Page 18: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/18.jpg)
3241 aaaa
MA
D traces full circle about A
![Page 19: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/19.jpg)
Application: solution of a BVP(Boundary Value Problem)
0';00
022
2
yyy
ykdx
yd
0cossin
sin
kkk
kxAxy
Solution
![Page 20: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/20.jpg)
ProblemSolve previous equation for the smallest suitablenonzero value of k (eigenvalue) using (a) The Newton method (script ‘NEWT.M’)(b) The built-in Newton method (script ‘ZEROIN.M’)(c) The secant method (script ‘SEC.M’)
![Page 21: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/21.jpg)
FIXED POINT ITERATIONS
1
,,
Kx
baxbax
xx
Unique fixed point in interval [a,b]
![Page 22: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/22.jpg)
fixed-point iteration: the idea
0,2
1
0
2
1
1
1
n
n
n
nn
n
nn
x
x
x
x
xx
![Page 23: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/23.jpg)
Error estimate
kkkk
kkk
k
xxx
ce
xxx
1
1
sin
1
1
use difference between successive iterates to estimate error
![Page 24: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/24.jpg)
Newton iteration as fixed-point iteration: quadratic convergence
0
1
2
22
1
f
ff
xf
xfxf
xf
xfxf
xf
xfx
xf
xfxxx
k
kkkk
![Page 25: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/25.jpg)
Example: the logistic map
4
10,14 axaxx
What happens if uniqueness is violated?
![Page 26: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/26.jpg)
function fixed_point()close alla = .9; tol = 10^(-16); imax = 1000; i = 1; deltax = 1;x(1) = .01;while deltax >= tol & i <= imax x(i+1) = f1(x(i),a); deltax = abs(x(i+1)-x(i)); i = i+1;endn = length(x) xx = linspace(0,1,100); yy = f1(xx,a);
plot(xx,xx,'g',xx,yy,'b',x(1:n-1),x(2,n),'ro')
function y = f1(x,a)y = 4*a*x.*(1-x);
![Page 27: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/27.jpg)
a=.2: unique fixed point at x=0
![Page 28: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/28.jpg)
a=.4
![Page 29: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/29.jpg)
![Page 30: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/30.jpg)
a=.5
![Page 31: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/31.jpg)
![Page 32: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/32.jpg)
a=.75: slow convergence
![Page 33: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/33.jpg)
![Page 34: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/34.jpg)
a=.8: fixed point of order 2
![Page 35: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/35.jpg)
![Page 36: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/36.jpg)
a=.875: fixed point of order 4
![Page 37: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/37.jpg)
a=.89: order 8
![Page 38: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/38.jpg)
a=.9: no fixed points (chaos)
![Page 39: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/39.jpg)
![Page 40: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/40.jpg)
a=.95
![Page 41: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/41.jpg)
FMINBND Scalar bounded nonlinear function minimization. X = FMINBND(FUN,x1,x2) starts at X0 and finds a local minimizer X of the function FUN in the interval x1 < X < x2. FUN accepts scalar input X and returns a scalar function value F evaluated at X.
![Page 42: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/42.jpg)
SPLINE Cubic spline data interpolation. YY = SPLINE(X,Y,XX) uses cubic spline interpolation to find YY, the values of the underlying function Y at the points in the vector XX. The vector X specifies the points at which the data Y is given. If Y is a matrix, then the data is taken to be vector-valued and interpolation is performed for each column of Y and YY will be length(XX)-by-size(Y,2). PP = SPLINE(X,Y) returns the piecewise polynomial form of the cubic spline interpolant for later use with PPVAL and the spline utility UNMKPP. Ordinarily, the not-a-knot end conditions are used. However, if Y contains two more values than X has entries, then the first and last value in Y are used as the endslopes for the cubic spline. Namely: f(X) = Y(:,2:end-1), df(min(X)) = Y(:,1), df(max(X)) = Y(:,end) Example: This generates a sine curve, then samples the spline over a finer mesh: x = 0:10; y = sin(x); xx = 0:.25:10; yy = spline(x,y,xx); plot(x,y,'o',xx,yy)
![Page 43: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/43.jpg)
PPVAL Evaluate piecewise polynomial. V = PPVAL(PP,XX) returns the value at the points XX of the piecewise polynomial contained in PP, as constructed by SPLINE or the spline utility MKPP. V = PPVAL(XX,PP) is also acceptable, and of use in conjunction with FMINBND, FZERO, QUAD, and other function functions. Example: Compare the results of integrating the function cos and this spline: a = 0; b = 10; int1 = quad(@cos,a,b,[],[]); x = a : b; y = cos(x); pp = spline(x,y); int2 = quad(@ppval,a,b,[],[],pp); int1 provides the integral of the cosine function over the interval [a,b] while int2 provides the integral over the same interval of the piecewise polynomial pp which approximates the cosine function by interpolating the computed x,y values.
![Page 44: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/44.jpg)
0x1x
0xf
fp
0_
00_01 xfxf
xfxxxx
The Secant iteration for locating zeros
_x
_xf
![Page 45: Root Finding The solution of nonlinear equations and systems Vageli Coutsias, UNM, Fall ‘02.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649e535503460f94b49239/html5/thumbnails/45.jpg)
% script SEC.M: uses secant method to find zero of FUNC.Mclose all; clear all; clc; format long efname = 'func'; a = input('Enter a:');b = input('Enter b:');xc = input('Enter starting value:'); fc = feval(fname,xc,a,b); del = .0001; k=0; disp(sprintf('k x fval fpval '))
fpc = (feval(fname,xc+delta,a,b)-fc)/delta;while input('secant step? (0=no, 1=yes)') k=k+1; x(k) = xc; y(k) = fc; f_ = fc; xnew = xc - fc/fpc; x_ = xc; xc = xnew; fc = feval(fname,xc,a,b); fpc= (fc - f_)/(xc-x_);disp(sprintf('%2.0f %20.15f %20.15f %20.15f',k,xc,fc,fpc))
end if x(1) <= x(k); xa = floor(x(1)-.5); xb = ceil(x(k)+.5); else; xb = floor(x(1)-.5); xa = ceil(x(k)+.5); end x0=linspace(xa,xb,201);y0=feval(fname,x0,a,b);plot(x0,y0,x,y,'r*')