我试着重做泊松方程,这样∂?∂/Φx?=2x?-0.5x+exp(X)。每次我尝试输入公式的右侧时,它都会出现语法错误,如果有任何帮助,我将非常感激。
% Solving of 1D Poisson equation
% using finite differences
% Clearing memory and figures
clear all
clf
% Define 1D numerical model
xsize=100; % Model size in horizontal direction, m
Nx=101; % Number of grid points
dx=xsize/(Nx-1); % Gridstep, m
x=0:dx:xsize; % Coordinates of grid points, m
% Defining global matrixes
L=zeros(Nx,Nx); % Koefficients in the left part
R=zeros(Nx,1); % Right parts of equations
% Composing global matrixes
% by going through all grid points
for j=1:1:Nx
% Discriminating between BC-points and internal points
if(j==1 || j==Nx)
% BC-points
L(j,j)=1; % Left part
R(j)=0; % Right part
else
% Composing Poisson eq. d2T/dx2=1
%
% ---T(j-1)------T(j)------T(j+1)---- STENCIL
%
% 1/dx2*T(j-1)+(-2/dx2)*T(j)+1/dx2*T(j+1)=1
%
% Left part of j-equation
L(j,j-1)=1/dx^2; % T(j-1)
L(j,j)=-2/dx^2; % T(j)
L(j,j+1)=1/dx^2; % T(j+1)
% Right part
R(j)=2x;
end
end
% Solve the matrix
S=L\R;
% Reload solutions
T=zeros(1,Nx); % Create array for temperature, K
for j=1:1:Nx
T(j)=S(j);
end
% Visualize results
figure(1); clf
plot(x,T,'o r') % Plotting results
我再次感谢在这件事上的任何帮助
1条答案
按热度按时间2mbi3lxu1#
问题很明显:“在公式的右侧输入它出现语法错误”,要纠正错误,您应该正确定义公式的右侧:
因为它取决于
x
,所以在计算公式左侧和右侧的for循环中的系数时,您应该指向每个元素j
。偏微分方程解析解为:
更改这行代码并绘制离散化和解析的解决方案会产生
这似乎是正确的。不同之处在于离散化本身。