# How do I fix my display of the x and y values? They should be x = 1.15 and y = 3.85.

1 view (last 30 days)
MyKah Smith on 27 Apr 2021
Answered: Image Analyst on 27 Apr 2021
%code to calculate volume of water in a cylinder tank
voltage = 1.15; %input voltage from floater when tank is empty
%maximum voltage is 5V
%tank dimensions in metres
A = 2.63;
B = 3.945;
C = 5.9175;
x = minimum_voltage == acosd(B/2*A)*5/180;
y = maximum_voltage == ((180 - angle)/180)*5;
disp(x)
disp(y)
%Convert voltage to angle
angle = (voltage*180)/5;
disp(angle)
%Check input voltage is valid
if voltage < minimum_voltage
disp('ERROR')
elseif voltage > maximum_voltage
disp('ERROR')
volume = 'ERROR';
else
disp('Voltage seems sensible')
end
>> VolumeCalculations
1
0
41.4000
Voltage seems sensible

Image Analyst on 27 Apr 2021
Since you have not yet defined minimum_voltage and maximum_voltage, these lines will throw an error saying they are not defined. And if you had defined them, x and y would be either true or false since x and y are both set equal to a comparison condition.
x = minimum_voltage == acosd(B/2*A)*5/180;
y = maximum_voltage == ((180 - angle)/180)*5;
Simply delete all references to x and y and have this:
minimum_voltage = acosd(B/2*A)*5/180;
maximum_voltage = ((180 - angle)/180)*5;
fprintf('Min voltage = %f. Max voltage = %f.\n', minimum_voltage, maximum_voltage);

Chad Greene on 27 Apr 2021
The double equals is defining x and y as a boolean. It's saying x = 1 if minimum_voltage equals acosd(B/2*A)*5/180. Let's find out what acosd(B/2*A)*5/180 actually equals:
voltage = 1.15;
A = 2.63;
B = 3.945;
C = 5.9175;
acosd(B/2*A)*5/180
ans = 0.0000 + 3.7083i
The way you have written your code, it says x = 1 if minimum_voltage equals 0.0000 + 3.7083i. Otherwise, x = 0. If you want x to be 1.15, write
x = 1.15;