我正在尝试用枫树写一个梯形规则程序

时间:2016-05-10 10:29:26

标签: algorithm numerical maple

我正在尝试用枫树写一个梯形规则的程序,我会发布我的代码,但问题是当我读这篇文章时,我发现很难编程所有的数值例子,任何人都可以帮助我吗?

此网址上的文章:http://www.cs.technion.ac.il/~asidi/Sidi_Journal_Papers/P097_JCAM.sinpq.trans.pdf 我枫的代码:

with(linalg); with(LinearAlgebra); with(plots); with(MTM); 
a := 0; b := 1; p := 20; 
k := 1; R := vector(p); 
f := proc (x) options operator, arrow; x*(1-x)/(1+x) end proc;
Uex := int(f(x), x = a .. b); 
Uex := evalf(Uex);
for N to p do 
    h := (b-a)/N;
    U[N] := (f(a)+f(b))*(1/2);
    for j to N-1 do
        U[N] := U[N]+f(a+j*(b-a)/N)
    end do;
    U[N] := h.U[N]
end do;
for N to p do
    R[N] := -evalf(log(abs(Uex-U[N]))/log(10));
    print(N, evalf(Uex-U[N]))
end do;
plot(f); listplot(R)

1 个答案:

答案 0 :(得分:1)

试试这个:

a:= 0: b:= 1: p:= 20: 
R:= Vector(p): H:= Vector(p): 
f:= x-> x*(1-x)/(1+x);
Uex:= int(f(x), x= a..b); 
for n to p do
    N:= 2^n; 
    h:= (b-a)/N;
    H[n]:= h;
    R[n]:= evalhf(abs(Uex-h*((f(a)+f(b))/2 + add(f(a+j*h), j= 1..N-1))))    
end do:
unassign('h');
err = Statistics:-PowerFit(H,R,h);