耦合偏微分方程的FDM错误

时间:2016-11-24 16:33:05

标签: matlab

以下是尝试使用有限差分法求解耦合偏微分方程的代码,

clear;
Lmax = 1.0;           % Maximum length
Wmax = 1.0;            % Maximum wedth
Tmax = 2.;            % Maximum time

% Parameters needed to solve the equation 
K = 30;            % Number of time steps
n = 3;            % Number of space steps
m =30;             % Number of space steps
M = 2;
N = 1;
Pr = 1;
Re = 1;
Gr = 5;
maxn=20;                   % The wave-front: intermediate point from which u=0
maxm = 20;
maxk = 20;
dt = Tmax/K;
dx = Lmax/n;
dy = Wmax/m;

%M = a*B1^2*l/(p*U)
b =1/(1+M*dt);
c =dt/(1+M*dt);
d = dt/((1+M*dt)*dy);
%Gr = gB*(T-T1)*l/U^2;
% Initial value of the function u (amplitude of the wave)
for i = 1:n
if i < maxn
u(i,1)=1.;
else
u(i,1)=0.;
end
x(i) =(i-1)*dx;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j = 1:m
if j < maxm
v(j,1)=1.;
else
v(j,1)=0.;
end
y(j) =(j-1)*dy;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k = 1:K
if k < maxk
T(k,1)=1.;
else
T(k,1)=0.;
end
z(k) =(k-1)*dt;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Value at the boundary
%for k=0:K


%end
% Implementation of the explicit method
for k=0:K    % Time loop
for i=1:n    % Space loop
for j=1:m    
u(i,j,k+1) = b*u(i,j,k)+c*Gr*T(i,j,k+1)+d*[((u(i,j+1,k)-u(i,j,k))/dy)^(N-1)*((u(i,j+1,k)-u(i,j,k))/dy)]-d*[((u(i,j,k)-u(i,j-1,k))/dy)^(N-1)*((u(i,j,k)-u(i,j-1,k))/dy)]-d*[u(i,j,k)*((u(i,j,k)-u(i-1,j,k))/dx)+v(i,j,k)*((u(i,j+1,k)-u(i,j,k))/dy)];
v(i,j,k+1) = dy*[(u(i-1,j,k+1)-u(i,j,k+1))/dx]+v(i,j-1,k+1);
T(i,j,k+1) = T(i,j,k)+(dt/(Pr*Re))*{(T(i,j+1,k)-2*T(i,j,k)+T(i,j-1,k))/dy^2-Pr*Re{u(i,j,k)*((T(i,j,k)-T(i-1,j,k))/dx)+v(i,j,k)*((T(i,j+1,k)-T(i,j,k))/dy)}};
end
end
end
% Graphical representation of the wave at different selected times
plot(x,u(:,1),'-',x,u(:,10),'-',x,u(:,50),'-',x,u(:,100),'-')
title('graphs')
xlabel('X')
ylabel('Y')

但我收到此错误

  

下标索引必须是实数正整数或逻辑。

我正在尝试实现这一点 enter image description here

有边界条件

enter image description here

有人可以帮帮我! 感谢

1 个答案:

答案 0 :(得分:6)

说实话,看起来你开始时头脑中的东西已经开始了,只需一次性输入所有内容而不用多想,现在你感到惊讶它不起作用......

将来,请将这些问题分解为 waaaay 小块,你可以单独绘制,检查,测试等等。更好的是,首先尝试更简单的问题(波动方程,热方程, ...),逐步努力达到这个目标。

