電磁場有限元方法EX2.2-里茲法求解泊松方程控制的邊值問題
簡單學習一下有限元法的基礎理論,書本為電磁場有限元經典教材:
THE FINITE ELEMENT METHOD IN ELECTROMAGNETICS, JIAN-MING JIN
目錄
- 電磁場有限元方法EX2.2-里茲法求解泊松方程控制的邊值問題
- 1、問題定義
- 1.1、英文描述
- 1.2、中文描述
- 2、解題
- 2.1、解題思路
- 2.2、Matlab代碼實現
- 2.2.1 定義試探函數和變分問題
- 2.2.2 變分F對c1、c2變量求偏導
- 2.2.3 F對c1、c2變量偏導為0,建立方程并求解
- 2.2.4 對比理論解和數值解,并繪圖
- 2.3、結果對比
- 3、全部代碼
- 4、變分公式的證明
1、問題定義
1.1、英文描述
1.2、中文描述
給定由方程定義的邊值問題:
d 2 ? d x 2 + 2 ? = 0 ( 0 < x < 1 ) \frac{\mathrm{d}^{2}\phi}{\mathrm{d}x^{2}} + 2\phi = 0 \quad (0 < x < 1) dx2d2??+2?=0(0<x<1)
邊界條件:
? ∣ x = 0 = 0 和 ? ∣ x = 1 = 1 \left.\phi\right|_{x = 0} = 0 \quad \text{和} \quad \left.\phi\right|_{x = 1} = 1 ?∣x=0?=0和?∣x=1?=1
證明該問題的泛函 F F F為:
F ( ? ~ ) = 1 2 ∫ 0 1 [ ( d ? ~ d x ) 2 ? 2 ? ~ 2 ] d x F(\tilde{\phi}) = \frac{1}{2} \int_{0}^{1} \left[ \left( \frac{\mathrm{d} \tilde{\phi}}{\mathrm{d} x} \right)^{2} - 2 \tilde{\phi}^{2} \right] \mathrm{d}x F(?~?)=21?∫01? ?(dxd?~??)2?2?~?2 ?dx
應用里茨方法,使用試探函數:
? ~ ( x ) = x + ∑ n = 1 N c n sin ? ( n π x ) ( 取? N = 2 ) \tilde{\phi}(x) = x + \sum_{n=1}^{N} c_{n} \sin(n\pi x) \quad (\text{取 } N = 2) ?~?(x)=x+n=1∑N?cn?sin(nπx)(取?N=2)
求近似解,并將結果與精確解:
? = sin ? ( 2 x ) sin ? 2 \phi = \frac{\sin(\sqrt{2}\, x)}{\sin\sqrt{2}} ?=sin2?sin(2?x)?
進行比較。
2、解題
2.1、解題思路
這題比較簡單,使用經典里茲方法,將試探函數 ? ~ ( x ) \tilde{\phi}(x) ?~?(x)帶入泛函 F F F中,然后對泛函 F F F求c1和c2的偏導即可( F F F對c1,c2的偏導為0,以此求得極小值)。變分公式的證明見最后。
下面使用matlab實現代碼思路
2.2、Matlab代碼實現
2.2.1 定義試探函數和變分問題
定義試探函數和變分問題,并將試探函數帶入變分:
%% Solve
syms x c1 c2
% define the trail function and variational functional
phi = x+c1*sin(pi*x) +c2*sin(2*pi*x);
F=0.5*int((diff(phi, x))^2-2*(phi^2),x,0,1);
disp('F=');
pretty(simplify(F))
2.2.2 變分F對c1、c2變量求偏導
% Find the extreme value of the functional. calculate the partial derivative of c
diff_c1 = diff(F, c1); % ?F/?c1
diff_c2 = diff(F, c2); % ?F/?c2
2.2.3 F對c1、c2變量偏導為0,建立方程并求解
% Solve the system of equations for variables [c1, c2]
equations = [diff_c1 == 0, diff_c2 == 0];
solutions = solve(equations, [c1, c2]);
disp('Solution:');
% Extract all solutions for c1 and c2
c1_val = double(solutions.c1);
c2_val = double(solutions.c2);
% Display results (both symbolic and numeric forms)
disp(['c1 = ', num2str(c1_val)]);
disp(['c2 = ', num2str(c2_val)]);
2.2.4 對比理論解和數值解,并繪圖
phi_curve=subs(phi, {c1, c2}, {c1_val, c2_val});
phi_curve_exact=sin(sqrt(2)*x)/sin(sqrt(2));%% Draw figure
figure(1)
fplot(phi_curve, [0, 1], ...'Color', [0, 0.4470, 0.7410], ... % MATLAB經典藍'LineStyle', '--', ... % 虛線'LineWidth', 2.5, ...'Marker', 'o', ... % 圓形標記'MarkerSize', 7, ...'MarkerFaceColor', 'auto', ... % 自動填充'MarkerEdgeColor', 'k', ... % 黑色邊框'DisplayName', sprintf('Numerical: $\\phi(x) = x + %.2f\\sin(\\pi x) + %.2f\\sin(2\\pi x)$', c1_val, c2_val));hold on
fplot(phi_curve_exact, [0, 1], ...'Color', [0.8500, 0.3250, 0.0980], ... % MATLAB經典紅'LineStyle', '-', ... % 實線'LineWidth', 2.5, ...'Marker', 's', ... % 正方形標記'MarkerSize', 8, ...'MarkerFaceColor', 'auto', ... % 自動填充'DisplayName', 'Exact: $\frac{\sin(\sqrt{2} x)}{\sin(\sqrt{2})}$');xlabel('Position (x)', 'FontSize', 14);
ylabel('Function Value \phi(x)', 'FontSize', 14);
title('Comparison of Numerical and Exact Solutions', 'FontSize', 16);
xlim([0, 1]);legend('Interpreter', 'latex', 'Location', 'best', 'FontSize', 12);set(gca, 'FontSize', 12, 'GridAlpha', 0.3, 'Box', 'on');
grid minor;
2.3、結果對比
可以看到數值解和解析解是幾乎一致的:
3、全部代碼
% Exercise 2.2 THE FINITE ELEMENT METHOD IN ELECTROMAGNETICS JIN JIAN MINGclc
clear
%% Solve
syms x c1 c2
% define the trail function and variational functional
phi = x+c1*sin(pi*x) +c2*sin(2*pi*x);
F=0.5*int((diff(phi, x))^2-2*(phi^2),x,0,1);
disp('F=');
pretty(simplify(F))% Find the extreme value of the functional. calculate the partial derivative of c
diff_c1 = diff(F, c1); % ?F/?c1
diff_c2 = diff(F, c2); % ?F/?c2% Solve the system of equations for variables [c1, c2]
equations = [diff_c1 == 0, diff_c2 == 0];
solutions = solve(equations, [c1, c2]);
disp('Solution:');
% Extract all solutions for c1 and c2
c1_val = double(solutions.c1);
c2_val = double(solutions.c2);
% Display results (both symbolic and numeric forms)
disp(['c1 = ', num2str(c1_val)]);
disp(['c2 = ', num2str(c2_val)]);phi_curve=subs(phi, {c1, c2}, {c1_val, c2_val});
phi_curve_exact=sin(sqrt(2)*x)/sin(sqrt(2));%% Draw figure
figure(1)
fplot(phi_curve, [0, 1], ...'Color', [0, 0.4470, 0.7410], ... % MATLAB經典藍'LineStyle', '--', ... % 虛線'LineWidth', 2.5, ...'Marker', 'o', ... % 圓形標記'MarkerSize', 7, ...'MarkerFaceColor', 'auto', ... % 自動填充'MarkerEdgeColor', 'k', ... % 黑色邊框'DisplayName', sprintf('Numerical: $\\phi(x) = x + %.2f\\sin(\\pi x) + %.2f\\sin(2\\pi x)$', c1_val, c2_val));hold on
fplot(phi_curve_exact, [0, 1], ...'Color', [0.8500, 0.3250, 0.0980], ... % MATLAB經典紅'LineStyle', '-', ... % 實線'LineWidth', 2.5, ...'Marker', 's', ... % 正方形標記'MarkerSize', 8, ...'MarkerFaceColor', 'auto', ... % 自動填充'DisplayName', 'Exact: $\frac{\sin(\sqrt{2} x)}{\sin(\sqrt{2})}$');xlabel('Position (x)', 'FontSize', 14);
ylabel('Function Value \phi(x)', 'FontSize', 14);
title('Comparison of Numerical and Exact Solutions', 'FontSize', 16);
xlim([0, 1]);legend('Interpreter', 'latex', 'Location', 'best', 'FontSize', 12);set(gca, 'FontSize', 12, 'GridAlpha', 0.3, 'Box', 'on');
grid minor;