为什么我的等式给出了错误的结果?

问题描述:

为什么我仍然得到1.0以上的值?为什么我的等式给出了错误的结果?

当试图实现这个图:

enter image description here

使用this网站给我提供两条线/\这是y = (1/3)x + 1y = (-1/3)x + 1分别时,我把它放在这样的简单的数学公式:

for (int i = 0; i < 50; i++) 
{ 
    input = GenerateRandom(-3.0, 3.0); // this function works fine 
    output = FuzzyFunction(input); // get crisp outputs 
    std::cout << "input: " << input << " - output: " << output << " \n"; 
} 

// stuff  

double FuzzyFunction(double inputVal) 
{ 
    outputVal = (1.0/3) * inputVal + 1; 
    return outputVal; 
} 

然后输出是这样的:

enter image description here

+0

你可以包括一些预期输出的例子吗? –

+2

为什么你会期望一个正数PLUS ONE比任何一个少? –

+2

该功能完全按照您所说的进行。看第一行,'input = 2.74548'。乘以1.0/3,然后做'+ 1',得到'1.91516'。如果你不想获得'1.91516',那么你必须在函数中使用不同的输入或不同的方程... –

我犯了一个可怕的逻辑错误。我需要这在我FuzzyFunction()

if(inputVal<0) 
    outputVal = (1.0/5) * inputVal + 1; 
else if (inputVal > 0) 
    outputVal = (-1.0/5) * inputVal + 1; 
else 
    outputVal = 0; 

正确的输出:

enter image description here

斜率为正的投入应该是否定的:

double FuzzyFunction(double inputVal) 
{ 
    if (inputVal < 0) 
     outputVal = (1.0/3) * inputVal + 1; 
    else 
     outputVal = (-1.0/3) * inputVal + 1; 
    return outputVal; 
}