2011-12-08 29 views
1

我需要幫助瞭解代碼以及溫度TN是如何計算/存儲的。具體來說,我不明白以開始的雙循環,而k < = imax。這裏的Matlab程序解決2-d拉普拉斯方程明確:關於Matlab拉普拉斯方程的說明

function [x,y,T]= LaplaceExplicit(n,m,Dx,Dy) 
echo off; 
numgrid(n,m); 
R = 5.0; 
T = R*ones(n+1,m+1); % All T(i,j) = 1 includes all boundary conditions 
x = [0:Dx:n*Dx];y=[0:Dy:m*Dy]; % x and y vectors 
for i = 1:n % Boundary conditions at j = m+1 and j = 1 
    T(i,m+1) = T(i,m+1)+ R*x(i)*(1-x(i)); 
    T(i,1) = T(i,1) + R*x(i)*(x(i)-1); 
end; 
TN = T; % TN = new iteration for solution 
err = TN-T; 
% Parameters in the solution 
beta = Dx/Dy; 
denom = 2*(1+beta^2); 
% Iterative procedure 
epsilon = 1e-5; % tolerance for convergence 
imax = 1000; % maximum number of iterations allowed 
k = 1; % initial index value for iteration 
% Calculation loop **IMPORTANT PART** 

**while k<= imax 
    for i = 2:n 
     for j = 2:m 
     TN(i,j)=(T(i-1,j)+T(i+1,j)+beta^2*(T(i,j-1)+T(i,j+1)))/denom; 
     err(i,j) = abs(TN(i,j)-T(i,j)); 
     end; 
    end; **/IMPORTANT PART** 

T = TN; k = k + 1; 
errmax = max(max(err)); 
if errmax < epsilon 
[X,Y] = meshgrid(x,y); 
figure(2);contour(X,Y,T',20);xlabel('x');ylabel('y'); 
title('Laplace equation solution - Dirichlet boundary conditions 
- Explicit'); 
figure(3);surfc(X,Y,T');xlabel('x');ylabel('y');zlabel('T(x,y)'); 
title('Laplace equation solution - Dirichlet boundary conditions 
- Explicit'); 
fprintf('Convergence achieved after %i iterations.\n',k); 
fprintf('See the following figures:\n'); 
fprintf('==========================\n'); 
fprintf('Figure 1 - sketch of computational grid \n'); 
fprintf('Figure 2 - contour plot of temperature \n'); 
fprintf('Figure 3 - surface plot of temperature \n'); 
return 
end; 
end; 
fprintf('\n No convergence after %i iterations.',k); 

這裏的source。我還需要下面顯示的圖中節點處的溫度。

enter image description here

+1

你能更具體地瞭解你不明白的東西嗎?我的意思是,如果它是背後的數學,你應該在http://math.stackexchange.com/上發佈這個問題。這是一個迭代過程,'while'循環只遍歷所有迭代。 for循環計算每個空間(x,y)點的「TN」值。 – Smash

回答

1

這是一個卷積由拉普拉斯濾波器:

[ 0 0.25 0 ;... 
0.25 0 0.25;... 
    0 0.25 0 ]; 

這是拉普拉斯算子的approximiation。

欲瞭解更多信息,這是關於物理。