Re: [心得] fortran 做辛普森積分

看板Fortran作者 (sjgau)時間16年前 (2008/09/11 16:08), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/4 (看更多)
以下的程式,跑三次 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
文章代碼(AID): #18oD7rC0 (Fortran)
文章代碼(AID): #18oD7rC0 (Fortran)