Python實(shí)現(xiàn)之初等函數(shù)二之反函數(shù)
本文轉(zhuǎn)載自微信公眾號(hào)「python與大數(shù)據(jù)分析」,作者一只小小鳥(niǎo)鳥(niǎo)。轉(zhuǎn)載本文請(qǐng)聯(lián)系python與大數(shù)據(jù)分析公眾號(hào)。
一般來(lái)說(shuō),設(shè)函數(shù)y=f(x)(x∈A)的值域是C,若找得到一個(gè)函數(shù)g(y)在每一處g(y)都等于x,這樣的函數(shù)x= g(y)(y∈C)叫做函數(shù)y=f(x)(x∈A)的反函數(shù),記作x=f-1(y) 。反函數(shù)x=f -1(y)的定義域、值域分別是函數(shù)y=f(x)的值域、定義域。最具有代表性的反函數(shù)就是對(duì)數(shù)函數(shù)與指數(shù)函數(shù)。
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- # _ooOoo_
- # o8888888o
- # 88" . "88
- # ( | - _ - | )
- # O\ = /O
- # ____/`---'\____
- # .' \\| |// `.
- # / \\|||:|||// \
- # / _|||||-:- |||||- \
- # | | \\\ - /// | |
- # | \_| ''\---/'' | _/ |
- # \ .-\__ `-` ___/-. /
- # ___`. .' /--.--\ `. . __
- # ."" '< `.___\_<|>_/___.' >'"".
- # | | : `- \`.;`\ _ /`;.`/ - ` : | |
- # \ \ `-. \_ __\ /__ _/ .-` / /
- # ==`-.____`-.___\_____/___.-`____.-'==
- # `=---='
- '''
- @Project :pythonalgorithms
- @File :Inversefunction.py
- @Author :不勝人生一場(chǎng)醉@Date :2021/7/29 23:17
- '''
- import matplotlib.pyplot as plt
- import numpy as np
- if __name__ == '__main__':
- inversefunction()
- def inversefunction():
- plt.figure(figsize=(5, 15))
- ax = plt.gca() # 通過(guò)gca:get current axis得到當(dāng)前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負(fù)號(hào)
- x = np.linspace(-2, 2, 100)
- y1 = np.power(x, 3)
- y2 = np.power(abs(x), 1 / 3) * np.sign(x)
- y3 = x
- label = 'np.power(x,3)'
- plt.plot(x, y1, label=label)
- label = 'np.power(x,1/3)'
- plt.plot(x, y2, label=label)
- # plt.plot(y1,x,label=label)
- # np.power(x,1/3)和x,y1調(diào)換一下是等價(jià)的
- label = 'y=x'
- plt.plot(x, y3, label=label)
- # 設(shè)置圖片的右邊框和上邊框?yàn)椴伙@示
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過(guò)值來(lái)設(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ù)")
- plt.legend(loc='upper right')
- plt.show()
- # 反函數(shù)與原函數(shù)的復(fù)合函數(shù)等于x
- plt.figure(figsize=(5, 5))
- ax = plt.gca() # 通過(guò)gca:get current axis得到當(dāng)前軸
- plt.rcParams['font.sans-serif'] = ['SimHei'] # 繪圖中文
- plt.rcParams['axes.unicode_minus'] = False # 繪圖負(fù)號(hào)
- x = np.linspace(-2, 2, 100)
- y1 = np.power(x, 3)
- y2 = np.power(abs(y1), 1 / 3) * np.sign(y1)
- label = 'np.power(abs(np.power(x, 3)), 1 / 3) * np.sign(np.power(x, 3))'
- plt.plot(x, y2, label=label)
- ax.spines['right'].set_color('none')
- ax.spines['top'].set_color('none')
- # 挪動(dòng)x,y軸的位置,也就是圖片下邊框和左邊框的位置
- # data表示通過(guò)值來(lái)設(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ù)與原函數(shù)的復(fù)合函數(shù)")
- plt.legend(loc='upper right')
- plt.show()