MATLAB: A(I) = B, number of elements must be the same. I’m new and can’t figure out what it wants me to do!!!

a(i)=b

I keep getting this message regardless of what I try. Im not even sure what A(I)=B is. This is for school. Im trying to write a program that calculates how much pipe will cost. if pipe is less then 10 meters it costs $25 but any amount over 10 meters the cost goes to $15. everytime I put in some variation of the formula for amounts over 10 meters. I get the following message "In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in matlabhw2 (line 39) pipeCost(i) = (price1*10)+(price2*(pipeLength-10)); % Price for lengths greater than 10m"
here is my program so far…
% 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('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+???; % Price for lengths greater than 10m
end
end

Best Answer

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('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(i).^2));
if pipeLength<=10
pipeCost(i) = price1*pipeLength;
else
pipeCost(i) = price1*10+price2*(pipeLength-10);
% Price for lengths greater than 10m
end
end
Related Question