如何生成多个具有截然不同的域的图,以便每次都能轻松地看到它们的根?

2024-06-07 13:59:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前的代码有问题。在某些背景下,我试图找到函数ces_dd的根,使用y、rho和ud的固定值。我使用函数ces_inv来查找根,它对应于这个包中的第一个函数(https://quanteconpy.readthedocs.io/en/latest/optimize/root_finding.html

然而,为了做到这一点,我需要起始值(接近根所在位置的x值)。因此,我需要ces_dd的绘图,以及各种不同的rho固定值(保持所有其他值相等),以可视化根的位置并选择起始值

唯一的问题是,matplotlib没有给我提供非常有用的说明,因为它没有显示它与x轴相交的位置。我已经尝试过一些方面,例如从中间绘制一个轴(How to draw axis in the middle of the figure?),但是这也不起作用,因为有一些非常大的根(例如1566),在这里绘制原点的软件失败了。p>

如果你们中有人能找到解决办法,请告诉我。此外,我是stackoverflow的新手,所以对这篇文章的任何反馈以及我如何改进都将不胜感激。多谢各位

相关部分如下:

##Function I want to find the root of##
@jit
def ces_dd(x,y,rho,ud):
    U_xd = (x**(rho-1)) * (x**rho + y**rho)**((1/rho) - 1) - ud 
    return U_xd # New Function: CES Derivative - ud

v1 = np.arange(0.10,1,0.10) ##Testing it on Different Values of Rho, all else equal)##

for i in v1:
    x = np.arange(-2000, 2000)
    z = ces_dd(x,5,i,3)
    plt.subplots()
    plt.plot(x,z)
    plt.show()  #The plots produced are not very helpful, roots are not visible

设置代码

from numba import jit
import numpy as np 
import matplotlib.pyplot as plt

####################QuantEcon Methods#######################
from quantecon.optimize.root_finding import newton, brentq

##Coded Up Functions (Jitted)##

##Original Function##
@jit 
def ces(x,y,rho):
    U = (x**rho + y**rho)**(1/rho)
    return U # original CES Function

##Derivative of that Function##
@jit
def ces_d(x,y,rho):
    U_x = (x**(rho-1)) * (x**rho + y**rho)**((1/rho) - 1) 
    return U_x # CES Derivative

##Function I want to find the root of##
@jit
def ces_dd(x,y,rho,ud):
    U_xd = (x**(rho-1)) * (x**rho + y**rho)**((1/rho) - 1) - ud 
    return U_xd # New Function: CES Derivative - ud

##Needed to put into ces_inv##
@jit
def ces_d2(x,y,rho,z):
    U_xx = (rho-1) * (x**(rho-2)) * (x**rho + y**rho)**((1/rho)-1) \
    + ((1/rho) - 1)*rho*x**((2*rho)-2)*((x**rho + y**rho))**((1/rho)-2)
    return U_xx #Second Derivative of New Function

def ces_inv(y,rho,ud): #Root-Finding Algorithim##
 args = (y,rho,ud) #order must be preserved
 x_bar = newton(ces_dd, 0.002 ##placeholder starting value ,ces_d2, args=args)
 return x_bar 

Tags: ofthetoreturndeffunctionrootdd

热门问题