error: Unable to perform assignment because the left and right sides have a different number of elements.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Raphael Hatami
el 31 de Ag. de 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 comentarios
Respuesta aceptada
R.G.
el 31 de Ag. de 2019
Hello! Change pipeCost(i) to pipeCost(i,:)
3 comentarios
Adam Danz
el 31 de Ag. de 2019
@Raphael Hatami, this avoids an error but I doubt you're expecting a vector of outputs assigned to pipeCost for these reasons:
- within your i-loop, x is always a scalar (it has 1 value)
- "if pipeCost <= 10" is completely wrong syntax if pipeCost is a vector.
- this line always produces a scalar: pipeCost(i) = price1*pipeLength;
Más respuestas (2)
Adam Danz
el 31 de Ag. de 2019
Editada: Adam Danz
el 31 de Ag. de 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 comentarios
Walter Roberson
el 31 de Ag. de 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 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!