Re: [心得] fortran 做辛普森積分
以下的程式,跑三次
x1= 0.1
x2= 4.0
no= 10, ans= 1.658253
no= 20, ans= 1.658258
no= 40, ans= 1.658259
.
.
.
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)
y(ct)= sin(xx)/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.0.58
討論串 (同標題文章)
完整討論串 (本文為第 4 之 4 篇):
Fortran 近期熱門文章
PTT數位生活區 即時熱門文章
-4
30