Error Matrix must dimensions agree HELP!

1 回表示 (過去 30 日間)
Humphrey Dogbe
Humphrey Dogbe 2021 年 3 月 16 日
回答済み: Deepak Meena 2021 年 3 月 21 日
Get this error when running the code. Hbar, nbasis and mu are constants.
T = zeros(length(n));
n = linspace(1, nbasis, nbasis);
for j = 1:length(n)
for k = 1:length(n)
funT = @(x) (sqrt(.5).* sin(((j.*pi).*(x-0.6))./4)).* ((hbar.^2)./mu).* (sqrt(0.5).* ((n.^2 * pi.^2)./16) .* -sin((n.*pi).*(x-0.6))./4).* (sqrt(0.5).*sin(((k.*pi)).*(x-0.6))./4);
T(j,k) = integral(@(x) funT(x), 0.6, 4.6);
end
end
Please point me in the right direction, thank you!
  2 件のコメント
Geoff Hayes
Geoff Hayes 2021 年 3 月 16 日
Humphrey - please copy and paste the full error message. Also, is the hbar a constant array or a constant scalar? I suspect that part of the problem might be with x and n being of different dimensions as the latter is dependent upon nbasis. Do you mean for n to be an array in your funT ?
Humphrey Dogbe
Humphrey Dogbe 2021 年 3 月 16 日
Hbar is a constant scalar. n is meant to be an array from 1 to 4. The n pasted above is referenced from an earlier point in the code

サインインしてコメントする。

回答 (1 件)

Deepak Meena
Deepak Meena 2021 年 3 月 21 日
Hi ,
The error is coming because the size of the left side is 1-by-1 and the size of the right side is
1-by-4.
Also Change your integral line to this :
%code
P0 = integral( funT, 0.6, 4.6, 'ArrayValued', true );
I am storing the result of the integral in a temporary variable , you can store it as per your convenience
Thanks

カテゴリ

Help Center および File ExchangeMathematics and Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by