Anda di halaman 1dari 3

program nrmult

implicit none
real*8 tolf,tolx,x
real*8 d,errf,errx,fjac,fvec,p
integer n,ntrial,NP,m,i,k,indx
integer r,s
parameter(ntrial=500,tolf=1*10**-8.d0,tolx=1*10**-8d0,n=2,NP=2)
dimension x(n),fjac(NP,NP),fvec(NP),p(NP),indx(NP)
x(1)=0
x(2)=0
do k=1,ntrial
!write(*,*)'X:',(x(r),r=1,n)
call usrfun(x,n,NP,fvec,fjac)

!write(*,*)'Fvec:',(fvec(s),s=1,n)
!do r=1,n
!write(*,*)'Fjac:',(fjac(r,s),s=1,n)
!enddo
!pause

errf=0
do i=1,n
errf=errf+abs(fvec(i))
enddo
if(errf.le.tolf)goto 1
do i=1,n
p(i)=-fvec(i)
enddo
call ludcmp(fjac,n,NP,indx,d)
call lubksb(fjac,n,NP,indx,p)
!write(*,*)'P:',(p(s),s=1,n)
!pause
errx=0.d0
do i=1,n
errx=errx+abs(p(i))
x(i)=x(i)+p(i)
enddo
if(errx.le.tolx)goto 1
enddo
1 write(*,*)'X:',(x(r),r=1,n)
pause
end

subroutine usrfun(x,n,NP,fvec,fjac)
!usrfun(x,n,NP,fvec,fjac)
real*8 x,fvec,fjac
integer n,NP
dimension x(n),fjac(NP,NP),fvec(NP)
fjac(1,1)=2*x(1)-10
fjac(1,2)=2*x(2)
fjac(2,1)=x(2)**2+1
fjac(2,2)=2*x(1)*x(2)-10
fvec(1)=x(1)**2-10*x(1)+x(2)**2+8
fvec(2)=(x(1)*x(2)**2)+x(1)-10*x(2)+8
return
end
subroutine ludcmp(a,n,NP,indx,d)
integer n,np,indx,NMAX
real*8 d,a,TINY
integer i,imax,j,k
real*8 aamax,dum,sum,vv
parameter(NMAX=500,TINY=1.0*10-20d0)
dimension indx(n),a(NP,NP),vv(NMAX)
d=1.d0
do i=1,n !12
aamax=0.d0
do j=1,n !11
if(abs(a(i,j)).gt.aamax)aamax=abs(a(i,j))
enddo !11
if(aamax.eq.0)pause
vv(i)=1/aamax
enddo !12
do j=1,n !19
do i=1,j-1 !14
sum=a(i,j)
do k=1,i-1 !13
sum=sum-a(i,k)*a(k,j)
enddo !13
a(i,j)=sum
enddo !14
aamax=0
do i=j,n !16
sum=a(i,j)
do k=1,j-1 !15
sum=sum-a(i,k)*a(k,j)
enddo !15
a(i,j)=sum
dum=vv(i)*abs(sum)
if(dum.ge.aamax)then
imax=i
aamax=dunm
endif
enddo !16
if(j.ne.imax)then
do k=1,n !17
dum=a(imax,k)
a(imax,k)=a(j,k)
a(j,k)=dum
enddo !17
d=-d
vv(imax)=vv(j)
endif
indx(j)=imax
if(a(j,j).eq.0)a(j,j)=TINY
if(j.ne.n)then
dum=1/a(j,j)
do i=j+1,n !18
a(i,j)=a(i,j)*dum
enddo !18
endif
enddo !19
return
end

subroutine lubksb(a,n,NP,indx,b)
integer n,np,indx
real*8 a,b
integer i,ii,j,ll
real*8 sum
dimension a(NP,NP),b(n),indx(n)
ii=0
do i=1,n !12
ll=indx(i)
sum=b(ll)
b(ll)=b(i)
if(ii.ne.0)then
do j=ii,i-1 !11
sum=sum-a(i,j)*b(j)
enddo !11
elseif(sum.ne.0)then
ii=i
endif
b(i)=sum
enddo !12
do i=n,1,-1 !14
sum=b(i)
do j=i+1,n !13
sum=sum-a(i,j)*b(j)
enddo !13
b(i)=sum/a(i,i)
enddo
return
end