使用scanf输出x的标准逻辑函数?

问题描述 投票:-4回答:1

我需要创建一个代码,提示某人输入一个浮点数,并使用scanf输出应用于输入数字的标准逻辑函数的值。

逻辑函数定义为:

                     L

 f(x) = ----------------------

           1 + e^(-k(x - x0))

这是我到目前为止:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
{
float number;
 printf("Enter an integer: ");
  scanf("%f",&number);
}

所以我的问题是我可以编写什么代码以使程序输出正确的值?我认为最重要的是如何将逻辑函数合并到代码中?所有变量都应声明为float。非常感谢你提前!

c scanf
1个回答
0
投票

只需编写一个执行计算的函数,并在下面给出的代码中调用它。希望它符合您的目的。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>


float logistic(float x, float x0, float k)
{

    return (1 / ( 1 - exp(-k * ( x - x0)))) ;
}

int main()
{
    float number;
    printf("Enter an integer: ");
    scanf("%f",&number);
   /* Let x0= 1.0 and  k= 2.0 for simplicity */
   /* You can change it whenever you want */
   float x0= 1.0, k= 2.0;

   printf(" Logistic value is %f ",logistic(number, x0 , k) );
}

谢谢你们的建议。

© www.soinside.com 2019 - 2024. All rights reserved.