Incorrect Dimensions for matrix multiplication

197 views (last 30 days)
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?

Answers (3)

James Tursa
James Tursa on 13 Oct 2023
Edited: James Tursa on 13 Oct 2023
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 on 13 Oct 2023
Edited: Sam Chak on 13 Oct 2023
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 on 13 Oct 2023
Edited: Torsten on 13 Oct 2023
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:

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!