简体   繁体   English

定义一个作为函数导数的函数

[英]Define a function that is a derivative of a function

I was wondering if is there is a way to define a function that is a derivative of a function.我想知道是否有一种方法可以定义一个函数的派生函数。 I'm new to python so I don't no much, I tired looking up stuff that might be similar but nothing has worked so far.我是python的新手,所以我没有太多,我厌倦了查找可能相似但到目前为止没有任何效果的东西。 This is what I have for my code right now.这就是我现在的代码。

import sympy as sp

import math 

x = sp.Symbol('x')


W = 15 #kN/m
E = 70 # Gpa
I = 52.9*10**(-6) #m**4
L = 3 #m 

e = 0.01
xi = 1.8 
y = 9


def f(x):
    return ( ( y*3*(math.pi**4)*E*I/(W*L) ) - ( 48*(L**3)*math.cos(math.pi*x/(2*L)) ) + ( 48*(L**3) ) + ( (math.pi**3)*(x**3) ) )/(3*L*(math.pi**3))**(1/2)

def derv(f,x):
    return sp.diff(f)

print (derv(f,x))

Also, I don't understand what x = sp.Symbol('x') does, so if someone could explain that, that would be awesome.另外,我不明白x = sp.Symbol('x')是做什么的,所以如果有人能解释一下,那就太棒了。

Any help is appreciated.任何帮助表示赞赏。

You are conflating two different things: python functions like f and math functions, which you can express with sympy like y = π * x/3 .你把两个不同的东西混为一谈:像f和数学函数这样的 python 函数,你可以用y = π * x/3这样的 sympy 来表达。 f is a python function that returns a sympy expression. f是一个 python 函数,它返回一个 sympy 表达式。 sympy lets you stay in the world of symbolic math functions by defining variables like x = sp.Symbol('x') So calling f() produces a symbolic math function like: sympy 通过定义像x = sp.Symbol('x')这样的变量让你留在符号数学函数的世界中,所以调用f()会产生一个符号数学函数,如:

在此处输入图像描述

You can use sympy to find the derivative of the symbolic function returned by f() but you need to define it with the sympy versions of the cos() function (and sp.pi if you want to keep it symbolic).您可以使用 sympy 来查找f()返回的符号函数的导数,但您需要使用cos()函数的sp.pi版本(如果要使其保持符号性,还需要使用 sp.pi)来定义它。

For example:例如:

import sympy as sp
    
x = sp.Symbol('x')
   
W = 15 #kN/m
E = 70 # Gpa
I = 52.9*10**(-6) #m**4
L = 3 #m 

e = 0.01
xi = 1.8 
y = 9


def f(x):
    return ( ( y*3*(sp.pi**4)*E*I/(W*L) ) - ( 48*(L**3)*sp.cos(sp.pi*x/(2*L)) ) + ( 48*(L**3) ) + ( (sp.pi**3)*(x**3) ) )/(3*L*(sp.pi**3))**(1/2)

def derv(f,x):
    return sp.diff(f(x)) # pass the result of f() which is a sympy function

derv(f,x)

在此处输入图像描述

You've programmed the function.您已经对功能进行了编程。 it appears to be a simple function of two independent variables x and y.它似乎是两个自变量 x 和 y 的简单函数。

Could be that x = sp.Symbol('x') is how SymPy defines the independent variable x.可能x = sp.Symbol('x')是 SymPy 定义自变量 x 的方式。 I don't know if you need one or another one for y.我不知道你是否需要一个或另一个。

You know enough about calculus to know that you need a derivative.你对微积分有足够的了解,知道你需要一个导数。 Do you know how to differentiate a function of a single independent variable?你知道如何区分单个自变量的函数吗? It helps to know the answer before you start coding.在开始编码之前知道答案会有所帮助。

y*3*(math.pi**4)*E*I/(W*L) ) - ( 48*(L**3)*math.cos(math.pi*x/(2*L)) ) + ( 48*(L**3) ) + ( (math.pi**3)*(x**3) ) )/(3*L*(math.pi**3))**(1/2)

Looks simple.看起来很简单。

There's only one term with y in it.只有一个术语带有 y。 The partial derivative wrt y leaves you with 3*(math.pi**4)*E*I/(W*L) ) wrt y 的偏导数给你留下3*(math.pi**4)*E*I/(W*L) )

There's only one term with C x**3 in it.只有一个术语包含 C x**3。 That's easy to differentiate: 3 C*x**2.这很容易区分:3 C**x**2。

What's so hard?什么这么难? What's the problem?有什么问题?

In traditional programming, each function you write is translated to a series of commands that are then sent to the CPU and the result of the calculation is returned.在传统编程中,您编写的每个函数都被转换为一系列命令,然后发送到 CPU 并返回计算结果。 Therefore, symbolic manipulation, like what we humans do with algebra and calculus, doesn't make any sense to the computer.因此,符号操作,就像我们人类对代数和微积分所做的那样,对计算机没有任何意义。 Sympy gets around this by overriding Python's normal arithmetic operators, allowing you to do generate algebraic functions that can be manipulated similarly to how we humans do math. Sympy通过覆盖 Python 的普通算术运算符来解决这个问题,允许您生成代数函数,这些函数可以像我们人类进行数学运算一样进行操作。 That's what sp.Symbols('x') is doing: providing you with a symbolic variable you can work with (you're also naming it in sympy).这就是sp.Symbols('x')正在做的事情:为您提供一个可以使用的符号变量(您也在 sympy 中命名它)。

If you want to evaluate your derivative, simply call evalf with the numerical value you want to assign to x.如果您想计算您的导数,只需调用evalf并使用您要分配给 x 的数值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM