module funcoes

use dados

contains

!----------------------------------------------------------

subroutine F(N,x,func)

	integer,intent(in) :: N
	real*8,dimension(N),intent(in)  :: x
	real*8,dimension(N),intent(out) :: func
	
	func(1) = 3.0d0*x(1) - dcos( x(2)*x(3) ) - 5.0d-1
	func(2) = x(1)**2 - 81.0d0*( x(2) + 1.0d-1 )**2 + &
	          dsin( x(3) ) + 1.06d0
	func(3) = dexp( -(x(1)*x(2)) ) + 20.0d0*x(3) + &
	  		  ( 10.0d0*dacos(-1.0d0) - 3.0d0)/3.0d0

end subroutine F

!----------------------------------------------------------

subroutine jacobiano

	Jac(1,1) = 3.0d0
	Jac(1,2) = x(3)*dsin( x(2)*x(3) )
	Jac(1,3) = x(2)*dsin( x(2)*x(3) )

	Jac(2,1) = 2.0d0*x(1)
	Jac(2,2) = -162.0d0*( x(2) + 1.0d-1)
	Jac(2,3) = dcos( x(3) )

	Jac(3,1) = -x(2)*dexp( -(x(1)*x(2)) )
	Jac(3,2) = -x(1)*dexp( -(x(1)*x(2)) )
	Jac(3,3) = 20.0d0

end subroutine jacobiano

!----------------------------------------------------------

subroutine jac_numerico

	integer :: i, j
	real*8,dimension(:),allocatable :: xaux

	allocate ( xaux(N) )

	do i = 1, N

		do j = 1, N

			xaux = x
			xaux(j) = x(j)*(1.0d0+pert)

			call F(N,x,func)
			call F(N,xaux,func2)

			Jac(i,j) = ( func2(i) - func(i) ) / ( xaux(j) - x(j) )

		end do

	end do

	deallocate ( xaux )

end subroutine jac_numerico

!----------------------------------------------------------

end module funcoes