C语言 如何使用c找到函数的导数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12915317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:06:47  来源:igfitidea点击:

How to find derivative of a function using c

cderivative

提问by Eka

Is it possible to find derivative of a function using c program. I am using matlab in that it has an inbuilt function diff() which can be used for finding derivative of a function.

是否可以使用c程序找到函数的导数。我正在使用 matlab,因为它有一个内置函数 diff() 可用于查找函数的导数。

f(x)=x^2

Is it possible to find the derivative of above function using c. What is the algorithm for that?

是否可以使用c找到上述函数的导数?什么是算法?

回答by

Yes, it is quite possible. However, the solution depends on your needs. If you need a simple numerical solution, the following will do (to a certain extent, with some constraints - naive implementation):

是的,这是很有可能的。但是,解决方案取决于您的需求。如果您需要一个简单的数值解决方案,则可以执行以下操作(在一定程度上,有一些限制 - 幼稚的实现):

double derive(double (*f)(double), double x0)
{
    const double delta = 1.0e-6; // or similar
    double x1 = x0 - delta;
    double x2 = x0 + delta;
    double y1 = f(x1);
    double y2 = f(x2);
    return (y2 - y1) / (x2 - x1);
}

// call it as follows:
#include <math.h>

double der = derive(sin, 0.0);
printf("%lf\n", der); // should be around 1.0

For more advanced numericalcalculations, you can use the GNU Scientific Library.

对于更高级的数值计算,您可以使用GNU 科学库。

However, if you need to analitically find the formulaof the derivative of a given function, then you have to:

但是,如果您需要分析地找到给定函数的导数公式,那么您必须:

  1. Parse the input formula to some abstract data type, for example an AST;
  2. Derivate it using the identities and rules of derivation (there's only a few of them, this part should be the easiest),
  3. Serialize the abstract data type you got as the result of the derivation process to a string and output that as the result.
  1. 将输入公式解析为某种抽象数据类型,例如 AST;
  2. 使用恒等式和推导规则来推导它(只有几个,这部分应该是最简单的),
  3. 将作为推导过程结果获得的抽象数据类型序列化为字符串并将其作为结果输出。

However, you won't need to do all this; there are great C mathematical libraries that provide such functionality.

但是,您不需要执行所有这些操作;有很多很棒的 C 数学库可以提供这样的功能。

Edit: after some Googling, I couldn't find one. The closest solution for getting you started I can think of is having a look at GeoGebra's source code- although it's written in Java, it's fairly easy to read for anybody fluent enough in a C-like language. If not, just go ahead and implement that algorithm yourself :)

编辑:经过一些谷歌搜索,我找不到一个。我能想到的让您入门的最接近的解决方案是查看GeoGebra 的源代码- 尽管它是用 Java 编写的,但对于精通类 C 语言的任何人来说,它都相当容易阅读。如果没有,请继续自己实现该算法:)

回答by coproc

For simple functions the following numerical differentiation works quite well:

对于简单的函数,以下数值微分非常有效:

typedef double (*TFunc)(double);

// general approximation of derivative using central difference
double diff(TFunc f, double x, double dx=1e-10)
{
  double dy = f(x+dx)-f(x-dx);
  return dy/(2.*dx);
}

// more or less arbitrary function from double to double:
double f(double x)
{
   return x*x;
}

// and here is how you get the derivative of f at specified location
double fp = diff(f, 5.);

回答by mathematician1975

There is nothing built into the C language to enable this. You might be able to find a numerical library to do it though if you search online, although I would doubt that there is anything available that will provide symbolic derivatives. You could consider coding approximate numerical derivatives yourself using forward, backward and/or central differences.

C 语言中没有内置任何内容来实现这一点。如果您在线搜索,您可能可以找到一个数值库来执行此操作,尽管我怀疑是否有任何可用的东西可以提供符号导数。您可以考虑使用前向、后向和/或中心差异自己编写近似数值导数。

回答by Dmytro Sirenko

In C, you can do rough numerical differentiation relatively easy, but any kind of symbolic differentiation requires a third-party framework or rolling your own.

在 C 中,您可以相对容易地进行粗略的数值微分,但任何类型的符号微分都需要第三方框架或滚动您自己的框架。

C is a general-purpose and low-level programming language, unlike Matlab, which is specialized for mathematical computations and has advanced tools for symbolic computations.

C 是一种通用的低级编程语言,与 Matlab 不同,它专门用于数学计算并具有用于符号计算的高级工具。

回答by Farhan Khan

For the curious peeps who want the maths behind f'(x) we use the standard definition of the derivative obtained from the limits see :Formula for derivative.

对于想要了解 f'(x) 背后数学的好奇的人,我们使用从极限中获得的导数的标准定义,请参阅:导数公式。

Here, h->0 (h tends to 0) means that h is a very small number. You can take this number to be 10^-5 for most calculations. Although both equations work perfectly well, in practice, the second one gives better values. However, one may also note that these computations are highly expensive and should be avoided when the actual function can be derived by hand.

这里,h->0(h趋于0)意味着h是一个非常小的数。对于大多数计算,您可以将此数字设为 10^-5。尽管这两个方程都运行良好,但实际上,第二个方程给出了更好的值。然而,人们可能还会注意到,这些计算非常昂贵,当可以手动推导出实际函数时应该避免。