Solve the generalized form of the Poisson equation
Show older comments
Hello, I am trying to solve the following problem
in a rectangle with Dirichlet conditions at the boundary. I have the following implementation for this problem:
n =25;
dx = 1/(n-1);
x= 0:dx:1;
y= x;
[X,Y] = ndgrid(x,y);
isDirichlet = (X==0) | (X==1) | (Y==0) | (Y==1);
u = @(x,y) 10*x.*y.*(1-x).*(1-y).*exp(x.^(4.5));
k = @(x,y) cos(x);
f = @(x,y) e^(x.^4.5).*(-45 *(-x.^5.5 + x.^4.5 - 0.444444*x + 0.222222).* (y - 1).* y.* sin(x)...
+ 247.5* (-1.36364*x.^4.5 + x.^3.5 - 0.818182*x.^9 + 0.818182*x.^8 - 0.0808081).*(y - 1).*y.*cos(x)...
- 20*(x - 1).*x.*cos(x));
g = @(x,y) 0;
A = spalloc(n^2,n^2,5);
b = zeros(n^2,1);
for i=1:n^2
l(i)=isDirichlet(i);
end
for i = 1:n^2
if isDirichlet(i)
A(i,i) = 1;
b(i) = g(X(i),Y(i));
continue
end
[row,col] = ind2sub([n,n],i);
L = sub2ind([n,n],row-1,col);
R = sub2ind([n,n],row+1,col);
U = sub2ind([n,n],row,col+1);
D = sub2ind([n,n],row,col-1);
kl = k((X(i)+X(L))/2,Y(i));
kr = k((X(i)+X(R))/2,Y(i));
ku = k(X(i),(Y(i)+Y(U))/2);
kd = k(X(i),(Y(i)+Y(D))/2);
A(i,i) = kl+kr+ku+kd;
A(i,L) = -kl;
A(i,R) = -kr;
A(i,U) = -ku;
A(i,D) = -kd;
b(i) = f(X(i),Y(i))*dx*dx;
end
uh=A\b;
Uh = reshape(uh,[n,n]);
surf(Uh); hold on
surf(u(X,Y)+1);
max(max(abs(Uh-u(X,Y))))
With the above it gives me the following error. With n = 50 I have the following error::
error: sub2ind: index (51,_): out of bound 50
error: called from
prueba at line 33 column 6
But with n = 51 it still works, I don't know what this error could be. Also, I have to solve this for 500, 1000, 5000 and 10000 points, but for that I would need a lot of memory, so I guess you can take advantage of the matrix structure in some way to achieve these results. I appreciate if you could help me to fix that error and see how I can implement for 500, 1000, 5000 and 10000. First of all, thank you.
Accepted Answer
More Answers (0)
Categories
Find more on Eigenvalue Problems 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!