Sie sind auf Seite 1von 12

Roots of Equations

The root of a one dimensional equation is the value of x for which the equation
y=f(x)=0. One might ask what this has to do with chemistry or science, etc. Th
ere are many examples of chemical problems in which one needs to find the roots
of equations. Some of the chemical examples include

Weak acid/base equilibria; titrations

The vapor pressure equation, ln P = A + (B/T) + C ln T

Equation of state problems

Equilibrium calculations

Quantum mechanics

Kinetics
The philosophy of finding roots of equations is to use a series of approximation
s and iteration to solve the problems, in short, to use brute force. Why do we
use this approach? A computer can do about 106 operations/sec or about 3.6 x 10
9 operations/hr. A talented person might be able to do 1 operation/sec or about
4 x 108 operations in a lifetime. In short, one hour of computer time is worth
about 1 human lifetime in mathematical problem solving. Thus the speed of comp
uters makes the brute force approach an effective strategy.
Idiots method
The simplest method that is relatively foolproof is to plot the function and see
where it crosses the x axis. Depending on how accurately you need to know the s
olution, this method should give you the answer you need relatively simply.
Newtons method (Newton-Raphson method)
Newtons method consists of four steps:
Guess a value of the root, x.
Calculate f(x) and the derivative f(x), i.e., df(x)/dx
Iterate
Graphically this can be depicted as:

One of the problems with the Newton method is that one needs a good first guess,
especially for multidimensional problems. The following illustrations indicate
some of the difficulties that can occur when one has a poor guess and some path
ological behavior of the equation.

The following derivation demonstrates the convergence of the Newton method.

What if the derivatives are not known?


One can approximate the derivatives using numerical methods (Lesson
11) Thus we have :
As h goes to zero, this is the derivative. BUT the computer arithmetic wont work
as h goes to zero. Can get a sufficiently accurate approximation in many cases
by taking h = 10-3x. This should work for a seven significant figure variable.

Two other methods of getting roots are based on having two point ini
tial guesses about the root, which hopefully bracket the true value. They are c
alled the secant and regula falsi approaches. They are shown schematically belo
w.

In the secant method, two points 1 and 2 are chosen. One draws a line between t
hem. That line intersects the x axis and the projection of that point back on t
he curve gives the point 3. One draws a line from 2 to 3. That line intersects
the x axis at a point which is projected to give the point 4. The procedure is
now repeated using the points 3 and 4. The generating algorithm for this metho
d is
In the regula falsi method (method of false position) , one draw a line between
the two initial points 1 and 2, to give a intersection point with the x axis. T
hat point is projected to the curve to give the point 3. A line is drawn betwee
n 3 and point 2. The point at which it intersects the x axis, is projected to g
ive point 4. Blah, blah, blahThe generating algorithm for the regula falsi metho
d is

Problems with the regula falsi method include the effect of roundoff errors and
slow converging nature of the algorithm.
Bisection
Bisection is the fail-safe method of root-finding.
acket the root. One picks a value of x halfway in
t whether the root is to the right or left of this
the original endpoints and the new middle point to

One picks two points that br


between the two point and tes
point. One then uses one of
start the cycle over again.

Computer Methods
FORTRAN90
These methods are most transparently displayed in FORTRAN where one can follow t
he program logic and the generating algorithms. The first program is xscrsho, a
program to implement the idiots method, i.e., a program to graph a function to s
how its roots.. The function being graphed is the zero order Bessel function.
PROGRAM xscrsho
driver for routine scrsho
REAL bessj0
EXTERNAL bessj0
write(*,*) Graph of the Bessel Function J0:
call scrsho(bessj0)
END
C (C) Copr. 1986-92 Numerical Recipes Software
SUBROUTINE scrsho(fx)
INTEGER ISCR,JSCR
REAL fx
EXTERNAL fx
PARAMETER (ISCR=60,JSCR=21)
INTEGER i,j,jz
REAL dx,dyj,x,x1,x2,ybig,ysml,y(ISCR)
CHARACTER*1 scr(ISCR,JSCR),blank,zero,yy,xx,ff
C

