【问题标题】:Solve non linear equation numpy.求解非线性方程 numpy。
【发布时间】:2018-08-19 18:28:36
【问题描述】:

编辑: 一切都很好:)

 This is a code which works with small values of t=20 and TR=([[30,20,12,23..],[...]]) but when I put higher values it is shown "Expect x to be a 1-D sorted array_like.". Do you know how to solve this problem??  

import matplotlib.pylab as plt
from scipy.special import erfc
from scipy import  sqrt
from scipy import  exp
import numpy as np
from scipy.interpolate import interp1d




# The function to inverse:
t = 100
alfa = 1.1*10**(-7)
k = 0.18
T1 = 20
Tpow = 180

def F(h):
    p = erfc(h*sqrt(alfa*t)/k)
    return T1 + (Tpow-T1)*(1-exp((h**2*alfa*t)/k**2)*(p))

# Interpolation 
h_eval = np.linspace(-80, 500, 200)   # critical step: define the discretization grid
F_inverse = interp1d( F(h_eval), h_eval, kind='cubic', bounds_error=True )


# Some random data:
TR = np.array([[130, 100, 130, 130, 130],
       [ 90, 101, 100, 120,  90],
       [130, 130, 100, 100, 130],
       [120, 101, 120,  90, 110],
       [110, 130, 130, 110, 130]])

# Compute the array h for a given array TR
h = F_inverse(TR)
print(h)

# Graph to verify the interpolation 
plt.plot(h_eval, F(h_eval), '.-', label='discretized F(h)');
plt.plot(h.ravel(), TR.ravel(), 'or', label='interpolated values')
plt.xlabel('h'); plt.ylabel('F(h) or TR'); plt.legend();


有谁知道如何在 numpy 中求解非线性隐式方程。 我的等式中包含数组 TR 和其他值。

我需要解决它 - 结果收到一个形状相同的新数组

【问题讨论】:

  • F(h) 在做什么?您面临的具体问题是什么?
  • 我有方程:0=TR+(Tpow-T1)*(1-np.exp(h2*alfat/k2 )+2/(3*np.sqrt(3))*h3*(alfat)**(3/2)/k3) 其中 h 是我的未知值。我想找到那个方程的解。 (考虑到 TR 是一个数组)提前谢谢

标签: python arrays numpy scipy nonlinear-optimization


【解决方案1】:

这是使用一维插值计算F(h) 函数的逆的解决方案。由于未使用standard root finding method,因此无法控制误差,必须谨慎选择离散化网格。但是,插值反函数可以直接在数组上计算。

注意:F的定义被修改,问题现在是Solve h for F(h) = TR

import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pylab as plt

# The function to inverse:
t = 10
alfa = 1.1*10**(-7)
k = 0.18
T1 = 20
Tpow = 100

def F(h):
    A = np.exp(h**2*alfa*t/k**2)
    B = h**3*2/(3*np.sqrt(3))*(alfa*t)**(3/2)/k**3
    return -(Tpow-T1)*( 1 - A + B )

# Interpolation 
h_eval = np.linspace(40, 100, 50)   # critical step: define the discretization grid
F_inverse = interp1d( F(h_eval), h_eval, kind='cubic', bounds_error=True )


# Some random data:
TR = np.array([[13, 10, 13, 13, 13],
       [ 9, 11, 10, 12,  9],
       [13, 13, 10, 10, 13],
       [12, 11, 12,  9, 11],
       [11, 13, 13, 11, 13]])

# Compute the array h for a given array TR
h = F_inverse(TR)
print(h)

# Graph to verify the interpolation 
plt.plot(h_eval, F(h_eval), '.-', label='discretized F(h)');
plt.plot(h.ravel(), TR.ravel(), 'or', label='interpolated values')
plt.xlabel('h'); plt.ylabel('F(h) or TR'); plt.legend();

使用其他功能,更改以下行:

from scipy.special import erf
def F(h):
    return (Tpow-T1)*(1-np.exp((h**2*alfa*t)/k**2)*(1.0-erf(h*np.sqrt(alfa*t)/k)))

# Interpolation 
h_eval = np.linspace(15, 35, 50)   # the range is changed 

【讨论】:

  • 非常感谢 - 它适用于该等式但是当我尝试使用 (Tpow-T1)*(1-exp((h**2*alfa*t)/k**2)*(1.0-erf(h*sqrt(alfa*t)/k))) 而不是 -(Tpow-T1)*( 1 - A + B ) 时出现错误:AttributeError: 'ImmutableDenseNDimArray' object has no attribute 'extract_multiplicatively'
  • 我测试了这个函数,它运行良好:你使用哪个erf函数?,它来自Scipy?
  • 我刚刚更新了所有代码来检查 - 如果你当然可以:)
  • 注意数学函数是从哪个库导入的:scipynumpy 可用于 nd-array,但 sympy 有所不同,不适用于 nd-array
  • 非常感谢 - 现在一切正常
猜你喜欢
  • 2019-10-02
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多