[心得] fortran 做辛普森積分

看板Fortran作者 (sjgau)時間16年前 (2008/09/09 13:19), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串1/4 (看更多)
! 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
AutoCAD 的答案是:1.985008, 足夠接近,OK!
09/09 13:55, 1F
文章代碼(AID): #18nWTb8j (Fortran)
文章代碼(AID): #18nWTb8j (Fortran)