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

1 次查看(过去 30 天)
%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
Image Analyst 2021-4-27
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);

更多回答(1 个)

Chad Greene
Chad Greene 2021-4-27
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;

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by