Consistant TypeError:只有 size-1 数组才能转换为 Python 标量

Consistant TypeError: only size-1 arrays can be converted to Python scalars

提问人:Aliyu A. Aziz 提问时间:11/11/2023 最后编辑:James ZAliyu A. Aziz 更新时间:11/11/2023 访问量:13

问:

我一直在尝试运行这个程序,但没有成功:

import numpy as np
from scipy.integrate import quad

# Constants
L = np.pi  # Length of the domain
n = 1      # Mode number

# Trial function
def trial_function(x):
    return np.sin(n * np.pi * x)

# Define the Mathieu equation
def mathieu_equation(x, X, A, q):
    return X * (A - 2 * q * np.cos(2 * x))

# Define the energy functional
def energy_functional(params):
    A, q = params
    def integrand(x):
        gradient = np.gradient(trial_function(x), x)
        return (0.5 * np.power(gradient, 2) + 0.5 * A * np.power(trial_function(x), 2) - q * np.power(trial_function(x), 2) * np.cos(2 * x))
    
    # Numerical integration using quad
    integral, _ = quad(integrand, 0, L)
    return integral.item()  # Ensure the result is a scalar

# Find A and q by minimizing the energy functional
from scipy.optimize import minimize

# Minimize the energy functional with initial guesses for A and q
initial_guess = [1.0, 1.0]
result = minimize(energy_functional, initial_guess, method='L-BFGS-B')

# Extract the optimized values
A_opt, q_opt = result.x

print(f"Optimized values - A: {A_opt}, q: {q_opt}")

我收到以下错误消息:

Traceback (most recent call last):

  File C:\anaconda3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File g:\my drive\mathieu_eqn\untitled35.py:39
    result = minimize(energy_functional, initial_guess, method='L-BFGS-B')

  File C:\anaconda3\lib\site-packages\scipy\optimize\_minimize.py:696 in minimize
    res = _minimize_lbfgsb(fun, x0, args, jac, bounds,

  File C:\anaconda3\lib\site-packages\scipy\optimize\_lbfgsb_py.py:305 in _minimize_lbfgsb
    sf = _prepare_scalar_function(fun, x0, jac=jac, args=args, epsilon=eps,

  File C:\anaconda3\lib\site-packages\scipy\optimize\_optimize.py:332 in _prepare_scalar_function
    sf = ScalarFunction(fun, x0, args, grad, hess,

  File C:\anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py:158 in __init__
    self._update_fun()

  File C:\anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py:251 in _update_fun
    self._update_fun_impl()

  File C:\anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py:155 in update_fun
    self.f = fun_wrapped(self.x)

  File C:\anaconda3\lib\site-packages\scipy\optimize\_differentiable_functions.py:137 in fun_wrapped
    fx = fun(np.copy(x), *args)

  File g:\my drive\mathieu_eqn\untitled35.py:31 in energy_functional
    integral, _ = quad(integrand, 0, L)

  File C:\anaconda3\lib\site-packages\scipy\integrate\_quadpack_py.py:463 in quad
    retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit,

  File C:\anaconda3\lib\site-packages\scipy\integrate\_quadpack_py.py:575 in _quad
    return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)

TypeError: only size-1 arrays can be converted to Python scalars

我尝试更改集成方法但没有成功。

参数 类型错误

评论


答: 暂无答案