matrix set up for kron use and speed up.
Show older comments
Hi, I am trying an alternative way of doing explicit finite difference method to explore and use other matlab available tools. I am attempting the following algorithm which is
an(j,i)=2*a(j,i)+c1*(a(j+1,i)-2*a(j,i)+a(j-1,i)+a(j,i+1)-2*a(j,i)+a(j,i-1))-ao(j,i);
assuming dx=dy
Unfortunately I run into some problems in the following code.
% iterative method 2-D
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n=100;
i=2:n-1;
j=2:n-1;
dx=1;
dt=0.1;
c1=dt^2/dx^2;
a=zeros(n,n);
ao=zeros(n,n);
an=zeros(n,n);
CC=2-4*c1;
an(j,i)=CC*a(j,i)+c1*(a(j+1,i)+a(j-1,i)+a(j,i+1)+a(j,i-1))-ao(j,i);
% sparse method
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n=100;
dx=1;
dt=0.1;
c1=dt^2/dx^2;
w=zeros(n,n);
wo=zeros(n,n);
wn=zeros(n,n);
CC=2-4*c1;
K = sparse(diag(2*ones(n,1))+diag(-ones(n-1,1),1) + diag(-ones(n-1,1),-1));
I = eye(n);
K2D = kron(c1*I,K)+kron(c1*K,I);
b=[-c1 zeros(1,n-2) -c1 zeros(1,n*n-n-n) -c1 zeros(1,n-2) -c1]'.*wo; % <-- this line gives me an error
wn = 2*w-K2D*w-wo-b;
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!