Incorrect Dimensions for matrix multiplication

196 ビュー (過去 30 日間)
Aijalon Marsh
Aijalon Marsh 2023 年 10 月 13 日
編集済み: Sam Chak 2023 年 10 月 13 日
Ld=(0:0.001:5);
L=0.007;
Ta=25;
h=375;
Kf=175;
Kd=0.032;
Tb=75;
Ap=3.02*10^-3;
N=238;
Ac=10^-6;
m=92.58;
Lf=L-Ld;
Rtf=(cosh(m*Lf)+(h/(m*Kf))*sinh(m*Lf))/(sqrt(4*h*(1)^3)*(sinh(m*Lf)+h/(m*Kf))*cosh(m*Lf));
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.
disp(Rtf)
why do i keep getting an error on the second to last line?

回答 (3 件)

James Tursa
James Tursa 2023 年 10 月 13 日
編集済み: James Tursa 2023 年 10 月 13 日
Because you are using matrix operators instead of element-wise operators. Try this:
Rtf=(cosh(m.*Lf)+(h./(m.*Kf)).*sinh(m.*Lf))./(sqrt(4*h.*(1)^3).*(sinh(m.*Lf)+h./(m.*Kf)).*cosh(m.*Lf));
Notice the dots. * and / are matrix operations, and .* and ./ are element-wise operations.
Not sure why you have (1)^3

Sam Chak
Sam Chak 2023 年 10 月 13 日
編集済み: Sam Chak 2023 年 10 月 13 日
Ld=(0:0.001:5); % <-- array is first defined here, but does not appear in Rtf
L=0.007;
Ta=25;
h=375;
Kf=175;
Kd=0.032;
Tb=75;
Ap=3.02*10^-3;
N=238;
Ac=10^-6;
m=92.58;
Lf=L-Ld; % <-- Lf array is derived from Ld array, and it appears in Rtf
% To perform element-wise division & multiplication of arrays, the dots (.) are in indicated below
Rtf=(cosh(m*Lf)+(h/(m*Kf))*sinh(m*Lf))./(sqrt(4*h*(1)^3)*(sinh(m*Lf)+h/(m*Kf)).*cosh(m*Lf));
% ^ ^
% disp(Rtf)
plot(Ld, Rtf), grid on, xlabel('Ld'), ylabel('Rtf')

Torsten
Torsten 2023 年 10 月 13 日
編集済み: Torsten 2023 年 10 月 13 日
You multiply and divide arrays elementwise when you compute Rtf. Thus you have to use elementwise multiplication (.*) and elementwise division (./) :
Rtf=(cosh(m*Lf)+(h/(m*Kf))*sinh(m*Lf))./(sqrt(4*h*(1)^3)*(sinh(m*Lf)+h/(m*Kf)).*cosh(m*Lf));
instead of
Rtf=(cosh(m*Lf)+(h/(m*Kf))*sinh(m*Lf))/(sqrt(4*h*(1)^3)*(sinh(m*Lf)+h/(m*Kf))*cosh(m*Lf));
Take a look here for more details:

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by