使用离散方法计算导数 [英] Compute a derivative using discrete methods

查看:305
本文介绍了使用离散方法计算导数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用离散快速方法来计算导数的方法。从现在开始,我不知道方程的类型,我正在寻找与我们可以找到积分方法类似的离散方法,例如欧拉方法。

解决方案

我认为您正在寻找以点计算的导数。
如果是这种情况,这里有一种简单的方法。您需要了解某个点的导数,例如 a 。它由h-> 0的差商的限制给出:





您实际上需要实现限制功能。因此,您:




  • 定义epsilon,将其设置得越小越精确,越大就越快

  • 计算起始h的商,假设h = 0.01,将其存储在 f1

  • 现在在DO-WHILE循环中:



    1-将h除以2(或除以10,最重要的是减小它)

    2-再次计算差商用新的h值,将其存储在 f2

    3-设置 diff = abs(f2-f1)

    4-指定 f1 = f2

    5-从点1重复 while(diff> epsilon)


  • 最后可以返回f1(或f2)作为f'(a)的值



记住:
您假设函数在 a 中是可区分的。
由于计算机可以处理的有限十进制数字的错误,您将获得的每个结果都是错误的。



python中的示例:

  def define(f,a,h = 0.01,epsilon = 1e-7):
f1 =( f(a + h)-f(a))/ h
而True:#DO-WHILE
h / =2。
f2 =(f(a + h)-f(a ))/ h
diff = abs(f2-f1)
f1 = f2
如果diff< epsilon:break
return f2

print in x = 0
print x ^ 2:\t\t%.6f%derive(lambda x:x ** 2,0)
print x:\t\ \t%.6f%派生(lambda x:x,0)
打印(x-1)^ 2:\t%.6f%派生(lambda x:(x-1)** 2,0)

打印 \n\n实数值:
打印得出(lambda x:x ** 2,0)
打印得出(lambda x: x,0)
打印得出(lambda x:(x-1)** 2,0)

输出:

  x = 0中的导数
x ^ 2:0.000000
x:1.000000
(x-1)^ 2:-2.000000


实际值:
7.62939453125e-08
1.0
-1.99999992328

由于只使用结果的前6位,所以我第一次获得精确值,请注意,我使用1e-7作为epsilon。此后会打印出REAL计算值,显然它们在数学上是错误的。小ε的选择取决于您希望结果的精确度。


I am looking for a method to compute a derivative using a discrete and fast method. Since now I do not know the type of equation I have, I am looking for discrete methods analog to the ones that we can find for the integral such as, the Euler method.

解决方案

I think you are looking for the derivative calculated in a point. If this is the case, here there is a simple way to do that. You need to know the derivative in a point, say a. It is given by the limit of the difference quotient for h->0:

You actually need to implement the limit function. So you:

  • Define an epsilon, set it more small to be more precise, bigger to be faster
  • calculate the difference quotient in a starting h, suppose h=0.01, store it in f1
  • Now in a DO-WHILE loop:

    1- divide h by 2 (or by 10, the important thing is to make it smaller)
    2- calculate again the difference quotient with the new value of h, store this in f2
    3- set diff = abs(f2-f1)
    4- assign f1 = f2
    5- repeat from point 1 while (diff>epsilon)

  • You can finally return f1 (or f2) as the value of your f'(a)

Remember: You are assuming the function is differentiable in a. Every result you will get will be wrong due of errors of the finite decimal digit your computer can handle, there is no escape from this.

Example in python:

def derive(f, a, h=0.01, epsilon = 1e-7):
    f1 = (f(a+h)-f(a))/h
    while True: # DO-WHILE
        h /= 2.
        f2 = (f(a+h)-f(a))/h
        diff = abs(f2-f1)
        f1 = f2
        if diff<epsilon: break
    return f2

print "derivatives in x=0"
print "x^2: \t\t %.6f" % derive(lambda x: x**2,0)
print "x:\t\t %.6f" % derive(lambda x: x,0)
print "(x-1)^2:\t %.6f" % derive(lambda x: (x-1)**2,0)

print "\n\nReal values:"
print derive(lambda x: x**2,0)
print derive(lambda x: x,0)
print derive(lambda x: (x-1)**2,0)

Output:

derivatives in x=0
x^2:         0.000000
x:       1.000000
(x-1)^2:     -2.000000


Real values:
7.62939453125e-08
1.0
-1.99999992328

The first time I've got "precise" value" because of using only the first 6 digits of the result, note I used 1e-7 as epsilon. The REAL calculated values are printed after that, and they are obviously mathematically wrong. The choose of how small epsilon is depends on how precise you want your results to be.

这篇关于使用离散方法计算导数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