Error using .* Array sizes must match.

41 views (last 30 days)
Joaquin
Joaquin ongeveer 22 uur ago
Answered: Image Analyst ongeveer 9 uur ago
I need help with understanding the error in the summary of this prompt. Any help in troubleshooting would be greatly appreciated. Keep in mind this is only one section of the total code I am running, the parts before are where the functions are defined, which work with no problems
The code I am using is as follows:
syms x y
xv = linspace(0,35,350);
Snorm = (Mnet(xv) .* y_max) ./ I(xv);
Sshear = (Dnet(xv) .* Q(xv)) ./ (I(xv) .* t);
y6 = Snorm(xv);
y7 = Sshear(xv);
figure;
plot(xv, y6, 'blue');
title('Shear Stress');
xlabel('Position along the wing (m)');
ylabel('Stress (Pa)');
grid on;
figure;
plot(xv, y7, 'blue');
title('Normal Stress');
xlabel('Position along the wing (m)');
ylabel('Stress (Pa)');
grid on;
  3 Comments
Joaquin
Joaquin ongeveer 22 uur ago
This is all of the red text from the error that I get.
I have attached a screenshot of the message I see.
Stephen23
Stephen23 ongeveer 22 uur ago
The sizes of the arrays you are attempting to multiply are not compatible:
Allow debugging on error by calling
dbstop if error
and then check the sizes of all of the arrays used in that operation.

Sign in to comment.

Answers (2)

Animesh
Animesh ongeveer 22 uur ago
The error you're encountering is due to the sizes of "Mnet(xv)" and "y_max," "Dnet(xv)" and "Q(xv)," or "I(xv)" and "t" not being compatible.
For element-wise multiplication, the sizes of both arrays must be either the same or compatible.
You can refer to the following MathWorks documentation for more information on element-wise multiplication: https://www.mathworks.com/help/matlab/ref/double.times.html

Image Analyst
Image Analyst ongeveer 9 uur ago
Add these extra lines and tell us what you see in the command window:
whos mNet
whos y_max
whos I
whos Dnet
whos Q
whos t
Snorm = (Mnet(xv) .* y_max) ./ I(xv);
Sshear = (Dnet(xv) .* Q(xv)) ./ (I(xv) .* t);
Since you're using element-by-element multiplication the sizes of all of those must match. However y_max and y could also be a single scalar number and it would work. The others should be vectors or functions. If they are vectors, then
xv = linspace(0,35,350)
xv = 1×350
0 0.1003 0.2006 0.3009 0.4011 0.5014 0.6017 0.7020 0.8023 0.9026 1.0029 1.1032 1.2034 1.3037 1.4040 1.5043 1.6046 1.7049 1.8052 1.9054 2.0057 2.1060 2.2063 2.3066 2.4069 2.5072 2.6074 2.7077 2.8080 2.9083
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
etc.
will give you problems because xv has a zero and a bunch of fractional numbers in it and arrays and vectors can take only integers as indexes, not zeros and fractions as you currently have xv.

Community Treasure Hunt

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

Start Hunting!