MATLAB Answers

Why am I getting the error "Array indices must be positive integers or logical values."?

907 ビュー (過去 30 日間)
Yogesh Pai
Yogesh Pai 2018 年 12 月 5 日
回答済み: henry espinoza 2020 年 6 月 19 日
I want to compare the exponential of matrix A obtained by the summation formula and the one with the eigen values. However, I'm getting the following error when exponential is computed with the diagonalised form (E here).
"Array indices must be positive integers or logical values."
A = [1,2,1,3; 3,1,2,0; 2,2,1,4; 1,3,2,1];
[V,D] = eig(A);
exp_A = 0;
for k = 0:100
exp_A = exp_A + (A^k)/factorial(k);
end
disp(exp_A);
E = V*(exp(D))\V;
disp(E);

  3 件のコメント

madhan ravi
madhan ravi 2018 年 12 月 5 日
to be frank I didnt get any error running your code
Dawit Alemayehu
Dawit Alemayehu 2020 年 5 月 25 日
No Error when I run your code; And this was the out put; >> A = [1,2,1,3; 3,1,2,0; 2,2,1,4; 1,3,2,1]; [V,D] = eig(A); exp_A = 0; for k = 0:100 exp_A = exp_A + (A^k)/factorial(k); end disp(exp_A); E = V*(exp(D))\V; disp(E); 304.3054 340.7344 263.9243 320.0369 275.4796 308.2690 238.7711 289.0775 389.2552 436.7654 339.1998 410.7063 309.9581 347.7556 269.7020 326.5840
Columns 1 through 2
0.0008 - 0.0000i -0.0001 - 0.0000i
-0.0001 - 0.0000i -0.8364 - 0.1567i
-0.0001 + 0.0000i 0.1039 + 0.0000i
-0.0007 - 0.0000i 0.8393 + 0.1795i
Columns 3 through 4
-0.0001 + 0.0000i -0.0007 + 0.0000i
0.1039 - 0.0000i 0.8393 + 0.1795i
-0.8364 + 0.1567i 0.8393 - 0.1795i
0.8393 - 0.1795i -0.7766 + 0.0000i
>> disp(exp_A) 304.3054 340.7344 263.9243 320.0369 275.4796 308.2690 238.7711 289.0775 389.2552 436.7654 339.1998 410.7063 309.9581 347.7556 269.7020 326.5840 I suggest running on different machine. Good Luck!

Sign in to comment.

採用された回答

Kelly Kearney
Kelly Kearney 2018 年 12 月 5 日
My guess is you have a variable named eig sitting in your workspace. So when you try to call the eig function, the variable is overshadowing it and matlab is trying to extract the 1st, 2nd, 1st, 3rd, etc. elements from the eig array. The attempt to extract the 0th element leads to the error.
Always be careful with your choice of variable names!

  1 件のコメント

Yogesh Pai
Yogesh Pai 2018 年 12 月 6 日
You were right. There was an old value stored in a variable 'exp'. Thanks!

Sign in to comment.

その他の回答 (1 件)

henry espinoza
henry espinoza 2020 年 6 月 19 日
Array indices must be positive integers or logical values.
Error in Tarea3_HE (line 15)
Xc1 = (-1 /(PRM(u)*(w)))*j

  0 件のコメント

Sign in to comment.

製品


Translated by