11

12
13

14

15

16

SAVE blank,zero,yy,xx,ff
DATA blank,zero,yy,xx,ff/ , - , l , - , x /
continue
write (*,*) Enter x1,x2 (= to stop)
read (*,*) x1,x2
if(x1.eq.x2) return
do 11 j=1,JSCR
scr(1,j)=yy
scr(ISCR,j)=yy
continue
do 13 i=2,ISCR-1
scr(i,1)=xx
scr(i,JSCR)=xx
do 12 j=2,JSCR-1
scr(i,j)=blank
continue
continue
dx=(x2-x1)/(ISCR-1)
x=x1
ybig=0.
ysml=ybig
do 14 i=1,ISCR
y(i)=fx(x)
if(y(i).lt.ysml) ysml=y(i)
if(y(i).gt.ybig) ybig=y(i)
x=x+dx
continue
if(ybig.eq.ysml) ybig=ysml+1.
dyj=(JSCR-1)/(ybig-ysml)
jz=1-ysml*dyj
do 15 i=1,ISCR
scr(i,jz)=zero
j=1+(y(i)-ysml)*dyj
scr(i,j)=ff
continue
write (*, (1x,1pe10.3,1x,80a1) ) ybig,(scr(i,JSCR),i=1,ISCR)
do 16 j=JSCR-1,2,-1
write (*, (12x,80a1) ) (scr(i,j),i=1,ISCR)
continue
write (*, (1x,1pe10.3,1x,80a1) ) ysml,(scr(i,1),i=1,ISCR)
write (*, (12x,1pe10.3,40x,e10.3) ) x1,x2
goto 1
END
(C) Copr. 1986-92 Numerical Recipes Software
FUNCTION bessj0(x)
REAL bessj0,x
REAL ax,xx,z
DOUBLE PRECISION p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4,r5,r6,
*s1,s2,s3,s4,s5,s6,y
SAVE p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4,r5,r6,s1,s2,s3,s4,
*s5,s6
DATA p1,p2,p3,p4,p5/1.d0,-.1098628627d-2,.2734510407d-4,
*-.2073370639d-5,.2093887211d-6/, q1,q2,q3,q4,q5/-.1562499995d-1,
*.1430488765d-3,-.6911147651d-5,.7621095161d-6,-.934945152d-7/
DATA r1,r2,r3,r4,r5,r6/57568490574.d0,-13362590354.d0,
*651619640.7d0,-11214424.18d0,77392.33017d0,-184.9052456d0/,s1,s2,
*s3,s4,s5,s6/57568490411.d0,1029532985.d0,9494680.718d0,
*59272.64853d0,267.8532712d0,1.d0/
if(abs(x).lt.8.)then
y=x**2

bessj0=(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6)))))/(s1+y*(s2+y*(s3+y*
*(s4+y*(s5+y*s6)))))
else
ax=abs(x)
z=8./ax
y=z**2
xx=ax-.785398164
bessj0=sqrt(.636619772/ax)*(cos(xx)*(p1+y*(p2+y*(p3+y*(p4+y*
*p5))))-z*sin(xx)*(q1+y*(q2+y*(q3+y*(q4+y*q5)))))
endif
return
END
C (C) Copr. 1986-92 Numerical Recipes Software
The second program implements a solution using Newtons method.

11

