Creating function to use pcg
1 view (last 30 days)
Show older comments
Hello, I am solving a problem that involves matrices of per side, but they are matrices that only have 4 different diagonals of 0, so I am looking to implement what you mention in the following link in the part of Using Function Handle Instead of Numeric Matrix.
I'm going to show you the structure that I have in the matrix for the case of :
function y=afun(x)
nx = 5;
N = nx*nx;
x1 = linspace(0,1,nx);
y1 = x1;
[X,Y] = ndgrid(x1,y1);
dx = x1(2)-x1(1);
k=@(x,y) 1+x.^2+y.^2;
isDirichlet = (X==0) | (X==1) | (Y==0) | (Y==1);
Ad = zeros(N,5);
Ad(:,1) = -k(X(:),Y(:)+dx/2);
Ad(:,2) = -k(X(:)+dx*0.5,Y(:));
Ad(:,4) = -k(X(:)-dx*0.5,Y(:));
Ad(:,5) = -k( X(:), Y(:)-dx/2 );
Ad(:,3) = -sum( Ad(:,[1,2,4,5]), 2 );
idx = find(isDirichlet(:));
L=Ad(:,1);L(idx)=[];B=Ad(:,2);B(idx)=[];
C=Ad(:,3);C(idx)=[];U=Ad(:,4);
U(idx)=[];UP=Ad(:,5);UP(idx)=[];
n=sqrt(size(C,1));
l=B(1:end-1);s=U(2:end);
for i=n:n:(n-1)*n
l(i)=0;s(i)=0;
end
y=([[diag(zeros(n));L(1:(n-1)*n)] +[0;l] +[C] +[s;0] +[UP(n+1:end);diag(zeros(n))]]).*x;
end
but when I apply pcg it does not solve the system correctly. I do not know if my code has any logic errors or if I am not understanding how the implementation works with afun.
I hope you can help me, thank you very much.
0 Comments
Answers (0)
See Also
Categories
Find more on Boundary Conditions 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!