Nullstellenbestimmung durch das Newton-Verfahren
Programm zur Bestimmung der Nullstelle einer Funktion f
durch das Newton-Verfahren.
module mysubs
contains
function f(x)
use kinds
implicit none
real(kind=REAL8) :: f
real(kind=REAL8), intent(in) :: x
f = cos(x) - x
return
end function f
function fstrich(x)
use kinds
implicit none
real(kind=REAL8) :: fstrich
real(kind=REAL8), intent(in) :: x
fstrich = - sin(x) - 1
return
end function fstrich
subroutine init(x0, eps)
use kinds
implicit none
real(kind=REAL8), intent(out) :: x0, eps
write (*,*) "Exemplarische Darstellung einer Nullstellensuche durch "
write (*,*) "das Newton-Verfahren anhand der Funktion f=cos(x)-x"
write (*,*)
write (*,*) "Geben Sie bitte den Anfangswert ein."
read (*,*) x0
write (*,*) "Wie genau soll die Nullstelle bestimmt werden?"
read (*,*) eps
do
if (eps > 0) exit
write (*,*) "Nur positive Genauigkeiten machen Sinn. Versuchen Sie es &
&noch einmal."
read (*,*) eps
end do
return
end subroutine init
subroutine result(x)
use kinds
implicit none
real(kind=REAL8), intent(in) :: x
write (*,'(A, F10.7)') 'Die Nullstelle liegt bei', x
return
end subroutine result
end module mysubs
program Newton
! Nullstellensuche der Funktion f=cos(x)-x um den Wert x0
! durch das Newton-Verfahren
use kinds
use mysubs
implicit none
real(kind=REAL8) :: x0 ! Anfangswert
real(kind=REAL8) :: eps ! Genauigkeit
real(kind=REAL8) :: fehler, x, y
call init(x0, eps)
x = x0
do
y = x - f(x)/fstrich(x)
fehler = abs(y-x)
x = y
if (fehler < eps) exit
end do
call result(x)
stop
end program Newton