Skip to main content
 首页 » 编程设计

python之2 个地 block 的交集(鼠尾草)

2025年05月04日6exmyth

你能帮我找到图形中的交点吗

import numpy as np 
import matplotlib.pyplot as plt 
 
@interact 
def foo(count=(1,10,1), t=slider(100,1000,100,default=100)): 
 
    plt.cla()     
 
    dt = 1/t 
 
    x = np.arange(1, step=dt) 
    xl = np.arange(1, step=.000001) 
 
    W = np.zeros(t, np.dtype(float)) 
 
#Iteration Logarithm 
    l = np.sqrt(2*xl*ln(ln(1/xl))) 
    plt.plot(xl, l,'r--')     
    plt.plot(xl, -l,'r--')   
 
    mu, sig = 0, 1 
 
    for ITER in range(1, count+1): #count of W[i] in plot 
        for i in range(1, len(W)): 
            W[i] = W[i-1] + np.random.normal(mu, sig) * np.sqrt(dt) #Wiener process 
        plt.plot(x,W)             
 
    plt.xlabel('t',fontsize=26) 
    plt.ylabel('W(t)',fontsize=26) 
    plt.grid(True) 
    @interact 
    def _dee(deep=slider(0.0001, .4, 0.001, default=.2)): #scale 
        plt.xlim(0, deep) 
        plt.ylim(-.5, .5) 
        plt.show() 

我使用 https://cloud.sagemath.com 无法实现,如何找到图形相互交叉的点。 谢谢。

请您参考如下方法:

虽然我不熟悉 sage,但给定两个采样函数得到交点应该很容易 - 只需计算它们之间的差异,每次符号都会有一个交点变化。

step=0.1 
x= np.arange(-5,5, step=step) 
 
f1 = x**2  - 4 
f2 = -x**2 + 4 
 
dif= f2-f1 
dif_sign= dif/np.abs(dif) #just the sign of the function 
dif_sign_dif= np.diff(dif_sign) #places where the sign changes 
change_idxs= np.where(dif_sign_dif!=0)[0] #indexes where the sign changes 
print -5+change_idxs*step 

输出:

[-2.  2.] 

不过,由于您要添加布朗运动,因此偶尔会出现“误报”。