PROGRAM xrtnewt
driver for routine rtnewt
INTEGER N,NBMAX
REAL X1,X2
PARAMETER(N=100,NBMAX=20,X1=1.0,X2=50.0)
INTEGER i,nb
REAL bessj0,rtnewt,root,xacc,xb1(NBMAX),xb2(NBMAX)
EXTERNAL funcd,bessj0
nb=NBMAX
call zbrak(bessj0,X1,X2,N,xb1,xb2,nb)
write(*, (/1x,a) ) Roots of BESSJ0:
write(*, (/1x,t19,a,t31,a/) ) x , F(x)
do 11 i=1,nb
xacc=(1.0e-6)*(xb1(i)+xb2(i))/2.0
root=rtnewt(funcd,xb1(i),xb2(i),xacc)
write(*, (1x,a,i2,2x,f12.6,e16.4) ) Root ,i,root,bessj0(root)
continue
END

SUBROUTINE funcd(x,fn,df)
REAL bessj0,bessj1,df,fn,x
fn=bessj0(x)
df=-bessj1(x)
return
END
C (C) Copr. 1986-92 Numerical Recipes Software
FUNCTION rtnewt(funcd,x1,x2,xacc)
INTEGER JMAX
REAL rtnewt,x1,x2,xacc
EXTERNAL funcd
PARAMETER (JMAX=20)
INTEGER j
REAL df,dx,f
rtnewt=.5*(x1+x2)
do 11 j=1,JMAX
call funcd(rtnewt,f,df)
dx=f/df
rtnewt=rtnewt-dx
if((x1-rtnewt)*(rtnewt-x2).lt.0.)pause
* rtnewt jumped out of brackets
if(abs(dx).lt.xacc) return
11
continue

pause rtnewt exceeded maximum iterations


END
C (C) Copr. 1986-92 Numerical Recipes Software
FUNCTION bessj0(x)
REAL bessj0,x
REAL ax,xx,z
DOUBLE PRECISION p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4,r5,r6,
*s1,s2,s3,s4,s5,s6,y
SAVE p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4,r5,r6,s1,s2,s3,s4,
*s5,s6
DATA p1,p2,p3,p4,p5/1.d0,-.1098628627d-2,.2734510407d-4,
*-.2073370639d-5,.2093887211d-6/, q1,q2,q3,q4,q5/-.1562499995d-1,
*.1430488765d-3,-.6911147651d-5,.7621095161d-6,-.934945152d-7/
DATA r1,r2,r3,r4,r5,r6/57568490574.d0,-13362590354.d0,
*651619640.7d0,-11214424.18d0,77392.33017d0,-184.9052456d0/,s1,s2,
*s3,s4,s5,s6/57568490411.d0,1029532985.d0,9494680.718d0,
*59272.64853d0,267.8532712d0,1.d0/
if(abs(x).lt.8.)then
y=x**2
bessj0=(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6)))))/(s1+y*(s2+y*(s3+y*
*(s4+y*(s5+y*s6)))))
else
ax=abs(x)
z=8./ax
y=z**2
xx=ax-.785398164
bessj0=sqrt(.636619772/ax)*(cos(xx)*(p1+y*(p2+y*(p3+y*(p4+y*
*p5))))-z*sin(xx)*(q1+y*(q2+y*(q3+y*(q4+y*q5)))))
endif
return
END
C (C) Copr. 1986-92 Numerical Recipes Software
FUNCTION bessj1(x)
REAL bessj1,x
REAL ax,xx,z
DOUBLE PRECISION p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4,r5,r6,
*s1,s2,s3,s4,s5,s6,y
SAVE p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4,r5,r6,s1,s2,s3,s4,
*s5,s6
DATA r1,r2,r3,r4,r5,r6/72362614232.d0,-7895059235.d0,
*242396853.1d0,-2972611.439d0,15704.48260d0,-30.16036606d0/,s1,s2,
*s3,s4,s5,s6/144725228442.d0,2300535178.d0,18583304.74d0,
*99447.43394d0,376.9991397d0,1.d0/
DATA p1,p2,p3,p4,p5/1.d0,.183105d-2,-.3516396496d-4,
*.2457520174d-5,-.240337019d-6/, q1,q2,q3,q4,q5/.04687499995d0,
*-.2002690873d-3,.8449199096d-5,-.88228987d-6,.105787412d-6/
if(abs(x).lt.8.)then
y=x**2
bessj1=x*(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6)))))/(s1+y*(s2+y*(s3+
*y*(s4+y*(s5+y*s6)))))
else
ax=abs(x)
z=8./ax
y=z**2
xx=ax-2.356194491
bessj1=sqrt(.636619772/ax)*(cos(xx)*(p1+y*(p2+y*(p3+y*(p4+y*
*p5))))-z*sin(xx)*(q1+y*(q2+y*(q3+y*(q4+y*q5)))))*sign(1.,x)
endif
return
END

