简体   繁体   English

通过使用scipy.optimize.fmin_cobyla得到错误的结果

[英]Get the wrong result by using scipy.optimize.fmin_cobyla

I am very new to scipy and now I struggle to use functions in scipy.optimize by making some little experiment. 我对scipy非常陌生,现在我通过做一些小实验来努力in scipy.optimize使用函数。

I tried to fit a sin-function by finding the parameter, which has lowest error-value. 我试图通过找到误差值最低的参数来拟合sin函数。

The used function is fmin_cobyla 使用的函数是fmin_cobyla

Code is below: 代码如下:

import matplotlib.pyplot as plt
from scipy.optimize import fmin_cobyla
from scipy.optimize import fmin_slsqp
from scipy.optimize import leastsq
import numpy as np
from sympy import *

noise = np.random.randn(100)

def func_model(x, para):
    ''' Model: y = a*sin(2*k*pi*x+theta)'''
    a, k, theta = para
    return a*np.sin(2*k*np.pi*x+theta)

def func_noise(x, para):
    a, k, theta = para
    return a*np.sin(2*k*np.pi*x+theta) + noise

def func_error(para_guess):
    '''error_func'''
    x_seq = np.linspace(-2*np.pi, 0, 100)
    para_fact = [10, 0.34, np.pi/6]
    data = func_noise(x_seq, para_fact)
    error_value  = data - func_model(x_seq, para_guess)
    return error_value

# 1<a<15  0<k<1  0<theta<pi/2
constraints = [lambda x: 15 - x[0], lambda x: x[0]- 1, \
               lambda x: 1 - x[1],  lambda x: x[1], \
               lambda x: np.pi/2 - x[2], lambda x: x[2]]

para_guess_init = np.array([7, 0.2, 0])

solution = fmin_cobyla(func_error, para_guess_init, constraints)
print(solution)   # supposed to be like [10, 0.34, np.pi/6]

xx = np.linspace(-2*np.pi, 0, 100)
plt.plot(xx, func_model(xx, [10, 0.34, np.pi/6]), label="raw")
plt.plot(xx, func_noise(xx, [10, 0.34, np.pi/6]), label="with noise")
plt.plot(xx, func_model(xx, solution), label="fitted")
plt.legend()
plt.show()

after running I got the result 运行后我得到了结果

solution = [1.6655938 0.59868667 0.0731335] 解决方案= [1.6655938 0.59868667 0.0731335]

在此处输入图片说明

This is certainly not the correct answer 这肯定不是正确的答案

Could someone help me. 有人可以帮我吗。 Thanks in advance.. 提前致谢..

There are two things that seem obviously wrong here: firstly, you're changing the noise each time your objective function is called, so your optimization is trying to hit a moving target. 这里似乎有两件事显然是错误的:首先,您每次调用目标函数时都会改变噪声,因此您的优化试图达到一个移动的目标。 Set the simulated data before calling fmin_cobyla : 在调用fmin_cobyla之前设置模拟数据:

the_noise = np.random.randn(100)
data = func_noise(x_seq, para_fact)

Also, your func_error should return the difference between the model and the data for each point, not the sum-of-squares difference: 同样,您的func_error应该返回每个点的模型和数据之间的差,而不是平方和差:

def func_error(para_guess):
    error_value = data - func_model(x_seq, para_guess)
    return error_value

You still may find that fmin_cobyla struggles to find the constrained minimum... some pre-processing to better estimate the initial guess for the phase or frequency might help you here. 您仍然可能会发现fmin_cobyla难以找到受约束的最小值...进行一些预处理以更好地估计相位或频率的初始猜测可能会帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM