Finite difference approximation of a 2D scalar diffusion equation in QTT [M]=FD_MTX(D, A, BOUND) Generate finite difference matrix from diffusion coefficient A(n,n) or A(n,n,n) M - n^D-by-n^D sparse matrix D - dimensions, BOUND: 0 - Dirichlet, 1 - Neuman TT-Toolbox 2.2, 2009-2012 This is TT Toolbox, written by Ivan Oseledets et al. Institute of Numerical Mathematics, Moscow, Russia webpage: http://spring.inm.ras.ru/osel For all questions, bugs and suggestions please mail ivan.oseledets@gmail.com ---------------------------
0001 function [mat]=Fd_mtx2(a) 0002 %Finite difference approximation of a 2D scalar diffusion equation in QTT 0003 % [M]=FD_MTX(D, A, BOUND) 0004 % Generate finite difference matrix from diffusion coefficient A(n,n) or A(n,n,n) 0005 % M - n^D-by-n^D sparse matrix 0006 % D - dimensions, 0007 % BOUND: 0008 % 0 - Dirichlet, 0009 % 1 - Neuman 0010 % 0011 % 0012 % TT-Toolbox 2.2, 2009-2012 0013 % 0014 %This is TT Toolbox, written by Ivan Oseledets et al. 0015 %Institute of Numerical Mathematics, Moscow, Russia 0016 %webpage: http://spring.inm.ras.ru/osel 0017 % 0018 %For all questions, bugs and suggestions please mail 0019 %ivan.oseledets@gmail.com 0020 %--------------------------- 0021 0022 0023 n=size(a,1)-1; %The coefficient is (n+1)x(n+1); 0024 0025 %The solution is (n).^2, thus the matrix will be of this size 0026 %we need to create 4 arrays: up down left right 0027 % 0028 % 0029 % |x y x | 0030 % y O y 0031 % |x y x | 0032 % 0033 % 0034 0035 %the stencil is 5-point 0036 0037 ad=zeros(n,n); 0038 for i=1:n-1 0039 for j=1:n 0040 ad(i,j)=(a(i+1,j)+a(i+1,j+1))*0.5; 0041 end 0042 end 0043 au=zeros(n,n); 0044 au(2:n,1:n)=ad(1:n-1,1:n); 0045 %for i=1:n-1 0046 % for j=1:n 0047 % au(i+1,j) = (a(i,j+1)+a(i+1,j+1))*0.5; 0048 % end 0049 %end 0050 %au(2:n,1:n-1)=ad(1:n-1,1:n-1); 0051 %au=ad'; 0052 al=zeros(n,n); 0053 for i=1:n 0054 for j=1:n-1 0055 al(i,j)=(a(i,j+1)+a(i+1,j+1))*0.5; 0056 end 0057 end 0058 0059 ar=zeros(n,n); 0060 % for i=1:n 0061 % for j=1:n 0062 % ar(i,j)=(a(i+1,j)+a(i+1,j+1))*0.5; 0063 % end 0064 % end 0065 %ar(1:n-1,2:n)=al(1:n-1,1:n-1); %The matrix is symmetric 0066 ar(1:n,2:n)=al(1:n,1:n-1); 0067 ac=zeros(n,n); 0068 for i=1:n 0069 for j=1:n 0070 ac(i,j)=(a(i,j)+a(i,j+1)+a(i+1,j)+a(i+1,j+1)); 0071 end 0072 end 0073 %2,4,3,1,5 0074 %d 0075 %u 0076 %c 0077 %l 0078 %r 0079 bar_a=[-al(:), -ad(:), ac(:), -au(:), -ar(:)]; 0080 mat = spdiags(bar_a, [-n, -1, 0, 1, n], n^2, n^2); 0081 mat=mat*(n+1).^2; %Assumes [0,1] 0082 0083 return 0084 end