C (C) Copr. 1986-92 Numerical Recipes Software


SUBROUTINE zbrak(fx,x1,x2,n,xb1,xb2,nb)
INTEGER n,nb
REAL x1,x2,xb1(nb),xb2(nb),fx
EXTERNAL fx
INTEGER i,nbb
REAL dx,fc,fp,x
nbb=0
x=x1
dx=(x2-x1)/n
fp=fx(x)
do 11 i=1,n
x=x+dx
fc=fx(x)
if(fc*fp.lt.0.) then
nbb=nbb+1
xb1(nbb)=x-dx
xb2(nbb)=x
if(nbb.eq.nb)goto 1
endif
fp=fc
11
continue
1
continue
nb=nbb
return
END
C (C) Copr. 1986-92 Numerical Recipes Software
The next program implements the secant method of finding roots.
PROGRAM xrtsec
driver for routine rtsec
INTEGER N,NBMAX
REAL X1,X2
PARAMETER(N=100,NBMAX=20,X1=1.0,X2=50.0)
INTEGER i,nb
REAL bessj0,rtsec,root,xacc,xb1(NBMAX),xb2(NBMAX)
EXTERNAL bessj0
nb=NBMAX
call zbrak(bessj0,X1,X2,N,xb1,xb2,nb)
write(*, (/1x,a) ) Roots of BESSJ0:
write(*, (/1x,t19,a,t31,a/) ) x , F(x)
do 11 i=1,nb
xacc=(1.0e-6)*(xb1(i)+xb2(i))/2.0
root=rtsec(bessj0,xb1(i),xb2(i),xacc)
write(*, (1x,a,i2,2x,f12.6,e16.4) ) Root ,i,root,bessj0(root)
11
continue
END
C (C) Copr. 1986-92 Numerical Recipes Software
FUNCTION rtsec(func,x1,x2,xacc)
INTEGER MAXIT
REAL rtsec,x1,x2,xacc,func
EXTERNAL func
PARAMETER (MAXIT=30)
INTEGER j
REAL dx,f,fl,swap,xl
fl=func(x1)
f=func(x2)
if(abs(fl).lt.abs(f))then

11
C

11
1

rtsec=x1
xl=x2
swap=fl
fl=f
f=swap
else
xl=x1
rtsec=x2
endif
do 11 j=1,MAXIT
dx=(xl-rtsec)*f/(f-fl)
xl=rtsec
fl=f
rtsec=rtsec+dx
f=func(rtsec)
if(abs(dx).lt.xacc.or.f.eq.0.)return
continue
pause rtsec exceed maximum iterations
END
(C) Copr. 1986-92 Numerical Recipes Software
SUBROUTINE zbrak(fx,x1,x2,n,xb1,xb2,nb)
INTEGER n,nb
REAL x1,x2,xb1(nb),xb2(nb),fx
EXTERNAL fx
INTEGER i,nbb
REAL dx,fc,fp,x
nbb=0
x=x1
dx=(x2-x1)/n
fp=fx(x)
do 11 i=1,n
x=x+dx
fc=fx(x)
if(fc*fp.lt.0.) then
nbb=nbb+1
xb1(nbb)=x-dx
xb2(nbb)=x
if(nbb.eq.nb)goto 1
endif
fp=fc
continue
continue
nb=nbb
return
END
(C) Copr. 1986-92 Numerical Recipes Software
FUNCTION bessj0(x)
REAL bessj0,x
REAL ax,xx,z
DOUBLE PRECISION p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4,r5,r6,
*s1,s2,s3,s4,s5,s6,y
SAVE p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4,r5,r6,s1,s2,s3,s4,
*s5,s6
DATA p1,p2,p3,p4,p5/1.d0,-.1098628627d-2,.2734510407d-4,
*-.2073370639d-5,.2093887211d-6/, q1,q2,q3,q4,q5/-.1562499995d-1,
*.1430488765d-3,-.6911147651d-5,.7621095161d-6,-.934945152d-7/
DATA r1,r2,r3,r4,r5,r6/57568490574.d0,-13362590354.d0,
*651619640.7d0,-11214424.18d0,77392.33017d0,-184.9052456d0/,s1,s2,
*s3,s4,s5,s6/57568490411.d0,1029532985.d0,9494680.718d0,
*59272.64853d0,267.8532712d0,1.d0/

