# How can I evaluate elements in an array individually. Typing in a range of eg -2:2 the negative elements are being evaluated at an incorrect condition (1st elseif condition). What is the problem

11 ビュー (過去 30 日間)
Eddy Wekesa Wanyonyi 2020 年 9 月 22 日 9:32
コメント済み: Eddy Wekesa Wanyonyi 2020 年 9 月 22 日 11:33
function [volume] = vol(h)
%Function computes the volume of a tank in the shape of a cylinder (h=10) at the bottom and a
%frustrum (h=5) above it
h = input ('Type height range here');
for c = h
if c <= 0 %If h is negative or 0 return zero for volume
volume = h.*0;
elseif (0 < c) && (c <= 10) %If h is the range of the cylinder height compue volume using the formula
volume = 16*pi.*h;
elseif (10 < c) && (c <= 15) %If h is not in range of cylinder height, add max cylinder volume to frustrum volume
volume = 160*pi + (8/3)*pi*(((h./2-1).^3)- 64)./4;
elseif (c > 15) %Otherwise if h is above h max for tank return max volume for tank at h=15
h=(h.*0 + 15);
volume = 160*pi + (8/3)*pi*(((h/2-1).^3)- 64)./4; (2nd els
else
disp('Please type in an acceptable value')
end
end

サインインしてコメントする。

### 回答 (1 件)

madhan ravi 2020 年 9 月 22 日 9:40
elseif (c > 0) && (c <= 10)
elseif (c > 10) && (c <= 15)

#### 1 件のコメント

Eddy Wekesa Wanyonyi 2020 年 9 月 22 日 11:33
I don't think change in arrangement of the expression solved the problem

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by