Incorrect Dimensions for matrix multiplication
197 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (3)
James Tursa
il 13 Ott 2023
Modificato: James Tursa
il 13 Ott 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
0 Commenti
Sam Chak
il 13 Ott 2023
Modificato: Sam Chak
il 13 Ott 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')
0 Commenti
Torsten
il 13 Ott 2023
Modificato: Torsten
il 13 Ott 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:
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!