Incorrect Dimensions for matrix multiplication
조회 수: 198 (최근 30일)
이전 댓글 표시
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));
disp(Rtf)
why do i keep getting an error on the second to last line?
댓글 수: 0
답변 (3개)
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
댓글 수: 0
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')
댓글 수: 0
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:
댓글 수: 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!