如何解决和使用numpy数组

时间:2019-03-09 05:36:25

标签: python numpy sympy

我想求解以下方程式

$ \ phi ^ 2 \ sqrt {\ frac {3M ^ 2 a ^ 2} {4 \ pi \ mu} \ phi ^ 2 +1} = \ frac {M ^ 2} {4 \ pi} $ < / p>

并绘制其解决方案,但出现错误。

from sympy import *
init_printing()
x = Symbol("x",positive=True,real=True)
A = Symbol("A",positive=True,real=True)
B = Symbol("B",positive=True,real=True)

Eq = x**2 * sqrt(A*x**2+1) - B
sol = solve(Eq,x)
solution_1 = sol[1]

M = Symbol("M",positive=True,real=True)
mu = Symbol("\mu",positive=True)
a = Symbol("a",positive=True)
phi = Symbol("\phi",positive=True)

solution_2  =  solution_1.subs({
                            A : 3*M**2 *a**2/(4*pi*mu),
                            B : M**2/(4*pi)
                            })

然后我要绘制它

import numpy as np
import matplotlib.pyplot as plt
Ps = 2.19E-9
M  = 1.22E19
N = 60
def oddpow(x,y):
    output = np.empty(len(x))
    for i in range(len(x)):
        if x[i] >= 0:
            output[i] = x[i]**y
        if x[i] <= 0:
            output[i] = -(-x[i])**y
    return output

mu = np.linspace(1e-1,1e-10)

a = (3/(np.pi*M**2*Ps))**(-0.5) * (np.sqrt(M**2/(12*np.pi))*(3*N+1))**(-1)
x1 = np.sqrt(2*np.pi*mu)/(3*M*a)
x2 = (-243*M**8*a**4/(256*np.pi**4*mu**2)+9*np.sqrt(3)*M**4*a**2/(16*np.pi**2*mu)*np.sqrt(243*M**8*a**4/(256*np.pi**4*mu**2)-4)+2)

x3  = oddpow(x2,1/3)
phi = x1 * np.sqrt(-2**(2/3)*x3-2-2*(2)**(1/3)/x3)

但是我明白了

RuntimeWarning: invalid value encountered in sqrt
  """Entry point for launching an IPython kernel.

0 个答案:

没有答案