其中,你把一个非常小的 h 和评估函数放在两个地方。这是最基本的公式,在实践中,人们使用其他公式,这些公式给出的估计误差更小。这种计算导数的方法主要适用于你不知道你的函数并且只能对其进行采样的情况。此外,对于高维函数,它需要大量的计算。
from functools import reduce
class Term:
def __init__(self, label: str or int, children=None):
self.label = label
self.children = children if children else []
def __repr__(self):
if self.children:
return f"({self.label} " + reduce(lambda str1, str2: str1+" "+str2, (repr(child) for child in self.children)) + ")"
else:
return f"{self.label}"
@classmethod
def copy(cls, obj):
"""makes a copy of obj"""
return cls(obj.label, [cls.copy(child) for child in obj.children])
MUL = '*'
ADD = '+'
SUB = '-'
DIV = '/'
POW = '^'
LOG = 'log'
SIN = 'sin'
COS = 'cos'
TAN = 'tan'
OPEN_BR = '('
CLOSE_BR = ')'
VAR = 'x'
symbols = {LOG, SIN, COS, TAN, MUL, ADD, SUB, DIV, POW, OPEN_BR, CLOSE_BR, VAR}
from term import Term
from symbols_table import *
import numbers
def calculate_derivative(term1: Term, rules: dict) -> Term:
if isinstance(term1.label, numbers.Number):
return Term(0)
elif term1.label == VAR:
return Term(1)
else:
return rules[term1.label](term1, rules)
def mul_derivative(term1: Term, rules) -> Term:
child1, child2 = term1.children
return Term(ADD, [Term(MUL, [calculate_derivative(child1, rules), child2]), Term(MUL, [child1, calculate_derivative(child2, rules)])])
def div_derivative(term1: Term, rules) -> Term:
child1, child2 = term1.children
return Term(DIV, [Term(SUB, [Term(MUL, [calculate_derivative(child1, rules), child2]), Term(MUL, [child1, calculate_derivative(child2, rules)])]),
Term(POW, [child2, Term(2)])])
def sum_derivative(term1: Term, rules) -> Term:
child1, child2 = term1.children
return Term(term1.label, [calculate_derivative(child1, rules), calculate_derivative(child2, rules)])
def power_derivative(term1: Term, rules) -> Term:
child1, child2 = term1.children
return Term(ADD,
[Term(MUL, [calculate_derivative(child2, rules), Term(LOG, [child1]), Term(POW, [child1, child2])]),
Term(MUL, [calculate_derivative(child1, rules), child2, Term(POW, [child1, Term(SUB, [child2, Term(1)])])])])
def sin_derivative(term1: Term, rules) -> Term:
return Term(MUL, [Term(COS, [term1.children[0]]), calculate_derivative(term1.children[0], rules)])
def cos_derivative(term1: Term, rules) -> Term:
return Term(MUL, [Term(-1), Term(SIN, [term1.children[0]]), calculate_derivative(term1.children[0], rules)])
def tan_derivative(term1: Term, rules) -> Term:
arg = Term.copy(term1.children[0])
return Term(ADD, [calculate_derivative(arg, rules), Term(MUL, [calculate_derivative(arg, rules), Term(POW, [term1, Term(2)])])])
def log_derivative(term1: Term, rules) -> Term:
return Term(DIV, [calculate_derivative(term1.children[0], rules), term1.children[0]])
rules_for_differentiation = {MUL: mul_derivative, ADD: sum_derivative, SUB: sum_derivative,
COS: cos_derivative, SIN: sin_derivative, TAN: tan_derivative,
POW: power_derivative, DIV: div_derivative, LOG: log_derivative}
def derivative(term1):
return calculate_derivative(term1, rules_for_differentiation)
syms f(x) y
y = f(x)^2*diff(f(x),x);
Dy = diff(y,f(x))
\begin{aligned} & Dy = \\ & \qquad 2 f(x) \frac{\partial}{\partial x} f(x)\end{aligned}
syms x(t) m k
T = m/2*diff(x(t),t)^2;
V = k/2*x(t)^2;
D1 = diff(L,diff(x(t),t))