-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSecant.f95
76 lines (65 loc) · 2.1 KB
/
Secant.f95
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
68
69
70
71
72
73
74
75
76
! 此为Fortran95语法的,利用割线法求解函数根的FORTRAN程序;
! 该程序由trace-shadow / watermelon / ZXG进行编写,实验函数为sin(x),实验结果良好。
module NUMERICAL
implicit none
private zero ! 此为私有变量
private GetPoint ! 此函数为私有函数,进行封装
real, parameter :: zero = 0.00001
contains
real function GetPoint(a, b, func)
implicit none
real, intent(in) :: a
real, intent(in) :: b
real, external :: func
real :: FA
real :: FB
real :: k
FA = func(a)
FB = func(b)
k = (FB-FA) / (b-a)
GetPoint = a - FA/k
return
end function
real function Solver(a, b, func)
implicit none
! 声明参数
real, intent(inout) :: a
real, intent(inout) :: b
real, external :: func ! 声明外部函数,进行调用
! 声明函数内临时变量
real :: c
real :: FA
real :: FB
real :: FC
c = GetPoint(a, b, func)
FA = func(a)
FB = func(b)
FC = func(c)
do while(abs(FC - 0) >= zero)
a = b
FA = FB
b = c
FB = FC
c = GetPoint(a, b, func)
FC = func(c)
end do
Solver = c
return
end function
real function func(X)
implicit none
real, intent(in) :: X
func = sin(X)
return
end function
end module
program main
use NUMERICAL
implicit none
real :: a, b
real :: ans
write(*, *) "输入两个猜测的数值:"
read(*, *) a, b
ans = Solver(a, b, func)
write(*, *) ans
end program