Search code examples
maple

I'm looking for some assistance with a Maple procedure


I'm trying to write a procedure in maple to that will approximate a function f as a Chebyshev polynomial of degree n on the interval[-1..1] without using any built-in Maple functions relating to Chebyshev polynomials. http://en.wikipedia.org/wiki/Chebyshev_polynomials

For instance a procedure CPlot so that ,for example, CPlot(f,[2,3,4]) produces a plot of the function f on [-1, 1] together with ,in a different color, its 2nd,3rd and 4th Chebychev approximations. It should work for arbitrary lists of arbitrary length as the second argument. This is my current code:

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:

When trying:

f:=x->x^2:

ChebyPlot(f,[2,5,10]);

I get "Error, (in ChebT) final value in for loop must be numeric or character" on line *

And if I use the build in function T for Chebyshev polynomial, by calling with(orthopoly,T) ,instead of ChebT,which I tested before and it works,all plots on the graph look the same. Any suggestions?


Solution

  • You had a typo, nopc(c) instead of nops(c). I took the liberty of changing sum to add (since ChebT was being wrongly called with first argument having value i-1 for unassigned i). For efficiency I pulled proc defns out of the loop, and then replaced the iterated concatenation of K by a more efficient seq call.

    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]);