我是Python编程的新手,我正在努力优化一个简单的非线性函数。代码如下(我的计算机上有anaconda,包括numpy和scipy库):
#!/usr/bin/python
# -*- coding: Latin-1 -*-
# System libraries
import os
import sys
import math
import getopt
import scipy
from scipy import optimize
#params
a = 78.0
b = 47.0
c = 0.0203242205945909
#initial guess (solution is not far to c so we use c parameter as initial guess)
guess = c
#objective function to find root
def obj(_x, _a, _b, _c):
f = (_c - (_x + (_b*x**_b)*(10**_a)))
return f;
x = guess
sol = scipy.optimize.fsolve(lambda x: obj(x, _a=a, _b=b, _c=c), guess)
#aaaand when we calculate the solution, it does not correspond to a root of the function!
print(sol)
print(obj(x, a, b, c))
使用脚本时的结果是:
[-1.38815731]
-1.40848153538
我希望:
[correct root that I don't know]
0.00000000000
为什么会给出结果?我做错了什么?如何使fsolve冗长?我在文档中没有这样做。