When I set the voltage = 2.5, the volume isn't being calculated and displayed on the command window and the wrong value for US Gallons is being calculated. Everything else is working apart from this

1 次查看(过去 30 天)
%code to calculate volume of water in a cylinder tank
voltage = 2.5; %input voltage from floater
%tank dimensions
A = 2.63;
B = 3.945;
C = 5.9175;
D = 1.9725; %this value is B/2
%minimum_voltage = acosd(B/2*A)*5/180;
minimum_voltage = 1.15;
%maximum_voltage = ((180 - minimum_angle)/180)*5;
maximum_voltage = 3.85;
fprintf('Min voltage = %f. Max voltage = %f.\n', minimum_voltage, maximum_voltage);
%Convert voltage to angle
%minimum_angle = 180 - angle at minimum_voltage
angle = (voltage*180)/5;
fprintf('angle = %f.', angle);
%Check input voltage is valid
if voltage < minimum_voltage
disp('ERROR - check the floater')
elseif voltage > maximum_voltage
disp('ERROR - check the floater')
else
disp('Voltage seems sensible')
end
%Calculate correct water levels (volume)
if voltage == 1.15
disp('tank is empty')
%tank is empty
end
if voltage == 2.5
%tank is half full
tank_depth = D;
disp('tank is half full')
fprintf('tank depth = %f.', tank_depth);
end
if voltage == 3.85
disp('tank is full')
%tank is full
end
%Volume of the tank
if voltage == 1.15
volume = 0;
fprintf('Volume = %f.', volume);
end
if volume == 2.5
volume = (pi*((D)^2)*C)/2;
fprintf('Volume = %f.', volume);
end
if voltage == 3.85
volume = pi*((D)^2)*C;
fprintf('Volume = %f.', volume);
end
%Conversion to US gallons
US_gallon = 264.172*volume;
fprintf('US gallon = %f.', US_gallon);
>>VolumeCalculations
Min voltage = 1.150000. Max voltage = 3.850000.
angle = 90.000000.Voltage seems sensible
tank is half full
tank depth = 1.972500.US gallon = 19107.723459.

回答(1 个)

DGM
DGM 2021-5-3
编辑:DGM 2021-5-3
This should fix the one error:
if voltage == 2.5 % voltage, not volume
volume = (pi*((D)^2)*C)/2;
fprintf('Volume = %f.', volume);
end
if voltage == 3.85
volume = pi*((D)^2)*C;
fprintf('Volume = %f.', volume);
end
For any practical application, you might want to avoid strict equality tests on floating-point inputs. If this is homework and the tests are working, then robustness doesn't really matter.
Regarding volume calculation, nobody can guess what the problem definition is. Nobody knows what dimensions A,B,C,D mean for a cylinder or what units they're in. Nobody knows where the sensor is mounted or what its range is or how that translates into physical units. You'll have to debug that.
  1 个评论
Stephen23
Stephen23 2021-5-4
编辑:Stephen23 2021-5-4
"you might want to avoid strict equality tests on floating-point inputs."
Not "might want to", but have to (because otherwise the code is latent buggy, and will just fail later).
"If this is homework and the tests are working, then robustness doesn't really matter."
Latent bugs due to poor floating point handling are often not picked up by a few hand-picked values.
Much better: show the OP exactly why they need to learn how to handle floating point. Replace equality with:
abs(A-B)<tol

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by