Fortran入门.04.重复 - 北方连萌

Fortran入门.04.重复

Do

写法

Do 变量名=起始是多少, 终点是多少, 每次增加多少
(重复执行的部分)
End Do

如果增量是负,则加到小于终点为止,反之加到大于终点为止
默认增量为1

用例

重复输出

program hello

    implicit none
    integer::x

    do x=1,10
    print *, "Hello World!"
    end do

    print *, ""
    print *, "press any key to continue"
    read *,

end program
 Hello World!
 Hello World!
 Hello World!
 Hello World!
 Hello World!
 Hello World!
 Hello World!
 Hello World!
 Hello World!
 Hello World!

 press any key to continue

常用对数log10计算

program log

implicit none
integer::x
real::y, z

print *, '  数               常用对数'
do x=0,6
y=10**x
z=log10(y)
print *, y, z
end do

print *, ""
print *, "press any key to continue"
read *,

end
       数               常用对数
       1.00000000       0.00000000
       10.0000000       1.00000000
       100.000000       2.00000000
       1000.00000       3.00000000
       10000.0000       4.00000000
       100000.000       5.00000000
       1000000.00       6.00000000
    
     press any key to continue

三角函数表

program tri
implicit none
    integer::degree
    real::radian,s,c,t,x

    print *, 'degree  sin    cos      tan'
    radian=3.1415926/180

    do degree=0,180,30
      x=real(degree)*radian
      s=sin(x)
      c=cos(x)
      t=tan(x)
      print "(i3,3f9.4)",degree,s,c,t
    end do

    print *, ""
    print *, "press any key to continue"
    read *,
    
end
 degree  sin    cos      tan
  0   0.0000   1.0000   0.0000
 30   0.5000   0.8660   0.5774
 60   0.8660   0.5000   1.7321
 90   1.0000  -0.0000*********
120   0.8660  -0.5000  -1.7321
150   0.5000  -0.8660  -0.5774
180  -0.0000  -1.0000   0.0000

 press any key to continue

麦克劳林级数

自然对数

https://wikimedia.org/api/rest_v1/media/math/render/svg/2fe7849758309b19fa49cad2f5e214fb4fbb8044

program e

implicit none
integer::n,b
real::c,s

print *,"输入n"
read *, n

c=1.0
s=1.0
do b=1,n
c=c/real(b)
s=s+c
end do

print *, "e=", s

print *, ""
print *, "press any key to continue"
read *,

end
 输入n
666
 e=   2.71828198

 press any key to continue

正弦

https://wikimedia.org/api/rest_v1/media/math/render/svg/85ab4b6d72c38ae78bcf9d65741747604121f172

program msin
    implicit none
    integer::n,b,degree
    real::c,s,x,y,radian

    print *, 'n,degree='
    read *, n,degree

    radian=3.14159265358979323846/180.0
    x=real(degree)*radian
    y=sin(x)

    c=x
    s=x
    do b=1,n
        c=c*(-1)*(x**2)/(2*real(b))/(2*real(b)+1)
        s=s+c
    end do

    print *, 'sin x=',s
    print *, 'sin x(comparison)=',y

    print *, ''
    print *, 'press any key to continue'
    read *,

end
 n,degree=
 10000,225
 sin x= -0.707106829
 sin x(comparison)= -0.707106709

 press any key to continue

傅里叶级数

 \sum_{n=1}^{\infty} b_n \sin nx

其中bn=(-1)n-1/n,对于输入的n,列出x=0,0.1,0.2,···,2.0时的计算值

program fourier
  implicit none
  integer::n,i,x
  real::s,a

  print *, 'enter a value for n'
  read *, n
  print *, '   10 times x','   sum'
  do x=0,20
    s=0
    do i=1,n
      a=(-1)**(real(i)-1)*(sin(real(i)*0.1*real(x)))/(real(i))
      s=s+a
    end do
    print *, x,s
  end do
  print *, ''
  print *, 'press any key to continue'
  read *,
end
 enter a value for n
66666
  10 times x   sum
           0   0.00000000
           1   4.99984734E-02
           2   9.99970958E-02
           3  0.149997145
           4  0.199994281
           5  0.249995768
           6  0.299991280
           7  0.349991828
           8  0.399990231
           9  0.449990660
          10  0.499992847
          11  0.549987793
          12  0.599990606
          13  0.649989665
          14  0.699990153
          15  0.750003338
          16  0.799997866
          17  0.850003183
          18  0.900006533
          19  0.949996352
          20  0.999998331

 press any key to continue

添加新评论

电子邮件地址不会被公开,评论内容可能需要管理员审核后显示。