• 欢迎访问GanAHE的小创元世纪,技术交流与资源分享,欢迎加入GanAHE的小创元世纪🤪 微信公众号:星辰换日
  • 电影、教程、软件等分享资源请在本站或微信公众号合作平台:【星辰换日】获取。
  • 首页展示个人参与或独立开发的产品,更多功能在建中......🙆🏼天王盖地虎
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏本站吧🥺
  • 注册采用有效的邮箱,有助于及时收到评论回复、最新资源以及活动通知哟~✔🥳
  • 目前在逐渐打通微信小程序与网站之前的间隔~💻🏢

[Python&Advances Mathematics Project # 2] How to take the derivative of a function by Python

Uncategorized GanAHE 8个月前 (09-08) 52次浏览 已收录 0个评论 扫描二维码
文章目录[隐藏]

title: >-
[Python&Advances Mathematics Project # 2] How to take the derivative of a
function by Python
type: categories
copyright: true
categories: Python
tags:

  • Python
  • 高等数学
  • 图像绘制与可视化
  • matplotlib
  • 考研数学工具
    comments: true
    top: false
    abbrlink: 2ad96df3
    date: 2020-08-06 21:00:49
    updated:

how can we take the derivative of a function by Python?

(1)Background And Introduction

How do you prove that the derivative of your function is correct?

You can’t know for sure if the derivative is correct by hand, but we can write a math library in Python to verify it

(2)Derivative

This is some mindmaps about how to derivative the function (by the way,the language of these images is Chinese):


(3)Ex.

(3.1)Ex. 1

Let’s derivative this function and caculate the value of derivative while the xo equals to one.

What does this function look like?

Let’s draw it( You can learn how to plot a function by this passage which I writen if you don’t how to plot a function in Python):

We can caculate it to the result like this:

Ok, now we will use the Lib which named Sympy of Mathematics in Python,, to solve it:

#!usr/bin/env python
# -*- conding:utf-8 -*-

# import the Lib
from sympy import *
import matplotlib.pyplot as plt

# define a function to caculate
# we can use the function to derivative the 
# function what you want to solve
def derivative(draw = False):
    """
    derivate
    @para draw: Bool,plot function or not
    """
    x,f = symbols("x,f")
    dx = 1
    # define a function which will be derivatived
    f = log(1+exp(x**2))
    print("the derivative of function :",diff(f,x))
    if dx != None:
        print("the value of derivative in xo (xo = {}):{}".format(dx,f.evalf(subs ={'x':dx})))
    if draw:
        ezplot = lambda expr:plot_implicit(sympify(expr))
        ezplot(f)

Calling derivative function: derivative(False) and we will take the derivative of it. Now we got the result:

the function of derivative like this:

(3.2)Ex. 2

We can get first derivative in default solution.

When we need to find the higher derivative, we can get the higher derivative through the following code:

#!usr/bin/env python
# -*- conding:utf-8 -*-

# import the Lib
from sympy import *
import matplotlib.pyplot as plt

# define a function to caculate
# we can use the function to derivative the 
# function what you want to solve
def derivative(draw = False):
    """
    derivate
    @para draw: Bool,plot function or not
    """
    x,f = symbols("x,f")
    dx = 1
    # define a function which will be derivatived
     n = 2
    f = x**3
    print("the derivative ({}) of function :{}".format(n,diff(f,x,n)))
    if dx != None:
        print("the value of derivative in xo (xo = {}):{}".format(dx,f.evalf(subs ={'x':dx})))
    if draw:
        ezplot = lambda expr:plot_implicit(sympify(expr))
        ezplot(f)

for example, We will take the second derivative of function: y = x^3 :

(4)Confusing Things

I try to show the function figure in Sympy code but failed.

    if draw:
        ezplot = lambda expr:plot_implicit(sympify(expr))
        ezplot(f)

You can find that the canvas have nothing on it.

Obviously, there are many problems with my code, so you can commit your correct code to me in comments if you are willing to help me and other learners!


GanAHE 的小创元世纪, 版权所有丨如未注明 , 均为原创丨本网站采用@BY-NC-SA协议进行授权
转载请注明原文链接:[Python&Advances Mathematics Project # 2] How to take the derivative of a function by Python
喜欢 (0)
[WeChat]
分享 (0)
关于作者:
网站的建设与维护者、DGZC的守护者
0 0 评分
都看完了,打个分儿叭~
通知
提醒
0 评论
内部反馈
显示所有评论
0
有什么想法吗,快来留下你的评论吧!x
()
x