-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNewton_solver.f95
54 lines (46 loc) · 1.37 KB
/
Newton_solver.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
! 使用牛顿法求解函数的根
module NUMERICAL
implicit none
private zero
real, parameter :: zero = 0.0001
contains
real function Newton(a, f, df)
implicit none
real, intent(inout) :: a
real, external :: f ! 需要求解的函数
real, external :: df ! 需要求解的函数的导数
real :: b
real :: FB
b = a - f(a)/df(a)
FB = f(b)
do while(abs(FB) > zero)
a = b
b = a - f(a)/df(a)
FB = f(b)
end do
Newton = b
return
end function
! 求值函数
real function func(x)
implicit none
real, intent(in) :: x
func = sin(x)
return
end function
! 求值函数的导函数
real function dfunc(x)
implicit none
real, intent(in) :: x
dfunc = cos(x)
return
end function
end module
program main
use NUMERICAL
implicit none
real :: a
write(*, *) "请输入起始值:"
read(*, *) a
write(*, *) Newton(a, func, dfunc)
end program