Error using * for matrix multiplication

1 次查看(过去 30 天)
I'm trying to write a script for an engineering class, but I'm getting the following error message:
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.
Error in cegr_454_optimizer (line 90)
qu = Nc*c*Fcs.*Fcd*Fci.*Fcc + gamma_a.*df*Nq*Fqs.*Fqd*Fqi*Fqc + 0.5*b*gamma_b*Ng*Fgs*Fgd*Fgi*Fgc;
I've done my best to check and re-check to make sure I'm using elementwise multiplication, but I'm still getting the message. Can anyone figure out what I'm doing wrong?
Thanks in advacen for your help!
%Excerpt from Table 6.2:
bcf = [20, 14.83, 6.40, 5.39; 21, 15.82, 7.07, 6.20; 22, 16.88, 7.82, 7.13; 23, 18.05, 8.66, 8.20; 24, 19.32, 9.60, 9.44; 25, 20.72, 10.66, 10.88; 26, 22.25, 11.85, 12.54; 27, 23.94, 13.20, 14.47; 28, 25.80, 14.72, 16.72; 29, 27.86, 16.44, 19.34; 30, 30.14, 18.40, 22.40; 31, 32.67, 20.63, 25.99; 32, 35.49, 23.18, 30.22; 33, 38.64, 26.09, 35.19; 34, 42.16, 29.44, 41.06; 35, 46.12, 33.30,48.03; 36, 50.59, 37.75, 56.31; 37, 55.63, 42.92, 66.19; 38, 61.35, 48.93, 78.03; 39, 67.87, 55.96, 92.25];
%User inputs:
fprintf('\n')
fprintf('Enter footing parameters: \n')
fprintf('\n')
b = input('Enter proposed footing width, B (ft): ');
l = input('Enter proposed footing breadth, L (ft): ');
t = input('Enter proposed footing thickness, T (ft): ');
beta = input('Enter angle of inclination, beta (deg): ');
fprintf('\n')
fprintf('Enter foundation soil conditions: \n')
fprintf('\n')
phi = input('Enter predominant angle of friction, phi (deg): ');
c = input('Enter value for cohesion, c (psf): ');
es = input('Enter estimated modulus of elasticity for the soil, Es (psf): ');
p = input('Enter estimated Poissons Ratio for the soil, v: ');
gamma_b = input('Enter unit weight of soil BELOW footing (pcf): ');
fprintf('\n')
fprintf('Enter surcharge conditions: \n')
fprintf('\n')
gamma_a = input('Enter weight avg. for unit weight of soil ABOVE footing (pcf): ');
n = 1;
df = linspace(0, 2*b);
%Table 6.2
parameters = bcf_lookup(n, phi);
Nc = parameters(1,2);
Nq = parameters(1,3);
Ng = parameters(1,4);
%Shape Factors
Fcs = 1 + (b/l)*(Nq/Nc);
Fqs = 1 + (b/l)*tan(deg2rad(phi));
Fgs = 1 - 0.4*(b/l);
%Depth Factors
if phi == 0
Fcd = 1 + 0.4.*df/b;
Fqd = 1;
Fgd = 1;
else
Fqd = 1 + 2*tan(deg2rad(phi))*((1-sin(deg2rad(phi)))^2).*(df/b);
Fcd = Fqd - (1-Fqd)/(Nc*tan(deg2rad(phi)));
Fgd = 1;
end
%Inclination Factors
Fci = (1 - beta/90)^2;
Fqi = Fci;
Fgi = (1 - beta/phi)^2;
%Compressibility Factors
gs = es/(2+2*p);
ir = gs./(c+(gamma_b.*df + gamma_b*0.5*b)*tan(deg2rad(phi)));
ircr = 0.5*exp((3.3-0.45*(b/l)*cot(deg2rad(45 - 0.5*phi))));
if ir>ircr
Fcc = 1;
Fqc = 1;
Fgc = 1;
else
Fqc = exp((-4.4 + 0.6*b/l)*tan(deg2rad(phi)) + (3.07*sin(deg2rad(phi))*log10(2*ir))/(1+sin(deg2rad(phi))));
Fgc = Fqc;
end
if phi == 0
Fcc = 0.32 + 0.12*b/l + 0.60*log10(ir);
else
Fcc = Fqc - (1-Fqc)/(Nq*tan(deg2rad(phi)));
end
if df <= t
qu = Nc*c*Fcs*Fcd*Fci*Fcc + 0.5*b*gamma_b*Ng*Fgs*Fgd*Fgi*Fgc;
qnet = Nc*c*Fcs*Fcd*Fci*Fcc + 0.5*b*gamma_b*Ng*Fgs*Fgd*Fgi;
qall = (Nc*c*Fcs*Fcd*Fci*Fcc + 0.5*b*gamma_b*Ng*Fgs*Fgd*Fgi)/3;
else
qu = Nc*c*Fcs.*Fcd*Fci.*Fcc + gamma_a.*df*Nq*Fqs.*Fqd*Fqi*Fqc + 0.5*b*gamma_b*Ng*Fgs*Fgd*Fgi*Fgc;
qnet = ((Nc*c*Fcs.*Fcd*Fci.*Fcc + gamma_a.*df*Nq*Fqs.*Fqd*Fqi*Fqc + 0.5*b*gamma_b*Ng*Fgs*Fgd*Fgi*Fgc) - (gamma_a.*df));
qall = ((Nc*c*Fcs.*Fcd*Fci.*Fcc + gamma_a.*df*Nq*Fqs.*Fqd*Fqi*Fqc + 0.5*b*gamma_b*Ng*Fgs*Fgd*Fgi*Fgc) - (gamma_a.*df))/3;
end
%Graphical Outputs
plot(df, qu, 'g', df, qnet, 'r', df, qall, 'b')
xlabel('Depth of Footing, D (ft)');
ylabel('Bearing Capacity, (psf)');
title('Ultimate Bearing Capacity vs. Foundation Depth');
legend({'Ult. Bearing Capacity, qu (psf)', 'Net Bearing Capacity, qnet (psf)', 'All. Bearing Capacity, qall (psf)'}, 'Location', 'northwest')
  3 个评论
Star Strider
Star Strider 2022-4-26
It is likely necessary to use it in every multiplication and division (unless the division is by a scalar) —
vectorize('qu = Nc*c*Fcs.*Fcd*Fci.*Fcc + gamma_a.*df*Nq*Fqs.*Fqd*Fqi*Fqc + 0.5*b*gamma_b*Ng*Fgs*Fgd*Fgi*Fgc;')
ans = 'qu = Nc.*c.*Fcs.*Fcd.*Fci.*Fcc + gamma_a.*df.*Nq.*Fqs.*Fqd.*Fqi.*Fqc + 0.5.*b.*gamma_b.*Ng.*Fgs.*Fgd.*Fgi.*Fgc;'
This likely applies to every other assignment using similar arguments.
The vectorize function makes this straightforward. (The documentation says that it is ‘not recommended’ in spite of its utility in such situations. I do recommend it!)
.
Ryan Haynes
Ryan Haynes 2022-4-26
Donya and Star Strider,
Both replies fixed the error. Thanks again for your quick help!
-Ryan

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by