Fortran入门

 

数值计算方法

二分法

 1 ! 二分法 求f(x) = x**3 -2*x**2 + 7*x +4 = 0的解
 2     program exam
 3     
 4     real x1, x2, x
 5     real bisect, func
 6     
 7     do
 8         print *, "输入x1,x2的值:"
 9         read *, x1,x2
10         if ( func(x1) * func(x2) <0.0 ) exit
11         print *, "不正确的输入!"
12     enddo
13     
14     x = bisect(x1, x2)
15     print 10, "x = ", x
16 10  format(a, f15.7)
17     
18     end
19     
20     real function bisect(x1, x2)
21     real x1, x2, x, f1, f2, fx
22     x =  (x1+x2)/2.0
23     fx = func(x)
24     do while(abs(fx) > 1e-6)
25         f1 = func(x1)
26         if(f1 * fx < 0) then
27             x2 = x
28         else
29             x1 = x
30         endif
31         x = (x1 + x2) /2.0
32         fx = func(x)
33     enddo
34     bisect = x
35     end
36     
37     function func(x)
38         real x
39         func = x**3 -2*x**2 + 7*x + 4
40     
41     end
42     
43     
View Code

 

posted @ 2020-12-09 12:04  谢义xieyi521149  阅读(342)  评论(0编辑  收藏  举报