Ошибка при использовании vertcat-Размеры сцепляемых матриц не согласованы - PullRequest
0 голосов
/ 15 мая 2019

Я новичок в 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 > 0 и c>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];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...