Note that there are some explanatory texts on larger screens.

plurals
  1. POConfusion with axes differences between surface plot and matrix. (trying to solve Poisson equation)
    primarykey
    data
    text
    <p>I tried to follow this tutorial: <a href="http://www.cryptosystem.org/archives/2009/09/using-matlab-for-numerical-solutions-to-the-discrete-poisson-equation/" rel="nofollow noreferrer" title="Numerical solutions to the discrete poisson equation">Numerical solutions to the discrete poisson equation</a> and add boundary conditions, but I could not get the axes right. Then I hacked around with some variables, and it seems to have start working, but I am not 100% certain. </p> <p>Here is the problem I am solving:</p> <pre><code>u_xx + u_yy = f(x,y), 0 &lt;= x &lt;= 1, 0 &lt;= y &lt;= 1. u(x,y)=lower(x), if y=0 u(x,y)=upper(x), if y=1 u(x,y)=left(y), if x=0 u(x,y)=right(y), if x=1 </code></pre> <p>Solver function is following: (the places that worry me are marked with %WARNING%):</p> <pre><code>function [ hor_padded ] = poisson_solver( f, upper, lower, left, right ,m, n) %m, n - Number of points (same in x and y direction) x_a=0; x_b=1; y_a=0; y_b=1; dx = (x_b-x_a)/m; %dx=dy x=linspace(x_a,x_b,m); y=linspace(y_a,y_b,n); g=zeros(m,n); for i=1:n, for j=1:m, %WARNING 1% g(j,i)=-f(x(i),y(j))*dx*dx; if j==2 g(j,i)=g(j,i)+lower(x(i)); end if j==n-1 g(j,i)=g(j,i)+upper(x(i)); end if i==2 g(j,i)=g(j,i)+left(y(j)); end if i==n-1 g(j,i)=g(j,i)+right(y(j)); end end end b = reshape(g(2:m-1, 2:n-1),(m-2)*(n-2),1); A = gallery('poisson',m-2); U = A\b; u = reshape(U,m-2,n-2); upper_part=upper(x(2:m-1)); lower_part=lower(x(2:m-1)); left_part = left(y'); right_part = right(y'); %WARNING 2% ver_padded = vertcat(lower_part, u, upper_part); hor_padded = horzcat(left_part, ver_padded, right_part); end </code></pre> <ol> <li>Warning 1) Matlab seems to mix x and y axes inside matrix. (as described here: <a href="https://stackoverflow.com/questions/3116849/axis-labeling-question">Axis labeling question</a>). So I changed X and Y directions in the for cycle. Is this correct?</li> <li>Warning 2) I had the upper_part and lower_part the other way around at the beginning, but the boundaries did not meet in the resultant graph. I examined the variables, tried the other way around, and it started working. Is this correct?</li> </ol> <p>My test program is here:</p> <pre><code>clear; m = 100; n = 100; x_a=0; x_b=1; y_a=0; y_b=1; e=exp(1); f=@(x,y)(e*x+e*y); %Boundary conditions upper = @(x) (x); lower = @(x) (3-x); left = @(y) (3-3*y); right = @(y) (2-y); u = poisson_solver(f, upper, lower, left, right, m, n); x=linspace(x_a,x_b,m); y=linspace(y_a,y_b,n); surf(x,y,u); xlabel('x'); ylabel('y'); </code></pre> <p>And another test case: (everything else is as on the upper example):</p> <pre><code>upper = @(x) (exp(x)); lower = @(x) (cos(x*2*pi)); left = @(y) (cos(y*2*pi)); right = @(y) (exp(y)); </code></pre> <p>Names "upper", "lower", "left" and "right" refer to the resultant graph, not the corresponding places in the matrix u in solver function.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload