| 查看: 423 | 回复: 5 | |||
| 当前主题已经存档。 | |||
[交流]
[求助]一个关于梯形积分的程序
|
|||
|
program D3R1 !driver for routine TRAPZD real,parameter::nmax=15,pio2=1.5707963 external Func,Fint real::A=0.0,B=pio2 write(*,'(1x,a)') 'integral of Func with 2^(n-1) points' write(*,'(1x,a,f10.6)')'actual value of integral is',& Fint(B)-Fint(A) write(*,'(1x,t7,a,t16,a)')'n','Approx.integral' do i=1,nmax call TRAPZD(Func,A,B,s,i) write(*,'(1x,i6,f20.6)')i,s end do end program D3R1 Function Func(x) Func=(x**2)*(x**2-2.0)*sin(x) end function Func function Fint(x) !integral of Func Fint=4.0*x*((x**2)-7.0)*sin(x)-((x**4)-14.0*(x**2)+28.0)*cos(x) end function Fint subroutine TRAPZD(Func,A,B,s,n) integer::n,tnm integer,parameter::p=selected_real_kind(6,20) real(p)::del,sum,x if(n==1)then s=0.5*(b-a)*(Func(a)+Func(b)) else tnm=2**(n-1) del=(b-a)/tnm x=A sum=0.0 do j=2,tnm x=x+del sum=sum+Func(x) end do s=0.5*(Func(A)+Func(B)+2*sum)*del end if end subroutine TRAPZD 运算的结果如下: integral of Func with 2^(n-1) points actual value of integral is -0.479158 n Approx.integral 1 0.905772 2 -0.020945 3 -0.361461 4 -0.449584 5 -0.471756 6 -0.477307 7 -0.478697 8 -0.479042 9 -0.479126 10 -0.479146 11 -0.479151 12 -0.479152 13 -0.479153 14 -0.479411 15 -0.479676 Press any key to continue 请问n=14,15 时怎么越不接近真实值? |
» 猜你喜欢
实验室接单子
已经有7人回复
假如你的研究生提出不合理要求
已经有11人回复
全日制(定向)博士
已经有5人回复
萌生出自己或许不适合搞科研的想法,现在跑or等等看?
已经有4人回复
Materials Today Chemistry审稿周期
已经有4人回复
参与限项
已经有3人回复
对氯苯硼酸纯化
已经有3人回复
求助:我三月中下旬出站,青基依托单位怎么办?
已经有12人回复
所感
已经有4人回复
要不要辞职读博?
已经有7人回复
★ ★ ★
sunxiao(金币+3,VIP+0):欢迎参与,加分鼓励 3-30 22:39
sunxiao(金币+3,VIP+0):欢迎参与,加分鼓励 3-30 22:39
|
俺不会Fortran,对你的程序只能看了个大概.循环部分没有问题,至少写成matlab后执行是正确的: clear; clc b=pi/2 a=0 nse=[]; sse=[]; for n=10:20 tnm=2^(n-1) del=(b-a)/tnm x=a s=0.0 for j=2:tnm x=x+del; s=s+Func(x); end s=0.5*(Func(a)+Func(b)+2*s)*del nse=[nse n]; sse=[sse s]; end [nse' sse'] --------------------------------------------- 结果是这样的: 10.00000000000000 -0.47915157829510 11.00000000000000 -0.47915700215368 12.00000000000000 -0.47915835811872 13.00000000000000 -0.47915869710992 14.00000000000000 -0.47915878185820 15.00000000000000 -0.47915880304466 16.00000000000000 -0.47915880834252 17.00000000000000 -0.47915880966840 18.00000000000000 -0.47915881000004 19.00000000000000 -0.47915881008306 有点怀疑的是你的变量s是在哪里初始化的,这个值是应该先初始化为零吧. |

2楼2009-03-30 19:53:13
3楼2009-03-30 22:44:55
snoopyzhao
至尊木虫 (职业作家)
- 程序强帖: 16
- 应助: 157 (高中生)
- 贵宾: 0.02
- 金币: 18844.7
- 红花: 29
- 帖子: 3803
- 在线: 1422.4小时
- 虫号: 183750
- 注册: 2006-02-13
- 专业: 污染生态化学
4楼2009-04-01 11:54:19

5楼2009-04-01 12:30:17
snoopyzhao
至尊木虫 (职业作家)
- 程序强帖: 16
- 应助: 157 (高中生)
- 贵宾: 0.02
- 金币: 18844.7
- 红花: 29
- 帖子: 3803
- 在线: 1422.4小时
- 虫号: 183750
- 注册: 2006-02-13
- 专业: 污染生态化学
|
又看了一下,觉得是数据精度的问题。比如把我给出的那个程序中的 DOUBLE PRECISION 全部换成 REAL 后的结果与 LZ 给出的程序结果是一致的。 所以我觉得问题可能出在 (A-B)/TNM 这一步,因为随着 N 的增加, TNM 会变得很大,而 (A-B)/TNM 则会非常小,由于精度的限制,会造成较大的误差,从而导致最后结果的差异。 所以我建议 LZ 将全部的 REAL 类型的变量换成相当于 fortran 77 中的 DOUBLE PRECISION 或 REAL*8 的精度,应该就不会有问题了。 [ Last edited by snoopyzhao on 2009-4-1 at 17:16 ] |
6楼2009-04-01 17:12:33












回复此楼
你的代码是在那个环境里运行的????我也运行一下