Trying to understand the implementation of boundary conditions in this MATLAB code


9 Comments
Hi Janee,
You asked, Now, I have a similar problem where I am trying to reproduce the exact BCs for a 2D Poisson's equation/problem. What I am struggling to understand is this line:BC=-Dy([1 Ny+1],[1 Ny+1])\Dy([1 Ny+1],2:Ny); %Homogenous Neumann BCs for y=1 I am not sure what is the author doing here so I can reproduce these exact BCs in my code. My understanding is we want to replace the first/last rows of D2 with values of first/last rows of D and set them to zero? Meaning we want to set the values if the first derivative to zero? Is this correct or am I misunderstanding the syntax
You did ask excellent questions and sounds like you are motivated about implementing Neumann boundary conditions to ensure that the flux or gradient of the solution is appropriately handled at the boundaries, reflecting physical constraints or characteristics of the problem being modeled. Let me break down the line in question and provide a detailed explanation to help you grasp its purpose and functionality.
The line of code you mentioned:
BC = -Dy([1 Ny+1],[1 Ny+1])\Dy([1 Ny+1],2:Ny); %Homogeneous Neumann BCs for y=1
Here's a breakdown of what this line is doing:
1. `Dy([1 Ny+1],[1 Ny+1])`: This part refers to selecting a submatrix from the matrix `Dy`. Specifically, it selects rows 1 and Ny+1, and columns 1 to Ny+1.
2. `\`: This symbol denotes matrix division. In this context, it is used to solve a system of linear equations represented by the two submatrices on either side of the division symbol.
3. `Dy([1 Ny+1],2:Ny)`: This part selects rows 1 and Ny+1 from the matrix `Dy` but only columns from 2 to Ny.
The purpose of this line is to apply homogeneous Neumann boundary conditions for y=1 in the context of your 2D Poisson's equation. By solving this linear system using the specified submatrices from `Dy`will ensure that the first and last rows' derivatives are set to zero, effectively enforcing Neumann boundary conditions at those boundaries.
Regarding your understanding, you are on the right track. The goal is indeed to replace the first and last rows of `D` with values from the first and last rows of `Dy` while setting them to zero. This process ensures that the first derivative values at those boundaries are zero, which aligns with the concept of homogeneous Neumann boundary conditions.
If you have any further questions or need additional clarification on any part of this explanation, feel free to ask.
Hi Janee,
I was reading your earlier comments regarding, “to reproduce the exact BCs for a 2D Poisson's equation/problem”. So, I did some experiments with my code to solve a 2D Poisson's equation with specific boundary conditions using MATLAB.I started with defining the grid and parameters such as the domain length, number of grid points in x and y directions, and grid spacing. Then, create the grid using meshgrid for x and y coordinates. Define the Poisson's equation function f(x, y) as an example.Set the boundary conditions for the left, right, top, and bottom boundaries. Initialize the solution matrix u with zeros and then, implement the boundary conditions by assigning the specified values to the boundary points. Afterwards, the code solved the Poisson's equation numerically using finite differences for the interior points.Finally, I was able to visualize the solution using surf to plot the 2D surface of the solution. Please see attached snippet code.

Please see attached plot.

Hi Janee,
Try to modify the differentiation matrix calculation within the Cheb function, (Reason: The Cheb function you are using is setting the values of u(x, y) to zero instead). I just made the modification in your Cheb function, here is revised version,
function [D, x] = cheb(N)
if (N == 0)
D = 0.0;
x = 1.0;
return
end x = cos(pi * (0:N) / N)';
c = [2.0; ones(N-1, 1); 2.0] .* (-1.0).^(0:N)';
X = repmat(x, 1, N + 1);
dX = X - X'; % Set the off-diagonal entries.
D = (c * (1.0 ./ c)') ./ (dX + eye(N + 1));% Modify diagonal entries for enforcing boundary conditions on first derivative.
D(1, :) = zeros(1, N + 1);
D(N + 1, :) = zeros(1, N + 1);% Update diagonal entries.
D = D - diag(sum(D'));
return end
By incorporating modification of diagonal entries for enforcing boundary conditions on first derivative into your Cheb function, you can make sure that the values of the first derivative at +/-1 are correctly set to zero as intended. This modification will help you enforce the appropriate boundary conditions for the first derivative in your numerical solution code.Hope, now all your issues have been resolved. Please let me know if you have any further questions.
Answers (0)
Categories
Find more on Calculus 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!


