You are on page 1of 1

program newton_raphson

implicit none

real, parameter :: eps = 1.0e-6, xmin = -10.0, xmax = 10.0

real function f(x)


real, intent(in) :: x
f = x**3 + x**2 - 3.0*x - 3.0
end function

real function df(x)


real, intent(in) :: x
df = 3.0*x**2 + 2.0*x - 3.0
end function

real, dimension(2) :: x, fx

integer :: i, iter, maxiter = 100

! Nilai awal
x(1) = 1.0

do iter = 1, maxiter

! Hitung nilai f(x) dan f'(x)


fx(1) = f(x(1))
fx(2) = df(x(1))

! Perbarui nilai x
x(2) = x(1) - fx(1) / fx(2)

! Periksa konvergensi
if (abs(x(2) - x(1)) < eps) then
exit
end if

! Periksa batas
if (x(2) < xmin) then
x(2) = xmin
end if

if (x(2) > xmax) then


x(2) = xmax
end if

x(1) = x(2)

end do

if (iter <= maxiter) then


print *, "Akar persamaan: ", x(2)
else
print *, "Metode Newton-Raphson gagal konvergen."
end if

end program

You might also like