# Can this be arranged to run

조회 수: 1(최근 30일)
MINATI 26 Sep 2020
댓글: MINATI 26 Sep 2020
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표시숨기기 이전 댓글 수: 7
Rik 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.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Walter Roberson 26 Sep 2020
In a new file, put in the code
f = @(x,y) exp(x) + sin(y);
g = @(x,y) exp(x) .* sin(y);
x0 = 0; xf = 4;
Mx = 20;
My = 20;
y0 = 0; yf = 4;
D = [x0 xf y0 yf];
bx0 = @(y) exp(y) - cos(y);
bxf = @(y) exp(y)*cos(4) - exp(4)*cos(y);
by0 = @(x) cos(x) - exp(x);
byf = @(x) exp(4)*cos(x) - exp(x)*cos(4);
MaxIter = 500;
tol = 1e-4;
[u,x,y] = poisson(f,g,bx0,bxf,by0,byf,D,Mx,My,tol,MaxIter);
Now you can use the green run button on that new file.
I notice that you are using cos() with values that can be up to 4. Are you sure you want your units to be radians, and not degrees?
##### 댓글 수: 1표시숨기기 없음
MINATI 26 Sep 2020
Dear Walter
it is ok now
Thanks
I notice that you are using cos() with values that can be up to 4. Are you sure you want your units to be radians, and not degrees?
I will modify this for my work.
Thanks Again

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by