-
Notifications
You must be signed in to change notification settings - Fork 0
/
bisect2.f90
67 lines (43 loc) · 937 Bytes
/
bisect2.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
program bisection
implicit none
real*8, parameter :: toleransi=1.d-5
integer :: langkah
real*8 :: a,b,x1,x2,kesrel
do
write(*,*) 'Masukkan batas kiri, batas kanan:'
read (*,*) a,b
if (f(a)*f(b) < 0.d0) then
exit
else
write(*,*) 'Batas kiri-kanan tidak mengapit akar fungsi.'
write(*,*) 'Coba lagi dengan nilai-nilai yang lain.'
end if
end do
x1=(a+b)*0.5d0
langkah=1
do
if (f(a)*f(x1) < 0.d0) then
b=x1
else
a=x1
end if
x2=(a+b)*0.5d0
kesrel=abs((x1-x2)/x2)
langkah=langkah+1
if (kesrel < toleransi) exit
x1=x2
end do
write(*,*) 'Pencarian akar konvergen pada langkah ke-', langkah
write(*,*) 'Akar = ',x2
write(*,*) 'f(akar) = ',f(x2)
write(*,*) 'Kesalahan relatif =',kesrel
stop
contains
function f(x) result(y)
implicit none
real*8, intent(in) :: x
real*8 :: y
y=cos(x)-x
return
end function f
end program bisection