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

3 views (last 30 days)
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');

Accepted Answer

R.G.
R.G. on 31 Aug 2019
Hello! Change pipeCost(i) to pipeCost(i,:)
  3 Comments
Adam Danz
Adam Danz on 31 Aug 2019
@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;
R.G.
R.G. on 31 Aug 2019
Edited: R.G. on 1 Sep 2019
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

Sign in to comment.

More Answers (2)

Adam Danz
Adam Danz on 31 Aug 2019
Edited: Adam Danz on 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

Walter Roberson
Walter Roberson on 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.

Categories

Find more on Financial Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!