【问题标题】:Using scanf to output a standard logistic function of x?使用scanf输出x的标准逻辑函数?
【发布时间】:2018-09-07 03:38:44
【问题描述】:

我需要创建一个代码,提示某人输入一个浮点数,并使用 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);
}

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

【问题讨论】:

  • x0k 的值是多少?这是一个很好的练习来学习 c.......
  • @rioV8 x0= 0 和 k= 1
  • 你知道如何用C写表达式吗?如果没有,您可能需要复习基础知识。你到底卡在哪一部分?您可能会发现 exp() 函数很有用。

标签: c scanf


【解决方案1】:

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

#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) );
}

谢谢大家的建议。

【讨论】:

  • 欢迎您@Ryan James。如果您觉得有帮助,请采纳答案吗?
  • 为什么需要这么多临时任务?这一切都可以在一个简单的返回语句中完成
  • 是的,我本可以做到的,但我不想让它看起来对你和将来可能会看到它的人来说很复杂。
  • @AkhileshPandey 我看不出这样的表达式有多复杂,因为它只是 OP 显然知道的逻辑函数的实现。如果有的话,您的 temp 变量实际上使事情变得更加复杂,因为很难看到原始表达式。
  • 好的,那我改一下,问题不大。谢谢@费翔
猜你喜欢
  • 2021-09-04
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多