将 Matlab @ 翻译成 Python 代码(运行时警告:true_divide 中遇到无效值)
Translating Matlab @ into Python code (RuntimeWarning: invalid value encountered in true_divide )
我正在将 Matlab 代码翻译成 python,但我被困在这里了。
我认为 @ 它正在创建一个局部变量,对吗?
我试图用嵌套函数来解释代码,但在处理其中一个变量时似乎有问题。
#Code's variable:
#LL0 = first tentative value
#Sad and f = returns of psd estimation via welch's method
#Sad = is the psd of discrete time-series
#f = is an array of the sampled frequencies
#u_mean = is a scalar
% Matlab Code
f = @(LL, Sad, n, U_media)sum((Sad - 4 * n * LL / U_media .* (1 + 70.8 * (n * LL / U_media).^2).^(-5/6)).^2);
fun = @(LL)f(LL, Sad, n, U_media);
LL = fminsearch(fun, LL0);
def f1(LL, Sad, n, u_mean):
a = sum((Sad - 4 * n * LL / u_mean * (1 + 70.8 * (n * LL / u_mean)**2)**(-5/6))**2)
return(a)
f2 = lambda LL, Sad, u_mean, f: f1(LL, Sad, n, u_mean)
fun = lambda LL: f2(LL, Sad, n, u_mean)
LL = scipy.optimize.fmin(func=fun, x0=LL0, maxfun=100000, xtol=1e-6, maxiter=10000, disp=True)
代码是 运行 但迭代给了我这个输出
RuntimeWarning: invalid value encountered in true_divide
sum((S_adim - 4 * f * LL / u_mean * (1 + 70.8 * (f * LL / u_mean**2)**(-5/6)))**2)
RuntimeWarning: invalid value encountered in reduce
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
Warning: Maximum number of iterations has been exceeded.
我在 return 中得到的值与我用作暂定值的值相同
谁能帮我?提前致谢
终于找到解决办法了;我不太确定为什么现在它工作认为我分配了两次相同的变量。无论如何,这是我得出的解决方案:
def f(LL, Sad, n, u_mean):
f = sum((Sad - (4 * n * LL / u_mean) *
((1 + 70.8 * ((n * LL / u_mean) ** (2)))**(-5 / 6)))**2)
return(f)
fun = lambda LL: f(LL, Sad, n, u_mean)
res = scipy.optimize.minimize(fun=fun, x0=LL0)
Lux = res.x
顺便说一句,spyder idle 一直说我不应该使用 lambda 函数,而是定义一个函数。谁能告诉我为什么?我能够将 "fun" 翻译成一个函数;我试过:
def fun(LL):
f(LL, Sad, n, u_mean)
return(f)
res = scipy.optimize.minimize(fun=fun, x0=LL0)
但它不起作用。如果有人能告诉我原因就太好了。
感谢您的帮助
我正在将 Matlab 代码翻译成 python,但我被困在这里了。 我认为 @ 它正在创建一个局部变量,对吗? 我试图用嵌套函数来解释代码,但在处理其中一个变量时似乎有问题。
#Code's variable:
#LL0 = first tentative value
#Sad and f = returns of psd estimation via welch's method
#Sad = is the psd of discrete time-series
#f = is an array of the sampled frequencies
#u_mean = is a scalar
% Matlab Code
f = @(LL, Sad, n, U_media)sum((Sad - 4 * n * LL / U_media .* (1 + 70.8 * (n * LL / U_media).^2).^(-5/6)).^2);
fun = @(LL)f(LL, Sad, n, U_media);
LL = fminsearch(fun, LL0);
def f1(LL, Sad, n, u_mean):
a = sum((Sad - 4 * n * LL / u_mean * (1 + 70.8 * (n * LL / u_mean)**2)**(-5/6))**2)
return(a)
f2 = lambda LL, Sad, u_mean, f: f1(LL, Sad, n, u_mean)
fun = lambda LL: f2(LL, Sad, n, u_mean)
LL = scipy.optimize.fmin(func=fun, x0=LL0, maxfun=100000, xtol=1e-6, maxiter=10000, disp=True)
代码是 运行 但迭代给了我这个输出
RuntimeWarning: invalid value encountered in true_divide
sum((S_adim - 4 * f * LL / u_mean * (1 + 70.8 * (f * LL / u_mean**2)**(-5/6)))**2)
RuntimeWarning: invalid value encountered in reduce
return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
Warning: Maximum number of iterations has been exceeded.
我在 return 中得到的值与我用作暂定值的值相同 谁能帮我?提前致谢
终于找到解决办法了;我不太确定为什么现在它工作认为我分配了两次相同的变量。无论如何,这是我得出的解决方案:
def f(LL, Sad, n, u_mean):
f = sum((Sad - (4 * n * LL / u_mean) *
((1 + 70.8 * ((n * LL / u_mean) ** (2)))**(-5 / 6)))**2)
return(f)
fun = lambda LL: f(LL, Sad, n, u_mean)
res = scipy.optimize.minimize(fun=fun, x0=LL0)
Lux = res.x
顺便说一句,spyder idle 一直说我不应该使用 lambda 函数,而是定义一个函数。谁能告诉我为什么?我能够将 "fun" 翻译成一个函数;我试过:
def fun(LL):
f(LL, Sad, n, u_mean)
return(f)
res = scipy.optimize.minimize(fun=fun, x0=LL0)
但它不起作用。如果有人能告诉我原因就太好了。
感谢您的帮助