[心得] fortran 做辛普森積分
! CF0610.f90
! -----------------------------------------------
!dec$if (.false.)
input x1= (ex. 0.1)
0.1
input x2= (ex. 3.0)
3.0
input no= (ex. 16)
16
dx= 0.1812500
ct= 17
area= 1.985008
Press any key to continue
!dec$endif
! -----------------------------------------------
program CF0610
implicit none
real x(101), y(101)
real x1, x2, dx, xx, sum
integer no, ct, i
! -----------------------------------------------
print *, 'input x1= (ex. 0.1) '
read(*, *)x1
print *, 'input x2= (ex. 3.0) '
read(*, *)x2
print *, 'input no= (ex. 16) '
read(*, *)no
dx= (x2 - x1)/no
print *, 'dx= ', dx
! -----------------------------------------------
! for xx= x1 to x2 step dx do ...
ct= 0
xx= x1
x2= x2 + dx/10.0
do while (xx .LE. x2)
ct= ct + 1
x(ct)= xx
y(ct)= sin(xx)
xx= xx + dx
end do
print *, 'ct= ', ct
! -----------------------------------------------
! do the simpson integrated
sum= 0.0
! for i=1 to (ct-2) step 2 do ...
i= 1
do while (i .LE. (ct-2))
sum= sum + y(i) + 4*y(i+1) + y(i+2)
i= i + 2
end do
sum= sum*((x(2) - x(1))/3.0)
print *, 'area= ', sum
end program CF0610
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.231.9.114
→
09/09 13:55, , 1F
09/09 13:55, 1F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 4 篇):
Fortran 近期熱門文章
PTT數位生活區 即時熱門文章
0
18