Python實現(xiàn)之一階二階導(dǎo)數(shù)
本文轉(zhuǎn)載自微信公眾號「python與大數(shù)據(jù)分析」,作者一只小小鳥鳥。轉(zhuǎn)載本文請聯(lián)系python與大數(shù)據(jù)分析公眾號。
函數(shù)的和、差、積、商的求導(dǎo)法則
u=u(x),v=v(x)
(u+v)'=u'+v'
(u-v)'=u'-v'
(Cu)'=Cu'
(uv)'=u'v+uv'
(u/v)'=(u'v-uv')/v^2
復(fù)合函數(shù)求導(dǎo)法則
y=f(u),u=φ(v)
復(fù)合函數(shù)y=f[φ(v)]的導(dǎo)數(shù)為
dy/dx=dy/du*du/dx=f'(u)*φ'(v)
(u-v+z)'=u'-v'+z',且(Cu)'=Cu'
exam1:
y =2*x*^3 -5*x^2+3*x-7
y'=6*x^2-10x+3+0
exam2:
f(x)=x^3+4cosx-sin(π/2)
f'(x)=(x^3)‘+(4cosx)‘-(sin(π/2))‘=3x^2-4sinx-0
f'(π/2)=f'(x)|x=(π/2)=3x^2-4sinx=3*(π/2)^2-4sin(π/2)=3/4π^2-4
exam3:
y=√x*lnx
y'=(√x)'*lnx+√x*(lnx)'=1/(2*√x)*lnx+√x*1/x=1/(√x)*(1/2*lnx+1)
exam4:
y=e^x(sinx+cosx)
y'=(e^x)'(sinx+cosx)+e^x(sinx+cosx)'=e^x(sinx+cosx)+e^x(cosx-sinx)=2e^xcosx
高階導(dǎo)數(shù)
y=f(x)
y'=f'(x)
y''=(y')'=d^2y/dx^2=d/dx(dy/dx)
導(dǎo)數(shù)的應(yīng)用:函數(shù)單調(diào)性
通過函數(shù)的導(dǎo)數(shù)的值,可以判斷出函數(shù)的單調(diào)性、駐點以及極值點:
若導(dǎo)數(shù)大于0,則單調(diào)遞增;
若導(dǎo)數(shù)小于0,則單調(diào)遞減;
導(dǎo)數(shù)等于零d的點為函數(shù)駐點
曲線的凹凸性,設(shè)函數(shù)f(x) 在區(qū)間I 上有二階導(dǎo)數(shù)
(1) 在 I 內(nèi) f''(x)>0則 f(x)在 I 內(nèi)圖形是凹的 ;
(2) 在 I 內(nèi) f''(x)<0則 f(x)在 I 內(nèi)圖形是凸的 .
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- # _ooOoo_
- # o8888888o
- # 88" . "88
- # ( | - _ - | )
- # O\ = /O
- # ____/`---'\____
- # .' \\| |// `.
- # / \\|||:|||// \
- # / _|||||-:- |||||- \
- # | | \\\ - /// | |
- # | \_| ''\---/'' | _/ |
- # \ .-\__ `-` ___/-. /
- # ___`. .' /--.--\ `. . __
- # ."" '< `.___\_<|>_/___.' >'"".
- # | | : `- \`.;`\ _ /`;.`/ - ` : | |
- # \ \ `-. \_ __\ /__ _/ .-` / /
- # ==`-.____`-.___\_____/___.-`____.-'==
- # `=---='
- '''
- @Project :pythonalgorithms
- @File :Nderivatives.py
- @Author :不勝人生一場醉@Date :2021/8/3 1:17
- '''
- import matplotlib.pyplot as plt
- import numpy as np
- import math
- import sympy
- if __name__ == '__main__':
- nderivativeplot()
- # f(x)=x^3+3x^2-24x-20
- # f'(x)=3x^2+6x-24
- # f''(x)=6x+6
- def nderivativeplot():
- plt.figure(figsize=(5, 8))
- ax = plt.gca() # 通過gca:get current axis得到當(dāng)前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負號
- x = np.linspace(-10,10, 200)
- y = np.power(x,3)+3*np.power(x,2)-24*x-20
- yd = 3*np.power(x,2)+6*x-24
- ydd=6*x+6
- label = '函數(shù)f(x)=x^3+3x^2-24x-20的曲線'
- plt.plot(x, y, label=label)
- label = "導(dǎo)數(shù)f'(x)=3x^2+6x-24的曲線"
- plt.plot(x, yd, label=label)
- label = "導(dǎo)數(shù)f''(x)=6x+6的曲線"
- plt.plot(x, ydd, label=label)
- # 設(shè)置圖片的右邊框和上邊框為不顯示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過值來設(shè)置x軸的位置,將x軸綁定在y=0的位置
- ax.spines['bottom'].set_position(('data', 0))
- # axes表示以百分比的形式設(shè)置軸的位置,即將y軸綁定在x軸50%的位置
- # ax.spines['left'].set_position(('axes', 0.5))
- ax.spines['left'].set_position(('data', 0))
- plt.title("函數(shù)、一階導(dǎo)數(shù)、二階導(dǎo)數(shù)")
- plt.legend(loc='upper right')
- plt.show()