2017-07-16 156 views
2

給出的等式2的z = z(x,y)表面III查找方程y = Y(x)的從兩個表面Z = Z(X,Y)的交點

z_I(x, y) = a0 + a1*y + a2*x + a3*y**2 + a4*x**2 + a5*x*y 
z_II(x, y) = a0_s2 + a1_s2*y + a2_s2*x + a3_s2*y**2 + a4_s2*x**2 + a5_s2*x*y 

enter image description here

的參數在代碼中給出(如下)。

兩個表面的交點是滿足的時候:

z_I(x, y) = z_II(x, y) 

我怎麼能找到此交匯式y=y(x)

代碼:

import numpy as np 
from mpl_toolkits.mplot3d import Axes3D 
import matplotlib 
import matplotlib.pyplot as plt 

# parameters: 
a0 = -941.487789748 
a1 = 0.014688246093 
a2 = -2.53546607894e-05 
a3 = -9.6435353414e-05 
a4 = -2.47408356408e-08 
a5 = 3.77057147803e-07 

a0_s2 = -941.483110904 
a1_s2 = 0.01381970471 
a2_s2 = -2.63051565187e-05 
a3_s2 = -5.5529184524e-05 
a4_s2 = -2.46707082089e-08 
a5_s2 = 3.50929634874e-07 


print """ 

The equations are the following: 

z_I (x, y) = a0 + a1*y + a2*x + a3*y**2 + a4*x**2 + a5*x*y 
z_II (x, y) = a0_s2 + a1_s2*y + a2_s2*x + a3_s2*y**2 + a4_s2*x**2 + a5_s2*x*y 

""" 
print('z_I (x, y) = ({a0}) + ({a1})*y + ({a2})*x ({a3})*y**2 ({a4})*x**2 + ({a5})*x*y'.format(a0 = a0, a1 = a1, a2 = a2, a3 = a3, a4 = a4, a5 = a5)) 

print """ 
""" 
print('z_II (x, y) = ({a0_s2}) + ({a1_s2})*y + ({a2_s2})*x ({a3_s2})*y**2 ({a4_s2})*x**2 + ({a5_s2})*x*y'.format(a0_s2 = a0_s2, a1_s2 = a1_s2, a2_s2 = a2_s2, a3_s2 = a3_s2, a4_s2 = a4_s2, a5_s2 = a5_s2)) 

print """ 
""" 

print """ 
The intersection of both surfaces is satisfied when: 

z_I(x, y) = z_II(x, y) 

In other words, I am looking for the expression of the function y=y(x) 

""" 

##### For plotting: 
x_mesh = np.linspace(10.0000000000000, 2000.0000000000000, 200) 
x_mesh_2 = np.linspace(10.0000000000000, 2000.0000000000000, 200) 
print x_mesh[0] 
print x_mesh[-1] 
y_mesh = np.linspace(-4.4121040129800, 10.8555489379000, 200) 
y_mesh_2 = np.linspace(8.0622039627300, 17.6458151433000, 200) 
print y_mesh[0] 
print y_mesh[-1] 

xx, yy = np.meshgrid(x_mesh, y_mesh) 
xx_2, yy_2 = np.meshgrid(x_mesh_2, y_mesh_2) 

z_fit = a0 + a1*yy + a2*xx + a3*yy**2 + a4*xx**2 + a5*xx*yy 
z_fit_2 = a0_s2 + a1_s2*yy_2 + a2_s2*xx_2 + a3_s2*yy_2**2 + a4_s2*xx_2**2 + a5_s2*xx_2*yy_2 


# set "fig" and "ax" varaibles 
fig = plt.figure() 
ax = fig.gca(projection='3d') 

# Plot the original function 
ax.plot_surface(xx, yy, z_fit, color='y', alpha=0.5) 
ax.plot_surface(xx_2, yy_2, z_fit_2, color='g', alpha=0.5) 

ax.set_xlabel('x') 
ax.set_ylabel('y') 
ax.set_zlabel('\nz', linespacing=3) 

plt.show() 

EDIT

作爲@Alex指出,獲得2個解決方案,即,兩個表面相交限定兩條曲線:

enter image description here

如果你運行下面的代碼,這些是o btained在sol

sol = sym.solve(z_I(x,y) - z_II(x,y), y)

現在,如果我們繪製這兩條曲線(所有這一切都是在下面的代碼),我們發現這兩個分支:

enter image description here

我意識到,在一個表面(即綠色表面)位於紅黃色頂部的情況下,對於域爲xy的情況是如此:

enter image description here

我想找到這兩個分支(二維圖中的藍色和橙色)之間的交集。

根據什麼進行了討論,這可以通過sym.solve太完成:

cross = sym.solve(y_sol_z_I_sym(x) - y_sol_z_II_sym(x), x)

不過,這一說法不符合sym.sqrt工作(它應該,因爲平方根作爲象徵性的處理。 ..)

你知道問題在哪裏嗎?

代碼:

import numpy as np 
import sympy as sym 
from mpl_toolkits.mplot3d import Axes3D 

import matplotlib.pyplot as plt 



a0 = -941.487789748 
a1 = 0.014688246093 
a2 = -2.53546607894e-05 
a3 = -9.6435353414e-05 
a4 = -2.47408356408e-08 
a5 = 3.77057147803e-07 

a0_s2 = -941.483110904 
a1_s2 = 0.01381970471 
a2_s2 = -2.63051565187e-05 
a3_s2 = -5.5529184524e-05 
a4_s2 = -2.46707082089e-08 
a5_s2 = 3.50929634874e-07 

x, y = sym.symbols('x y', real=True) 


def z_I(x,y): 
     return a0 + a1*y + a2*x + a3*y**2 + a4*x**2 + a5*x*y 

