SHOW:
|
|
- or go back to the newest paste.
1 | program Lista01_Q09 | |
2 | ||
3 | implicit none | |
4 | ||
5 | integer, parameter :: prec9 = selected_real_kind(p=9) | |
6 | real (kind=prec9) :: root, bissecMethod, falsaPosMethod, a, b | |
7 | ||
8 | print *, 'Entre com o inicio e fim do intervalo' | |
9 | read *, a, b | |
10 | ||
11 | root = bissecMethod(a, b) | |
12 | ||
13 | print *, 'Com o metodo da bisseccao', root | |
14 | ||
15 | root = falsaPosMethod(a, b) | |
16 | ||
17 | print *, 'Com o metodo da falsa posicao', root | |
18 | ||
19 | end program | |
20 | ||
21 | function f(x) | |
22 | implicit none | |
23 | ||
24 | integer, parameter :: prec9 = selected_real_kind(p=9) | |
25 | real (kind = prec9) :: f, x | |
26 | ||
27 | f = cos(x) + 1 - x | |
28 | end function f | |
29 | ||
30 | function bissecMethod(a, b) | |
31 | implicit none | |
32 | ||
33 | integer, parameter :: prec9 = selected_real_kind(p=9) | |
34 | real (kind = prec9) :: intervalIni, intervalEnd, c, fcVal, f, bissecMethod, a, b | |
35 | integer :: i | |
36 | ||
37 | intervalIni = a | |
38 | intervalEnd = b | |
39 | ||
40 | do i = 1, 100 | |
41 | c = (intervalIni + intervalEnd) / 2 | |
42 | ||
43 | fcVal = f(c) | |
44 | ||
45 | if(abs(fcVal) < 0.00000001) then | |
46 | exit | |
47 | end if | |
48 | ||
49 | if(f(a) < 0.0) then | |
50 | if(fcVal < 0.0) then | |
51 | intervalIni = c | |
52 | else | |
53 | intervalEnd = c | |
54 | end if | |
55 | else | |
56 | if(fcVal < 0) then | |
57 | intervalEnd = c | |
58 | else | |
59 | intervalIni = c | |
60 | end if | |
61 | end if | |
62 | end do | |
63 | ||
64 | bissecMethod = c | |
65 | end function bissecMethod | |
66 | ||
67 | function falsaPosMethod(a, b) | |
68 | implicit none | |
69 | ||
70 | integer, parameter :: prec9 = selected_real_kind(p=9) | |
71 | real (kind = prec9) :: intervalIni, intervalEnd, c, fcVal, faVal, fbVal, f, falsaPosMethod, a, b | |
72 | integer :: i | |
73 | ||
74 | intervalIni = a | |
75 | intervalEnd = b | |
76 | ||
77 | do i = 1, 100 | |
78 | faVal = f(intervalIni) | |
79 | fbVal = f(intervalEnd) | |
80 | ||
81 | c = intervalEnd - fbVal * (intervalEnd - intervalIni) / (fbVal - faVal) | |
82 | ||
83 | fcVal = f(c) | |
84 | ||
85 | if(abs(fcVal) < 0.00000001) then | |
86 | exit | |
87 | end if | |
88 | ||
89 | if(f(a) < 0.0) then | |
90 | if(fcVal < 0.0) then | |
91 | intervalIni = c | |
92 | else | |
93 | intervalEnd = c | |
94 | end if | |
95 | else | |
96 | if(fcVal < 0) then | |
97 | intervalEnd = c | |
98 | else | |
99 | intervalIni = c | |
100 | end if | |
101 | end if | |
102 | end do | |
103 | ||
104 | falsaPosMethod = c | |
105 | end function falsaPosMethod |