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.

volt_eq_ex2.m
function [x,solnum]=volt_eq_ex2(n,dom,kind)
% Example: y'(x)+y(x)-x(1+2x)int_0^x exp(t*(x-t))y(t)dt=1+2x, x in [0,1], 
%          y(0)=1
% From: T. A. Driscoll, J. Comp. Phys. 229 (2010) 59805998.
% Exact solution: solex=exp(x^2)
% call [x,solnum]=volt_eq_ex2(32,[0,1],2);
%
tic;
Vspec=volt(n,dom,kind);
D=deriv(n,dom);X=mult(n,dom);A=zeros(n);
F=x2t(myfun(x),kind);myDE;myBC;
sol=A\F;solnum=t2x(sol,kind);
toc;myOUT
function [Vspec,Vphys]=volt(n,dom,kind)
% Volterra operator with kernel K(x,t)
x=pd(n,dom,kind);T=cpv(n,x,dom);[~,J0]=prim(n,dom);
V=T*J0/T;
[YY,XX]=meshgrid(x,x);KW=mykernel(XX,YY);
Vphys=V.*KW;Vspec=T\Vphys*T;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function res=mykernel(x,t)
    % x,t matrices
    res=exp(t.*(x-t));% K(x,t) given by the user
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f=myfun(x)
    f=1+2*x;% f(x) given by the user
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myDE
    A=D+eye(n)-X*(eye(n)+2*X)*Vspec;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myBC
    T=cpv(n,dom(1),dom);
    A(n,:)=T;F(n)=1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function myOUT
solex=exp(x.^2); 
figure(1);
subplot(2,1,1);semilogy(abs(sol),'.');
title('The absolute values of the coefficients of the solution');grid;
subplot(2,1,2);plot(x,solnum);grid;xlabel('x');ylabel('y(x)');
title(['The numerical solution (error = ',num2str(norm(solnum-solex)),')']);
xlabel('x');ylabel('y(x)');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

Contact us