# Error with matrix size

3 visualizaciones (últimos 30 días)
VINH el 25 de Nov. de 2023
Movida: the cyclist el 25 de Nov. de 2023
I have a very confusing problem when working on Matlab Web version, by checking that the matrix A has a size of 9x9 but when running the program, the compiler reports an error at line 43 that the size of the matrix A is is 1x0. Please help me. Thank.
Here is my entire code:
clear all
close all
h=0.1;
k=0.01;
x=0:h:1;
n=length(x);
t=0:k:0.01;
m=length(t);
c=1;
r=(c^2*k)/h^2;
%Khai báo hàm điều kiện đầu
f=@(x) sin(pi*x)+sin(3*pi*x);
%khai báo hàm điều kiện biên
g=@(t) 0;
%khởi tạo ma trận mạng lưới
u=zeros(m,n);
%Khai báo điều kiện đầu cho mạng lưới
for j=2:n-1
u(1,j)= f(x(j));
end
%Khai báo điều kiện biên cho 2 bên lưới
for i=1:m
u(i,1)= g(x(i));
u(i,n)= g(x(i));
end
d1=zeros(n-2,1);
d2=zeros(n-2,1);
for i=1:n-2
d2(i,1)=-1;
d1(i,1)=2/r+2;
end
A=spdiags([d2 d1 d2],-1:1,n-2,n-2);
B=zeros(m-1,n-2);
for i=2:11
for j=2:n-1
B(j-1,i-1)=r*u(i-1,j-1)+(2-2*r)*u(i-1,j)+r*u(i-1,j+1)+r*u(i,j-1);
end
u(i,2:(m-1))=A\B(:,i-1);
end
surf(u)
colorbar
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Stephen23 el 25 de Nov. de 2023
Movida: the cyclist el 25 de Nov. de 2023
The size of the matrix A is not important. The size of the RHS of the "=" is important. Lets check that size:
A = rand(9,9);
B = rand(9,1);
A\B(:,1) % this is what you actually have on the RHS, a 9x1 array
ans = 9×1
0.7286 16.1988 2.4267 -8.6586 -11.1960 8.6900 -12.4435 -5.6568 3.2342
Apparently the colon on the LHS evaluates to an empty list (most likely because m<3), so the indexing on the LHS is allocating to a 1x0 area of u. So far everything is very simple to explain, there are no surprises here.
"I have a very confusing problem when working on Matlab Web version"
It is unclear what the confusion is: the RHS is 9x1 and the LHS is 1x0. It is not possible to allocate 9 element into 0 elements.
The only confusing thing seems to be that you have not checked to see what your code is actually doing.
" the compiler reports an error at line 43 that the size of the matrix A is is 1x0."
No, it definitely does not say that. Read it again: the error message does not mention the array "A" anywhere. The RHS is always the result of the final operation performed, which in this case is an MLDIVIDE operation, and not your array A.
##### 3 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
VINH el 25 de Nov. de 2023
Movida: the cyclist el 25 de Nov. de 2023
I figured it out, I mistakenly typed an additional 0 in the t array declaration line, it should be t=0:k:0.1, it's just a simple thing but it took me a day to think about it without considering these things. The simple thing
VINH el 25 de Nov. de 2023
Movida: the cyclist el 25 de Nov. de 2023
Your comment was really helpful, I didn't realize the matrix u didn't have enough size to fit the right side. Thank you for taking the time to answer my stupid question.

Iniciar sesión para comentar.

### Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

R2023b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by