Error with matrix size
12 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Stephen23
2023 年 11 月 25 日
移動済み: the cyclist
2023 年 11 月 25 日
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
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!