Can this be arranged to run

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 Commenti

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
MINATI il 26 Set 2020
Dear Walter
I have copied this code but unable to arrange this so that it will run to create mesh plot.
And can I put f = exp(x) + sin(y); g = exp(x) .* sin(y); ?
This is a 2D steady state pde.
Thanks
f = @(x,y) exp(x) + sin(y);
g = @(x,y) exp(x) .* sin(y);
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:
  1. Describe the errors, if any, that you encounter when you run the code
  2. Describe the difference between what the code currently does and what you want it to do
  3. Give us the values for f, g, bx0, bxf, by0, byf, D, Mx, My, tol, MaxIter that you are testing with.
MINATI
MINATI il 26 Set 2020
Modificato: MINATI il 26 Set 2020
  1. When I run the code: Error came as,
Not enough input arguments.
Error in FDMpde_poisson (line 2)
x0 = D(1); xf = D(2); y0 = D(3); yf = D(4);
2. The code didn't run, Needs to be rearranged to draw mesh plot
3. f = @(x,y) exp(x) + sin(y); g = @(x,y) exp(x) .* sin(y);
x0 = 0; xf = 4; Mx = 20; y0 = 0; yf = 4; My = 20;D = [x0 xf y0 yf];
bx0 = exp(y) - cos(y); bxf = exp(y)*cos(4) - exp(4)*cos(y);
by0 = cos(x) - exp(x); byf = exp(4)*cos(x) - exp(x)*cos(4);
MaxIter = 500; tol = 1e-4;
Rik
Rik il 26 Set 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
MINATI il 26 Set 2020
@Rik
green button
Keeping both the parts in a single code
Rik
Rik il 26 Set 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.

Accedi per commentare.

 Risposta accettata

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 Commento

MINATI
MINATI il 26 Set 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

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by