if(abs(x).lt.8.)then
y=x**2
bessj0=(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6)))))/(s1+y*(s2+y*(s3+y*
*(s4+y*(s5+y*s6)))))
else
ax=abs(x)
z=8./ax
y=z**2
xx=ax-.785398164
bessj0=sqrt(.636619772/ax)*(cos(xx)*(p1+y*(p2+y*(p3+y*(p4+y*
*p5))))-z*sin(xx)*(q1+y*(q2+y*(q3+y*(q4+y*q5)))))
endif
return
END
C (C) Copr. 1986-92 Numerical Recipes Software
The next program implements regula falsi method.
PROGRAM xrtflsp
driver for routine rtflsp
INTEGER N,NBMAX
REAL X1,X2
PARAMETER(N=100,NBMAX=20,X1=1.0,X2=50.0)
INTEGER i,nb
REAL bessj0,rtflsp,root,xacc,xb1(NBMAX),xb2(NBMAX)
EXTERNAL bessj0
nb=NBMAX
call zbrak(bessj0,X1,X2,N,xb1,xb2,nb)
write(*, (/1x,a) ) Roots of BESSJ0:
write(*, (/1x,t19,a,t31,a/) ) x , F(x)
do 11 i=1,nb
xacc=(1.0e-6)*(xb1(i)+xb2(i))/2.0
root=rtflsp(bessj0,xb1(i),xb2(i),xacc)
write(*, (1x,a,i2,2x,f12.6,e16.4) ) Root ,i,root,bessj0(root)
11
continue
END
C (C) Copr. 1986-92 Numerical Recipes Software
FUNCTION rtflsp(func,x1,x2,xacc)
INTEGER MAXIT
REAL rtflsp,x1,x2,xacc,func
EXTERNAL func
PARAMETER (MAXIT=30)
INTEGER j
REAL del,dx,f,fh,fl,swap,xh,xl
fl=func(x1)
fh=func(x2)
if(fl*fh.gt.0.) pause root must be bracketed in rtflsp
if(fl.lt.0.)then
xl=x1
xh=x2
else
xl=x2
xh=x1
swap=fl
fl=fh
fh=swap
endif
dx=xh-xl
do 11 j=1,MAXIT
rtflsp=xl+dx*fl/(fl-fh)

