Can this be arranged to run
Afficher commentaires plus anciens
function [u,x,y] = poisson(f,g,bx0,bxf,by0,byf,D,Mx,My,tol,MaxIter)
x0 = D(1); xf = D(2); y0 = D(3); yf = D(4);
dx = (xf - x0)/Mx; x = x0 + [0:Mx]*dx;
dy = (yf - y0)/My; y = y0 + [0:My]'*dy;
Mx1 = Mx + 1; My1 = My + 1;
%Boundary conditions
for m = 1:My1, u(m,[1 Mx1])=[bx0(y(m)) bxf(y(m))]; end %left/right side
for n = 1:Mx1, u([1 My1],n) = [by0(x(n)); byf(x(n))]; end %bottom/top
%initialize as the average of boundary values
sum_of_bv = sum(sum([u(2:My,[1 Mx1]) u([1 My1],2:Mx)']));
u(2:My,2:Mx) = sum_of_bv/(2*(Mx + My - 2));
for i = 1:My
for j = 1:Mx
F(i,j) = f(x(j),y(i)); G(i,j) = g(x(j),y(i));
end
end
dx2 = dx*dx; dy2 = dy*dy; dxy2 = 2*(dx2 + dy2);
rx = dx2/dxy2; ry = dy2/dxy2; rxy = rx*dy2;
for itr = 1:MaxIter
for j = 2:Mx
for i = 2:My
u(i,j) = ry*(u(i,j + 1)+u(i,j - 1)) + rx*(u(i + 1,j)+u(i - 1,j)) + rxy*(G(i,j)*u(i,j)- F(i,j));
end
end
if itr > 1 & max(max(abs(u - u0))) < tol, break; end
u0 = u;
end
%solve_poisson
f = inline('0','x','y'); g = inline( '0','x','y');
x0 = 0; xf = 4; Mx = 20; y0 = 0; yf = 4; My = 20;
bx0 = inline( 'exp(y) - cos(y)','y'); bxf = inline( 'exp(y)*cos(4) - exp(4)*cos(y)','y');
by0 = inline( 'cos(x) - exp(x)','x'); byf = inline( 'exp(4)*cos(x) - exp(x)*cos(4)','x');
D = [x0 xf y0 yf]; MaxIter = 500; tol = 1e-4;
[U,x,y] = poisson(f,g,bx0,bxf,by0,byf,D,Mx,My,tol,MaxIter);
clf, mesh(x,y,U), axis([0 4 0 4 -100 100])
8 commentaires
Walter Roberson
le 26 Sep 2020
You do not describe any problems with the code, and you do not describe expected values, and you do not give us sample inputs -- so it is difficult for us to test.
f = inline('0','x','y'); g = inline( '0','x','y');
inline() has been recommended against for about 20 years.
f = @(x,y) zeros(max(size(x),size(y)))
g = f;
MINATI
le 26 Sep 2020
Walter Roberson
le 26 Sep 2020
f = @(x,y) exp(x) + sin(y);
g = @(x,y) exp(x) .* sin(y);
Walter Roberson
le 26 Sep 2020
I repeat:
You do not describe any problems with the code, and you do not describe expected values, and you do not give us sample inputs -- so it is difficult for us to test.
I guess I need to be more explicit:
- Describe the errors, if any, that you encounter when you run the code
- Describe the difference between what the code currently does and what you want it to do
- Give us the values for f, g, bx0, bxf, by0, byf, D, Mx, My, tol, MaxIter that you are testing with.
Rik
le 26 Sep 2020
How are you calling the function? Are you clicking the green run button, or are you calling it from a different script/function or the command window?
MINATI
le 26 Sep 2020
Rik
le 26 Sep 2020
The default of the run button is to run a function without any inputs. So you defined some variables, but you didn't actually provided any inputs to your function.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Polynomials dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!