使用vertcat时出错-所连接矩阵的维数不一致

时间:2019-05-14 22:21:33

标签: matlab

我是Matlab的新手,正在尝试实现此方案。

Scheme: Un+1 = Minv*((1+2theta)*Un + *Un-1 +bn+1 + fn+1

给出以下等式:

ut(x; t) = cuxx(x; t) + f(x; t)

其中x属于]0; 1[t属于]0; T[T > 0c>0 具有狄里克雷边界 条件:

u(0; t) = alpha(t)
u(1; t) = beta(t)
t belongs to [0; T]

该函数在代码中的目标是确定错误(在代码中定义)。

我不断得到:

Error using vertcat
Dimensions of matrices being concatenated are not consistent.

Error in wave1d_func11910448 (line 60)
uh=[UL;U;UR];

代码:

function [ h,err ] = wave1d_func11910448( nx )
a=0.0; b=1.0;
nx=20;
c=1.0;
tfinal=1;
h=(b-a)/(nx-1);
dt=0.5*h*h/c;
lambda=c*dt/h/h;
theta=1;
x=linspace(a,b,nx);

   %defining the function f(x,t)
f = @(x,t) (pi^2-1)*exp(-t)*cos(pi*x)+4*x-2;
Fh=zeros(nx-2,1);%filling the vector f(n+1)

   %defining the function g(x,t):initial condition-filling the vector U0 
u = @(x) x.^2+cos(pi*x);
U0=u(x(2:end-1));

   %defining the exact solution exact(x,t)
exact=@(x,t) x.^2+4*x*t+cos(pi*x)*exp(-t);

   %filling the vector u1
U1=exact(x(2:end-1),1);

   %defining the functions alpha(t) and beta(t)
alpha = @(t) exp(-t);
beta = @(t) -exp(-t)+4*t+1; 

    %filling the matrix Mh
Ah=diag(0.0*ones(1,nx-2),0)+diag(1.0*ones(1,nx-3),1)+diag(1.0*ones(1,nx-    
  3),-1);
Mh=eye(nx-2,nx-2)*(1+theta+2*lambda)-lambda*Ah;
Minv=inv(Mh);

Bh = zeros(nx-2,1);%filling the vector b
time=0.0;
i=0.0;
U2=Minv.*((1+2*theta )*U1-theta*U0+Bh+ dt*Fh);
while(time<tfinal)
 time=time+dt;
 i=i+1;
 Bh(1)=lambda*alpha(time);Bh(nx-2)=lambda*beta(time);
 Fh=f(x(2:end-1),time);
 U=Minv.*((1+2*theta )*U1-theta*U0+Bh+ dt*Fh);
 U0=U1;
 U1=U2;
 UL=alpha(time);
 UR=beta(time);
 uh=[UL;U;UR];
 error(i) = norm(uh - exact(x,time),inf); 
end
err=max(error);


expected results: err= a positive number <1

error message:

Error using vertcat
Dimensions of matrices being concatenated are not consistent.

Error in wave1d_func11910448 (line 60)
uh=[UL;U;UR];

0 个答案:

没有答案