f=func(rtflsp)
if(f.lt.0.) then
del=xl-rtflsp
xl=rtflsp
fl=f
else
del=xh-rtflsp
xh=rtflsp
fh=f
endif
dx=xh-xl
if(abs(del).lt.xacc.or.f.eq.0.)return
11
continue
pause rtflsp exceed maximum iterations
END
C (C) Copr. 1986-92 Numerical Recipes Software
FUNCTION bessj0(x)
REAL bessj0,x
REAL ax,xx,z
DOUBLE PRECISION p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4,r5,r6,
*s1,s2,s3,s4,s5,s6,y
SAVE p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4,r5,r6,s1,s2,s3,s4,
*s5,s6
DATA p1,p2,p3,p4,p5/1.d0,-.1098628627d-2,.2734510407d-4,
*-.2073370639d-5,.2093887211d-6/, q1,q2,q3,q4,q5/-.1562499995d-1,
*.1430488765d-3,-.6911147651d-5,.7621095161d-6,-.934945152d-7/
DATA r1,r2,r3,r4,r5,r6/57568490574.d0,-13362590354.d0,
*651619640.7d0,-11214424.18d0,77392.33017d0,-184.9052456d0/,s1,s2,
*s3,s4,s5,s6/57568490411.d0,1029532985.d0,9494680.718d0,
*59272.64853d0,267.8532712d0,1.d0/
if(abs(x).lt.8.)then
y=x**2
bessj0=(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6)))))/(s1+y*(s2+y*(s3+y*
*(s4+y*(s5+y*s6)))))
else
ax=abs(x)
z=8./ax
y=z**2
xx=ax-.785398164
bessj0=sqrt(.636619772/ax)*(cos(xx)*(p1+y*(p2+y*(p3+y*(p4+y*
*p5))))-z*sin(xx)*(q1+y*(q2+y*(q3+y*(q4+y*q5)))))
endif
return
END
C (C) Copr. 1986-92 Numerical Recipes Software
SUBROUTINE zbrak(fx,x1,x2,n,xb1,xb2,nb)
INTEGER n,nb
REAL x1,x2,xb1(nb),xb2(nb),fx
EXTERNAL fx
INTEGER i,nbb
REAL dx,fc,fp,x
nbb=0
x=x1
dx=(x2-x1)/n
fp=fx(x)
do 11 i=1,n
x=x+dx
fc=fx(x)
if(fc*fp.lt.0.) then
nbb=nbb+1

xb1(nbb)=x-dx
xb2(nbb)=x
if(nbb.eq.nb)goto 1
endif
fp=fc
11
continue
1
continue
nb=nbb
return
END
C (C) Copr. 1986-92 Numerical Recipes Software
The final program implements the bisection method.
C

11
C

11
C

PROGRAM xrtbis
driver for routine rtbis
INTEGER N,NBMAX
REAL X1,X2
PARAMETER(N=100,NBMAX=20,X1=1.0,X2=50.0)
INTEGER i,nb
REAL bessj0,rtbis,root,xacc,xb1(NBMAX),xb2(NBMAX)
EXTERNAL bessj0
nb=NBMAX
call zbrak(bessj0,X1,X2,N,xb1,xb2,nb)
write(*, (/1x,a) ) Roots of BESSJ0:
write(*, (/1x,t19,a,t31,a/) ) x , F(x)
do 11 i=1,nb
xacc=(1.0e-6)*(xb1(i)+xb2(i))/2.0
root=rtbis(bessj0,xb1(i),xb2(i),xacc)
write(*, (1x,a,i2,2x,f12.6,e16.4) ) Root ,i,root,bessj0(root)
continue
END
(C) Copr. 1986-92 Numerical Recipes Software
FUNCTION rtbis(func,x1,x2,xacc)
INTEGER JMAX
REAL rtbis,x1,x2,xacc,func
EXTERNAL func
PARAMETER (JMAX=40)
INTEGER j
REAL dx,f,fmid,xmid
fmid=func(x2)
f=func(x1)
if(f*fmid.ge.0.) pause root must be bracketed in rtbis
if(f.lt.0.)then
rtbis=x1
dx=x2-x1
else
rtbis=x2
dx=x1-x2
endif
do 11 j=1,JMAX
dx=dx*.5
xmid=rtbis+dx
fmid=func(xmid)
if(fmid.le.0.)rtbis=xmid
if(abs(dx).lt.xacc .or. fmid.eq.0.) return
continue
pause too many bisections in rtbis
END
(C) Copr. 1986-92 Numerical Recipes Software

