MATLAB Answers

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

Raphael Hatami 님이 질문을 제출함. 31 Aug 2019
최근 활동 R.G. 님이 편집함. 1 Sep 2019
Hello, bit of a long code here but the error seems to just be in line 33, pipeCost(i) = price1*10 + price2*(pipeLength-10);
error: Unable to perform assignment because the left and right sides have a different number of elements.
I'm confused because it does not have a problem with line 31. Can anyone see what's wrong here? Thank you
% WaterPipe.m
% This program computes the water exit velocity for a gravity-driven pipe system and determines the cost
% of the pipe for a range of different water tank heights. A plot of water exit velocity vs. tank height (z) is
% generated, and the z values corresponding to the minimum exit velocity and maximum pipe cost are
% indicated on the plot.
% Define variables
minVelocity = 10 % minimum exit velocity
maxCost = 600 % maximum cost of pipe
price1 = 25; % price per meter for first 10 meters
price2 = 15; % price per meter for each additional meter
g = 9.81; % gravity
% Calculate velocity values
z = linspace(0, 30, 11);
exitVelocity = sqrt(2*g*z)
% Plot velocity vs. z
plot(z,exitVelocity);
xlabel('Height [m]');
ylabel('Exit Velocity [m/s]');
hold on;
% Calculate the height that produces the minimum allowable exit velocity and mark it on the graph with
% a vertical line of 20 x’s.
minVelZ = (minVelocity^2)/(2*g);
zLine = minVelZ*ones(1,20); % z values for the vertical line
vLine = linspace(min(exitVelocity),max(exitVelocity),20); % velocity values for the vertical line
plot(zLine,vLine,'x');
% Compute pipe cost for each of the eleven z values
for i=1:11
x = z(i)/0.9;
pipeLength = sqrt((x.^2)+(z.^2))
if pipeLength <= 10
pipeCost(i) = price1*pipeLength;
else
pipeCost(i) = price1*10 + price2*(pipeLength-10); % Price for lengths greater than 10 m
end
end
% Plot the pipe cost vs. z
figure(2) % opens a second figure
plot(z, pipeCost);
xlabel('height of water tank (m)');
ylabel('cost of pipe ($)');
hold on;
% Find the height corresponding to the maximum pipe cost, and mark it on the graph with a vertical line
% of 20 o’s.
maxLength = (maxCost-price1*10)/price2 + 10;
maxHeight = sqrt(maxLength-x^.2)
zLine2 = maxHeight*ones(1,20) % z values for the vertical line (HINT: look at similar line in first plot)
cLine = linspace(0,600,20); % cost values for the vertical line (HINT: use min and max of pipeCost)
plot(zLine2,cLine,'o');

  댓글 수: 0

로그인 to comment.

태그

답변 수: 3

R.G. 님의 답변 31 Aug 2019
 채택된 답변

Hello! Change pipeCost(i) to pipeCost(i,:)

  댓글 수: 3

@Raphael Hatami, this avoids an error but I doubt you're expecting a vector of outputs assigned to pipeCost for these reasons:
  1. within your i-loop, x is always a scalar (it has 1 value)
  2. "if pipeCost <= 10" is completely wrong syntax if pipeCost is a vector.
  3. this line always produces a scalar: pipeCost(i) = price1*pipeLength;
Yes, it seems your truth. It makes sense to take i-th value of z (velocity). And there is a comment there: % Compute pipe cost for each of the eleven z values

로그인 to comment.


Adam Danz 님의 답변 31 Aug 2019
Adam Danz 님이 편집함. 31 Aug 2019

In your for-loop, it looks like you're expecting pipeCost to produce 1 value per iteration. If that is the case, you're missing an i-index with variable "z".
% Compute pipe cost for each of the eleven z values
for i=1:11
x = z(i)/0.9;
pipeLength = sqrt((x.^2)+(z(i)^2));
% ^^^.........this is missing

  댓글 수: 0

로그인 to comment.


Walter Roberson 님의 답변 31 Aug 2019

You have
x = z(i)/0.9;
pipeLength = sqrt((x.^2)+(z.^2))
The first of those lines tells us that z is a vector. A scalar element is extracted from z and used to calculate a scalar x. Then on the second line, the scalar x is used, but the complete vector z is used, so the result is a vector. Everything goes wrong from there.

  댓글 수: 0

로그인 to comment.



Translated by