【问题标题】:I'm looking for some assistance with a Maple procedure我正在寻求有关 Maple 程序的帮助
【发布时间】:2012-11-26 10:23:15
【问题描述】:

我正在尝试在 maple 中编写一个程序,将函数 f 近似为区间 [-1..1] 上的 n 次 Chebyshev 多项式,而不使用任何与 Chebyshev 多项式相关的内置 Maple 函数。 http://en.wikipedia.org/wiki/Chebyshev_polynomials

例如一个过程 CPlot,例如,CPlot(f,[2,3,4]) 生成函数 f 在 [-1, 1] 上的图,以及不同颜色的第二个第三和第四切比雪夫近似。它应该适用于任意长度的任意列表作为第二个参数。 这是我当前的代码:

ChebT := proc(n,x)
   local i,firstT,secondT,generalT;
   firstT := 1;
   if n=0 then return firstT end if;
   secondT := x;
   if n=1 then return secondT end if;
   for i from 1 to n-1 do
      generalT := 2*x*secondT - firstT;
      firstT := secondT;
      secondT := generalT;
   end do;
  return expand(secondT)
end proc:


CPlot:=proc(f,L::list)
local j, K,num_ip,num_prj,c,chb;
K:=f(x);
for j from 1 to nops( L)  while j<(nops(L)+1) do
         num_ip := (f,g) -> evalf(Int(f*g/sqrt(1-x^2),x=-1..1)*2/Pi);
         num_prj := (f,n) -> seq(num_ip(f,ChebT(i,x)),i=0..n);
         c := num_prj(f(x),L[j]);
         chb := c -> c[1]/2 + sum(c[i]*ChebT(i-1,x),i=2..nopc(c)); *
         K:=K, chb([c]);
end do;
  plot([K], x=-1..1, colour=[green, red, blue, yellow],linestyle=[1,2,3,4], thickness=[5,2,3,4]);
end proc:

尝试时:

f:=x->x^2:

切比图(f,[2,5,10]);

我在*行得到“错误,(在 ChebT 中)for 循环中的最终值必须是数字或字符”*

如果我为 Chebyshev 多项式使用内置函数 T,通过调用 with(orthopoly,T) 而不是 ChebT(我之前测试过并且它有效),图表上的所有图看起来都一样。 有什么建议吗?

【问题讨论】:

    标签: maple


    【解决方案1】:

    您打错字了,nopc(c) 而不是 nops(c)。我冒昧地将sum 更改为add(因为ChebT 被错误地调用,第一个参数的值为i-1 用于未分配的i)。为了提高效率,我将 proc defns 拉出循环,然后将 K 的迭代串联替换为更高效的 seq 调用。

    restart:
    
    ChebT := proc(n::nonnegint,x)
       local i,firstT,secondT,generalT;
       firstT := 1;
       if n=0 then return firstT end if;
       secondT := x;
       if n=1 then return secondT end if;
       for i from 1 to n-1 do
          generalT := 2*x*secondT - firstT;
          firstT := secondT;
          secondT := generalT;
       end do;
      return expand(secondT)
    end proc:
    
    CPlot:=proc(f,L::list)
    local j,K,num_ip,num_prj,c,chb;
      num_ip:=(f,g)-> evalf(Int(f*g/sqrt(1-x^2),x=-1..1)*2/Pi);
      num_prj:=(f,n)-> seq(num_ip(f,ChebT(i,x)),i=0..n);
      chb:=c->c[1]/2 + add(c[i]*ChebT(i-1,x),i=2..nops(c));
      ### Even if you insist of forming K in a do-loop you should
      ### still pull the assignments to num_ip, num_prj, and chb
      ### outside the loop. There's no need to reassign those each
      ### time through the loop.
      #K:=f(x);
      #for j from 1 to nops(L)  do
      #  c:= num_prj(f(x),L[j]);
      #  K:=K, chb([c]);
      #end do;
      K:=f(x), seq(chb([num_prj(f(x),L[j])]), j=1..nops(L));
      plot([K], x=-1..1, colour=[green, red, blue, yellow],
           linestyle=[1,2,3,4], thickness=[5,2,3,4]);
    end proc:
    
    f:=x->x^2:
    CPlot(f,[2,5,10]);
    

    【讨论】:

    • 谢谢...它有效...有什么方法可以知道/检查程序是否为切比雪夫近似值输出正确的结果/图?
    猜你喜欢
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多