11
1

FUNCTION bessj0(x)
REAL bessj0,x
REAL ax,xx,z
DOUBLE PRECISION p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4,r5,r6,
*s1,s2,s3,s4,s5,s6,y
SAVE p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4,r5,r6,s1,s2,s3,s4,
*s5,s6
DATA p1,p2,p3,p4,p5/1.d0,-.1098628627d-2,.2734510407d-4,
*-.2073370639d-5,.2093887211d-6/, q1,q2,q3,q4,q5/-.1562499995d-1,
*.1430488765d-3,-.6911147651d-5,.7621095161d-6,-.934945152d-7/
DATA r1,r2,r3,r4,r5,r6/57568490574.d0,-13362590354.d0,
*651619640.7d0,-11214424.18d0,77392.33017d0,-184.9052456d0/,s1,s2,
*s3,s4,s5,s6/57568490411.d0,1029532985.d0,9494680.718d0,
*59272.64853d0,267.8532712d0,1.d0/
if(abs(x).lt.8.)then
y=x**2
bessj0=(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6)))))/(s1+y*(s2+y*(s3+y*
*(s4+y*(s5+y*s6)))))
else
ax=abs(x)
z=8./ax
y=z**2
xx=ax-.785398164
bessj0=sqrt(.636619772/ax)*(cos(xx)*(p1+y*(p2+y*(p3+y*(p4+y*
*p5))))-z*sin(xx)*(q1+y*(q2+y*(q3+y*(q4+y*q5)))))
endif
return
END
(C) Copr. 1986-92 Numerical Recipes Software
SUBROUTINE zbrak(fx,x1,x2,n,xb1,xb2,nb)
INTEGER n,nb
REAL x1,x2,xb1(nb),xb2(nb),fx
EXTERNAL fx
INTEGER i,nbb
REAL dx,fc,fp,x
nbb=0
x=x1
dx=(x2-x1)/n
fp=fx(x)
do 11 i=1,n
x=x+dx
fc=fx(x)
if(fc*fp.lt.0.) then
nbb=nbb+1
xb1(nbb)=x-dx
xb2(nbb)=x
if(nbb.eq.nb)goto 1
endif
fp=fc
continue
continue
nb=nbb
return
END
(C) Copr. 1986-92 Numerical Recipes Software

Mathematica
In Mathematica, root finding is done in a black box manner, using canned functio
ns. The three relevant functions are Solve, which finds all roots; Nsolve, whic

h gives a numerical approximation to all roots and FindRoot which searches for
the root of an equation in the vicinity of an initial guess. These functions a
re summarized in the table below.

The following example shows the use of the FindRoot function


Click here to see the Mathematica example

Excel
Excel has two black box functions that can be used to find roots, Goal Seek and
Solver. The following example uses Goal Seek to solve cubic equations.
Consider a cubic equation of the ax3 + bx2 +cx +d =0. The cells b3-b7 contain t
he coefficients a,b,c,d. These coefficients have been set to 2,1,-246 and 360.
Initially the cells e4-e6 are set equal to the formula =a*d4^3+b*d4^2+c*d4+d.
Move to cell d4 and use Goal Seek to find the first solution by varying d4 until
e4 has a zero value. Move to e5 and use Goal Seek to vary d5 until e5 is zero.
Repeat with e6 and d6. The cells d4:d6 should contain the solutions 10,1.5,-1
2.
To do the same procedure using Solver, restore the spreadsheet to the original v
alues you had before using Goal Seek. Move to cell e4 and select Solver from th
e Tools menu. Make the target cell $e$4. set value =0 and select this option.
Select $d$4 as the cell to vary. Click Solve. Repeat for e5, d5 and e6 and d6
.
Click here to call up the Excel example

Das könnte Ihnen auch gefallen