格林函数 G ( r , r ′ ) G(\mathbf{r}, \mathbf{r}') G(r,r′) 是偏微分方程的基本解,满足:
∇ 2 G ( r , r ′ ) = − δ ( r − r ′ ) \nabla^2 G(\mathbf{r}, \mathbf{r}') = -\delta(\mathbf{r} - \mathbf{r}') ∇2G(r,r′)=−δ(r−r′)
最常见的三维自由空间格林函数 为:
G ( r , r ′ ) = 1 4 π ∣ r − r ′ ∣ G(\mathbf{r}, \mathbf{r}') = \frac{1}{4\pi|\mathbf{r} - \mathbf{r}'|} G(r,r′)=4π∣r−r′∣1
MATLAB 绘制代码
1. 三维自由空间格林函数(点源势场)
matlab
%% 三维格林函数可视化
clear; clc; close all;
% 创建网格
x = linspace(-2, 2, 100);
y = linspace(-2, 2, 100);
z = linspace(-2, 2, 100);
[X, Y, Z] = meshgrid(x, y, z);
% 点源位置
x0 = 0; y0 = 0; z0 = 0;
% 计算距离(避免除零)
R = sqrt((X-x0).^2 + (Y-y0).^2 + (Z-z0).^2);
R(R < 0.05) = 0.05; % 正则化
% 格林函数 G = 1/(4*pi*R)
G = 1./(4*pi*R);
%% 方法1: 等值面图
figure('Color', 'white', 'Position', [100 100 800 600]);
isosurface(X, Y, Z, G, 0.5); % 绘制等值面
isonormals(X, Y, Z, G, p);
p.FaceColor = 'red';
p.EdgeColor = 'none';
p.FaceAlpha = 0.8;
camlight;
lighting gouraud;
title('3D Green Function Isosurface');
xlabel('x'); ylabel('y'); zlabel('z');
axis equal; grid on;
%% 方法2: 切片图
figure('Color', 'white', 'Position', [100 100 800 600]);
xslice = 0; yslice = 0; zslice = 0;
slice(X, Y, Z, G, xslice, yslice, zslice);
shading interp;
colorbar;
title('3D Green Function Slices');
xlabel('x'); ylabel('y'); zlabel('z');
axis equal; colormap(jet);
%% 方法3: 二维截面(z=0平面)
figure('Color', 'white', 'Position', [100 100 700 600]);
G_2d = 1./(4*pi*sqrt(X(:,:,50).^2 + Y(:,:,50).^2));
G_2d(G_2d > 2) = 2; % 截断显示
surf(X(:,:,50), Y(:,:,50), G_2d);
shading interp; colorbar;
title('Green Function at z=0 plane');
xlabel('x'); ylabel('y'); zlabel('G');
🍊运行结果:
2. 二维格林函数(对数形式)
二维拉普拉斯方程的格林函数为对数形式:
G ( r , r ′ ) = − 1 2 π ln ∣ r − r ′ ∣ G(\mathbf{r}, \mathbf{r}') = -\frac{1}{2\pi}\ln|\mathbf{r} - \mathbf{r}'| G(r,r′)=−2π1ln∣r−r′∣
matlab
%% 二维格林函数
clear; clc;
% 创建网格
x = linspace(-2, 2, 200);
y = linspace(-2, 2, 200);
[X, Y] = meshgrid(x, y);
% 计算距离
R = sqrt(X.^2 + Y.^2);
R(R < 0.01) = 0.01;
% 二维格林函数 G = -ln(R)/(2*pi)
G2D = -log(R)/(2*pi);
% 绘制
figure('Color', 'white');
surf(X, Y, G2D);
shading interp;
colorbar;
title('2D Green Function: G = -\ln(r)/2\pi');
xlabel('x'); ylabel('y'); zlabel('G');
zlim([-2 2]); % 限制z轴范围
colormap(jet);
view(45, 30);
3. 含时格林函数(波动方程)
matlab
%% 波动方程的含时格林函数
clear; clc;
x = linspace(-5, 5, 300);
t_values = [0.5, 1, 1.5, 2]; % 不同时刻
figure('Color', 'white', 'Position', [100 100 1000 800]);
for i = 1:4
subplot(2, 2, i);
t = t_values(i);
% 一维波动方程格林函数: G = 0.5 * H(t - |x|/c)
% 这里 c=1, H是Heaviside阶跃函数
c = 1;
G_time = 0.5 * double(abs(x) < c*t);
plot(x, G_time, 'b-', 'LineWidth', 2);
title(['t = ' num2str(t)]);
xlabel('x'); ylabel('G(x,t)');
ylim([-0.1 0.6]);
grid on;
end
sgtitle('1D Wave Equation Green Function at Different Times');
4. 热传导方程格林函数(高斯形式)
matlab
%% 热传导方程格林函数(热核)
clear; clc;
x = linspace(-5, 5, 300);
t_values = [0.1, 0.5, 1, 2];
figure('Color', 'white', 'Position', [100 100 1000 800]);
for i = 1:4
subplot(2, 2, i);
t = t_values(i);
% 热核: G = exp(-x^2/(4t)) / sqrt(4*pi*t)
G_heat = exp(-x.^2/(4*t)) / sqrt(4*pi*t);
plot(x, G_heat, 'r-', 'LineWidth', 2);
hold on;
fill(x, G_heat, 'r', 'FaceAlpha', 0.3);
title(['t = ' num2str(t)]);
xlabel('x'); ylabel('G(x,t)');
grid on;
end
sgtitle('Heat Equation Green Function (Heat Kernel)');
5. 高级可视化:3D动画效果
matlab
%% 3D格林函数动画效果(静态展示)
clear; clc;
x = linspace(-3, 3, 100);
y = linspace(-3, 3, 100);
[X, Y] = meshgrid(x, y);
figure('Color', 'white', 'Position', [100 100 900 700]);
% 绘制表面
R = sqrt(X.^2 + Y.^2);
R(R < 0.05) = 0.05;
Z = 1./(4*pi*R);
% 主表面
surf(X, Y, Z, 'EdgeColor', 'none');
hold on;
% 添加等高线投影
contour(X, Y, Z, 10, 'k-', 'LineWidth', 1);
% 添加径向截面线
theta = linspace(0, 2*pi, 8);
for i = 1:8
x_line = 3*cos(theta(i));
y_line = 3*sin(theta(i));
z_line = linspace(0, 2, 50);
plot3(linspace(0,x_line,50), linspace(0,y_line,50), z_line, 'w--');
end
shading interp;
colorbar;
colormap(hot);
caxis([0 0.5]); % 调整颜色范围
title('3D Green Function with Radial Lines');
xlabel('x'); ylabel('y'); zlabel('G');
zlim([0 2]);
view(45, 30);
lighting gouraud;
camlight;
关键要点总结
| 方程类型 | 维度 | 格林函数形式 | MATLAB 特点 |
|---|---|---|---|
| 拉普拉斯方程 | 3D | 1 / ( 4 π r ) 1/(4\pi r) 1/(4πr) | 使用 isosurface 或 slice |
| 拉普拉斯方程 | 2D | − ln ( r ) / ( 2 π ) -\ln(r)/(2\pi) −ln(r)/(2π) | 对数奇异性,需正则化 |
| 波动方程 | 1D/3D | δ ( t − r / c ) \delta(t-r/c) δ(t−r/c) | 显示波前传播 |
| 热传导方程 | 1D/3D | 高斯函数 | 随时间扩散平滑 |
注意事项:
- 奇异性处理 :在 r = 0 r=0 r=0 处设置最小值(如
R(R<0.05)=0.05)避免除零 - 可视化范围 :适当截断颜色范围 (
caxis) 以突出主要特征 - 正则化 :实际计算中可用 1 / r 2 + ϵ 2 1/\sqrt{r^2+\epsilon^2} 1/r2+ϵ2 代替 1 / r 1/r 1/r