MATLAB Answers

Error: Unable to perform assignment because the left and right sides have a different number of elements.

1 view (last 30 days)
Alexis Clark
Alexis Clark on 6 Jul 2021
Answered: Arthi Sathyamurthi on 20 Jul 2021
At first I kept getting the error that the martix dimensions did not agree so I added the period before the operator "./" and now I am getting the error "Unable to perform assignment because the left and right sides have a different number of elements." I have attached my code below, the goal is to calculate strain with a changing diameter at an increment of 0.01.
F = 130;
Lb = 1.5;
T = (F * Lb) * 12;
M = T;
E = (9.863 * (10^6));
maxstrain = (1000 * (10^-6));
% Pipe Size 3
od3 = 3.5;
t3 = 0.216;
id3 = od3 - (2 * t3);
I3 = (pi / 4) * ((od3/2)^4) - ((id3)/2^4);
strain3 = (M * od3) / (2 * E * I3);
fprintf('The outer diamter for pipe size 3 is %.3f\n', od3)
fprintf('The strain for pipe size 3 is %.5f\n', strain3)
% Pipe Size 4
od4 = 4.5;
t4 = 0.237;
id4 = od4 - (2 * t4);
I4 = (pi / 4) * ((od4/2)^4) - ((id4)/2^4);
strain4 = (M * od4) / (2 * E * I4);
fprintf('The outer diamter for pipe size 4 is %.3f\n', od4)
fprintf('The strain for pipe size 4 is %.5f\n', strain4)
% Pipe Size 6
od6 = 6.625;
t6 = 0.280;
id6 = od3 - (2 * t6);
I6 = (pi / 4) * ((od6/2)^4) - ((id6)/2^4);
strain6 = (M * od6) / (2 * E * I6);
fprintf('The outer diamter for pipe size 6 is %.3f\n', od6)
fprintf('The strain for pipe size 6 is %.5f\n', strain6)
x = [od3, od4, od6];
y = [strain3, strain4, strain6];
subplot(2, 1, 1)
plot(x, y)
title('Outer Diamter vs Microstrain of Schedule 40 Pipe Sizes')
xlabel('Outer Diamter in inches')
ylabel('Microstrain')
inc = -0.01;
imax = 43;
din = od3;
d = zeros(43);
d = d(1:43);
I = zeros(43);
I = I(1:43);
for i = 1 : imax
d(i) = din - ((i-1).*inc);
I(i) = (M * dact) ./ (2 * E * I);
i = i + 1;
end
dact = d(:,1);
subplot(2, 1, 2)
plot(d, I)
  2 Comments
Superficial
Superficial on 7 Jul 2021
The reason you're getting that error is because Matlab is trying to fit 43 numbers (the result of the equation) into a space for a single number I(i).
Without knowing exactly what you're trying to achieve with the equation it's hard to know how to fix it. Perhaps you meant:
I(i) = (M * d(i)) ./ (2 * E * i); % (small i rather than I)
Your code is also kind of messy. I assume you've copied and pasted / adapted it rather than write it yourself? E.g:
d = zeros(43);
d = d(1:43);
For efficiency, should be replaced by:
d = zeros(1:43);
Also, you don't need the line
i = i+1
since your for loop will iterate that.

Sign in to comment.

Answers (1)

Arthi Sathyamurthi
Arthi Sathyamurthi on 20 Jul 2021
Hello Alexis,
The error has occurred because both sides of the equation doesn’t have equal number of elements. Hence, try replacing the lines from 48-57 with the following code
d = zeros(1,43);
I = zeros(1,43);
for i = 1 : imax
d(i) = din - ((i-1).*inc);
I(i) = (M * d(i)) ./ (2 * E * i);
end

Community Treasure Hunt

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

Start Hunting!

Translated by