我这么严厉地说,因为你的代码有很多相当基本的错误:

  • 您使用大括号({})和方括号([])完全按照等式编写。在MATLAB中,大括号是一个名为单元数组的特殊容器对象的构造函数,括号用于构造数组和矩阵。要对等式中的事物进行分组,您必须使用括号(())。
  • 你有很多错误的括号,当我重新分组并将那些巨大的难以理解的行分解成人类可以理解的多行时,这一点就显而易见了
  • 你忘了在u
  • 的3 rd 和4 th 条款中取绝对值
  • 您浏览了k = 0:Kj = 1:m,然后使用kj-1快乐地为所有内容编制索引。 MATLAB是基于1的,意思是,任何东西的第一个元素是元素1,索引为0是错误
  • 您已初始化了3个向量uvT,但随后将这些向量编入索引,就像它们是3D数组一样

现在,我已经设法提出以下代码,它运行正常,至少或多或少与所示方程一致。但我认为它仍然没有多大意义,因为我只得到零(除了初始值)。

但是,通过这些反馈,您应该能够纠正任何问题。

Lmax = 1.0;  % Maximum length
Wmax = 1.0;  % Maximum wedth
Tmax = 2.;   % Maximum time

% Parameters needed to solve the equation 
K  = 30;  % Number of time steps
n  = 3;   % Number of space steps
m  = 30;  % Number of space steps
M  = 2;
N  = 1;
Pr = 1;
Re = 1;
Gr = 5;

maxn = 20;  % The wave-front: intermediate point from which u=0
maxm = 20;
maxk = 20;

dt = Tmax/K;
dx = Lmax/n;
dy = Wmax/m;

%M = a*B1^2*l/(p*U)
b = 1/(1+M*dt);
c = dt/(1+M*dt);
d = dt/((1+M*dt)*dy);
%Gr = gB*(T-T1)*l/U^2;

% Initial value of the function u (amplitude of the wave)
u = zeros(n,m,K+1);
x = zeros(n,1);
for i = 1:n
    if i < maxn
        u(i,1)=1.;
    else
        u(i,1)=0.;
    end
    x(i) =(i-1)*dx;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
v = zeros(n,m,K+1);
y = zeros(m,1);
for j = 1:m
    if j < maxm
        v(1,j,1)=1.;
    else
        v(1,j,1)=0.;
    end
    y(j) =(j-1)*dy;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
T = zeros(n,m,K+1);
z = zeros(K,1);
for k = 1:K
    if k < maxk
        T(1,1,k)=1.;
    else
        T(1,1,k)=0.;
    end
    z(k) =(k-1)*dt;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Value at the boundary
%for k=0:K


%end
% Implementation of the explicit method
for k = 2:K      % Time loop
    for i = 2:n  % Space loop
        for j = 2:m-1    
            u(i,j,k+1) = b*u(i,j,k) + ...
                         c*Gr*T(i,j,k+1) + ...
                         d*(abs(u(i,j+1,k) - u(i,j  ,k))/dy)^(N-1)*((u(i,j+1,k) - u(i,j  ,k))/dy) - ...
                         d*(abs(u(i,j  ,k) - u(i,j-1,k))/dy)^(N-1)*((u(i,j  ,k) - u(i,j-1,k))/dy) - ...
                         d*(u(i,j,k)*((u(i,j  ,k) - u(i-1,j,k))/dx) +...
                            v(i,j,k)*((u(i,j+1,k) - u(i  ,j,k))/dy));

        v(i,j,k+1) = dy*(u(i-1,j,k+1)-u(i,j,k+1))/dx + ...
                     v(i,j-1,k+1);

        T(i,j,k+1) = T(i,j,k) + dt/(Pr*Re) * (...
                     (T(i,j+1,k) - 2*T(i,j,k) + T(i,j-1,k))/dy^2 - Pr*Re*(...
                         u(i,j,k)*((T(i,j,k) - T(i-1,j,k))/dx) + v(i,j,k)*((T(i,j+1,k) - T(i,j,k))/dy))...
                     );
        end
    end
end

% Graphical representation of the wave at different selected times
figure, hold on
plot(x, u(:,  1), '-',...
     x, u(:, 10), '-',...
     x, u(:, 50), '-',...
     x, u(:,100), '-')
title('graphs')
xlabel('X')
ylabel('Y')