def z_II(x,y): 
     return a0_s2 + a1_s2*y + a2_s2*x + a3_s2*y**2 + a4_s2*x**2 + a5_s2*x*y 

sol = sym.solve(z_I(x,y) - z_II(x,y), y) 
print 'sol =', sol 

# For obtaining the plot of the two branches y=y(x), we need np.sqrt 
def y_sol_z_I(x): 
    return 0.000319359080035813*x - 1.22230952828787e-15*np.sqrt(-1.07919313606384e+24*x**2 + 2.00910207755286e+28*x - 1.12101975048632e+30) + 10.6162640815323 


def y_sol_z_II(x): 
    return 0.000319359080035813*x + 1.22230952828787e-15*np.sqrt(-1.07919313606384e+24*x**2 + 2.00910207755286e+28*x - 1.12101975048632e+30) + 10.6162640815323 

# For obtaining where the two branches y=y(x) intersect, we need sym.sqrt 
def y_sol_z_I_sym(x): 
    return 0.000319359080035813*x - 1.22230952828787e-15*sym.sqrt(-1.07919313606384e+24*x**2 + 2.00910207755286e+28*x - 1.12101975048632e+30) + 10.6162640815323 


def y_sol_z_II_sym(x): 
    return 0.000319359080035813*x + 1.22230952828787e-15*sym.sqrt(-1.07919313606384e+24*x**2 + 2.00910207755286e+28*x - 1.12101975048632e+30) + 10.6162640815323 


cross = sym.solve(y_sol_z_I_sym(x) - y_sol_z_II_sym(x), x) 
print ' cross = ', cross 


##### Plotting: 
# set "fig" and "ax" varaibles 
fig = plt.figure() 
ax = fig.gca(projection='3d') 

# Plot the original function: 
x_mesh = np.linspace(10.0000000000000, 2000.0000000000000, 20) 
x_mesh_2 = np.linspace(10.0000000000000, 2000.0000000000000, 20) 
print x_mesh[0] 
print x_mesh[-1] 
y_mesh = np.linspace(-4.4121040129800, 10.8555489379000, 20) 
y_mesh_2 = np.linspace(8.0622039627300, 17.6458151433000, 20) 
print y_mesh[0] 
print y_mesh[-1] 

xx, yy = np.meshgrid(x_mesh, y_mesh) 
xx_2, yy_2 = np.meshgrid(x_mesh_2, y_mesh_2) 

ax.plot_surface(xx, yy, z_I(xx ,yy), color='y', alpha=0.5) 
ax.plot_surface(xx_2, yy_2, z_II(xx_2, yy_2), color='g', alpha=0.5) 

ax.set_xlabel('x') 
ax.set_ylabel('y') 
ax.set_zlabel('\n z, linespacing=3') 

# New figure for the y=y(x) function: 
fig = plt.figure() 
x = np.linspace(10.0, 2000.0, 10000) 
plt.plot(x, y_sol_z_I(x)) 
plt.plot(x, y_sol_z_II(x)) 
plt.xlabel('x') 
plt.ylabel('y') 
plt.title('Exact expression of y=y(x)\nas a result of making $z^{I}(x,y)=z^{II}(x,y)$') 
tics_shown = [10, 250, 500, 750, 1000, 1250, 1500, 1750, 2000, 2250] 
plt.xticks(tics_shown) 
plt.grid() 


# New figure for the y=y(x) function in circle: 
fig = plt.figure() 
x_circle = np.linspace(10.0, 2000.0*100, 10000*100) 
plt.plot(x_circle, y_sol_z_I(x_circle)) 
plt.plot(x_circle, y_sol_z_II(x_circle)) 
plt.xlabel('x') 
plt.ylabel('y') 
plt.title('Exact expression of y=y(x)\nas a result of making $z^{I}(x,y)=z^{II}(x,y)$') 
plt.grid() 


plt.show() 
+0

「我怎麼能找到這個交點的方程y = y(x)?」 - 您需要求解由'z_I(x,y)= z_II(x,y)'給出的二次方程。 – tarashypka

+0

你在問數學問題嗎? – wwii

回答

1

既然你正在尋找一個象徵解決方案(而不是數字),你需要象徵性的操作,如SymPy庫。

import sympy as sym 
x, y = sym.symbols('x y', real=True) 
z_I = a0 + a1*y + a2*x + a3*y**2 + a4*x**2 + a5*x*y 
z_II = a0_s2 + a1_s2*y + a2_s2*x + a3_s2*y**2 + a4_s2*x**2 + a5_s2*x*y 
sol = sym.solve(z_I-z_II, y) 

數組sol包含兩個解,這對二次方程來說並不罕見。

[0.000319359080035813*x - 1.22230952828787e-15*sqrt(-1.07919313606384e+24*x**2 + 2.00910207755286e+28*x - 1.12101975048632e+30) + 10.6162640815323, 
0.000319359080035813*x + 1.22230952828787e-15*sqrt(-1.07919313606384e+24*x**2 + 2.00910207755286e+28*x - 1.12101975048632e+30) + 10.6162640815323] 

如果你想找到他們見面,用

cross = sym.solve(sol[0]-sol[1]) 

返回[55.9652951064934, 18560.7401898885],交點的x座標。

+0

感謝您的回答。我一直在思考這兩個解決方案,並得出了一個結論。請參閱上面的編輯。再次感謝 –

+1

您試圖將Python函數(如同使用SciPy的數值解算器)等同於SymPy表達式。查看更新後的答案。 – FTP

+0

'sym.solve(sol [0] -sol [1])'返回錯誤RuntimeError:在調用Python對象時超出最大遞歸深度感謝 –

相關問題