Problem Solutions 3

advertisement
Problem Solutions 3
Jianjun Jia
Program codes are available on website:
http://www.pha.jhu.edu/~jiajj/mathmethods/hw3/
1. This function has a maximum value of 0.8619 at the point x=1.137268.
For the pure Newton-Raphson method, the iteration jumped out of brackets, no matter how
closely I bracketed the root. However, for the hybrid bisection-Newton-Raphson algorithm,
the maximum range of the bracket should not be over (0.4, 7.7), otherwise, it would take the
solution out of bounds.
If the initial bracket is (0.4, 5.0) for the hybrid bisection-Newton-Raphson algorithm, the
program has 21 bisection steps before getting the final root.
PROGRAM main
INTEGER N,NBMAX,number
REAL X1,X2,xl,xh
PARAMETER(N=100,NBMAX=20)
INTEGER i,nb
REAL rtnewt,root,xacc,xb1(NBMAX),xb2(NBMAX),err,h
EXTERNAL funcd,func,dfridr,dfunc
X1=0.4
X2=5.0
nb=NBMAX
do 11 i=1,nb
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
use newton's method and hybrid algorithm
c
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
c
root=rtnewt(funcd,x1,x2,1e-6)
root=rtsafe(funcd,x1,x2,1e-6,number)
11
write(*,'(1x,a,i2,2x,f12.6,2e16.4)') 'Root ',i,root,dfunc(root),
* func(root)
continue
write(*,'(1x,a,3x,i2)')'number of steps =',number
END
SUBROUTINE funcd(x,fn,df)
REAL dfridr,err,dfunc,h,df,fn,x
external func,dfunc
h=0.1
fn=dfunc(x)
df=dfridr(dfunc,x,h,err)
return
END
c
REAL FUNCTION func(x)
function of Fn(x) for n=10
real x
func=x**10*exp(10.*(x-1.))/(1+exp(20.*(x-1)))
return
end
c
REAL FUNCTION dfunc(x)
derivative of function Fn(x), n=10
EXTERNAL func
real x,h
h=0.1
dfunc=dfridr(func,x,h,err)
return
end
2. The roots of the polynomial are
{-5.0, -4.0, -2.9075, -2.5, 2.0, 2.4075}.
The algorithm and subroutines applied in this calculation is described in the comments of the
program codes below.
c
c
c
program main
parameter(m=6)
real a(m+1),rootr(m),rooti(m)
a(m+1) are the real coefficients, rootr and rooti
are the real and imaginary parts of the roots.
data a/700.0,195.0,-254.0,-91.75,17.25,10.0,1.0/
uses "zrhqr","hqr","balanc" subroutines
call zrhqr(a,m,rootr,rooti)
c
write(*,'(1x,2f15.6)')(rootr(i),rooti(i),i=1,m)
end
3. The roots of the set of equations are
{1.58108, 1.21879, 3.11363}.
I chose {1.5, 1.6, 2.5} as the initial guess of the solutions, and used the Newton-Raphson
method for nonlinear systems of equations to calculate numerically.
c
12
13
PROGRAM main
INTEGER NTRIAL,N,NP
REAL TOLX,TOLF
PARAMETER(NTRIAL=5,TOLX=1.0E-6,N=3,TOLF=1.0E-6,NP=15)
INTEGER i,j,k,kk
REAL xx,fjac(NP,NP),fvec(NP),x(NP)
initial guess of the solution
x(1)=1.5
x(2)=1.6
x(3)=2.5
do 13 j=1,NTRIAL
call mnewt(1,x,N,TOLX,TOLF)
call usrfun(x,n,NP,fvec,fjac)
write(*,'(/1x,t5,a,t14,a,t29,a/)') 'I','X(I)','F'
do 12 i=1,N
write(*,'(1x,i4,2e15.6)') i,x(i),fvec(i)
continue
write(*,'(/1x,a)') 'press RETURN to continue...'
read(*,*)
continue
END
SUBROUTINE usrfun(x,n,np,fvec,fjac)
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
User subroutine supplies function values at x
C
C
in fvec and Jacobian matrix in fjac
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
INTEGER i,n,np
REAL fjac(np,np),fvec(np),x(np)
fjac(1,1)=x(2)*x(3)
fjac(1,2)=x(1)*x(3)
fjac(1,3)=x(1)*x(2)
fjac(2,1)=2.*x(1)*x(2)+x(3)**2
fjac(2,2)=2.*x(2)*x(3)+x(1)**2
fjac(2,3)=2.*x(3)*x(1)+x(2)**2
fjac(3,1)=exp(x(1)+x(2)+x(3))
fjac(3,2)=exp(x(1)+x(2)+x(3))
fjac(3,3)=exp(x(1)+x(2)+x(3))
fvec(1)=x(1)*x(2)*x(3)-6.
fvec(2)=x(1)**2*x(2)+x(2)**2*x(3)+x(3)**2*x(1)-23.
fvec(3)=exp(x(1)+x(2)+x(3))-370.
END
Download