铁的饮用水标准为0.3mg / L。同样,镁的标准为0.05mg / L。创建一个程序,该程序将吸收铁和镁的测量值,并显示水是否可以安全饮用。
我已经启动了先测试Iron的程序,然后将创建针对Magnesium的另一个程序测试。我的问题是我必须使用子功能来解决程序,并且得到正确的答案,但是命令窗口中出现错误。我知道它必须包含test=testWater(IronSafe)
,因为我尚未声明输出语句,而且不确定如何将其实现到代码中。
function [IronStand] = ProblemWATER(IronSafe)
%Create a function that states whether H20 for iron is safe to drink
% 1 inputs: IronSafe
% 1 output:IronStand
IronSafe = input('What is the density level of your iron \n');
IronStand = testWater(IronSafe);
end
function test= testWater(IronSafe)
%Subfunction meant to determine if IronStand drinkable
IronStand = 0.3;
if IronSafe == IronStand
disp('Safe to drink');
else
disp('Not safe');
end
end
答案 0 :(得分:1)
问题出在您的第二个功能function test= testWater(IronSafe)
中。您正在返回值test
,但从未将其分配给任何值。您可以通过删除它来解决它:
function [] = ProblemWATER(IronSafe)
%Create a function that states whether H20 for iron is safe to drink
% 1 inputs: IronSafe
% 1 output:IronStand
IronSafe = input('What is the density level of your iron \n');
testWater(IronSafe);
end
function [] = testWater(IronSafe)
%Subfunction meant to determine if IronStand drinkable
IronStand = 0.3;
if IronSafe == IronStand
disp('Safe to drink');
else
disp('Not safe');
end
end
如果您想返回IronStand
的值,则需要更改返回值:
function [IronStand] = ProblemWATER(IronSafe)
%Create a function that states whether H20 for iron is safe to drink
% 1 inputs: IronSafe
% 1 output:IronStand
IronSafe = input('What is the density level of your iron \n');
IronStand = testWater(IronSafe);
end
function [IronStand] = testWater(IronSafe)
%Subfunction meant to determine if IronStand drinkable
IronStand = 0.3;
if IronSafe == IronStand
disp('Safe to drink');
else
disp('Not safe');
end
end