Code covered by the BSD License  

Highlights from
Chebpack

image thumbnail

Chebpack

by

 

15 Jul 2011 (Updated )

The MATLAB package Chebpack solves specific problems for differential or integral equations.

[S1,S]=monomials(p)
function [S1,S]=monomials(p)
% Scaling matrix S 
%
% U: Chebyshev coefficients for f(x)
% S1*U: Chebyshev coefficients for f(px), using monomials for S1
% S*U: Chebyshev coefficients for f(px), using direct calculus for Sdir
% use:  p=1/2;[S1,S]=monomials(p);
%
for n=32:2:128
U=zeros(n,1);
% A: conversion matrix from Chebyshev polynomials to monomials
[~,A]=cheb2poly(U);
S1=A\diag(p.^(0:n-1))*A;S1(1,:)=2*S1(1,:);S1(1,1)=S1(1,1)/2;
% Direct calculus using dev.m from Chebpack
dom=[-1,1];kind=2;x=pd(n,dom,kind);S=dev(n,x,dom,p*x);
% Comparation
f=5./(1+exp(-8*x));fp=5./(1+exp(-8*p*x));
U=x2t(f,kind);UU=S*U;UUval=t2x(UU,kind);Edir=norm(UUval-fp);
UUm=S1*U;UUvalm=t2x(UUm,kind);Emon=norm(UUvalm-fp);
semilogy(n,Edir,'.k',n,Emon,'ok');grid;xlabel('n');ylabel('Errors');
legend('Chebyshev','monomials');hold on;pause(0.1);
end

function [P,A]=cheb2poly(U)
% From "ChebyshevTools" by Michael Watson
% http://www.mathworks.com/matlabcentral/fileexchange/18496-chebyshevtools
%
M=length(U);temp=zeros(M,1);temp(1)=1;
A=temp;temp=zeros(M,1);temp(2)=1;A=[A,temp];
for j=3:M
   back=A(:,j-2:j-1);
   back(:,2)=[0;back(1:end-1,2)];
   A=[A,2*back(:,2)-back(:,1)];
end
P=A*U;
end
end

Contact us