我正在嘗試重做泊松方程,以便 ?2Φ /?x2 = 2x2 - 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
再次感謝您在這件事上的任何幫助
uj5u.com熱心網友回復:
問題很清楚:“輸入等式的右側會出現語法錯誤”,要糾正錯誤,您應該正確定義等式的右側:
R(j)=2*x(j)^2-0.5*x(j) exp(x(j));
由于它取決于,因此在計算等式左右兩側的 for 回圈中的系數時x,您應該指向每個元素。j
PDE 的決議解為:

更改這行代碼并繪制離散化和分析解決方案會產生
% Visualize results
figure(1); clf
plot(x,T,'o r');
hold on
plot(x , 0.166667*x.^4-0.0833333*x.^3-268811714181613573321934397213431784538112*x exp(x)-1)

這似乎是正確的。差異是由于離散化本身造成的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/511568.html
標籤:matlabpde
