import numpy as np
import scipy.optimize as spo
def function(x,y):
return (np.sin(x*y+y)*np.exp(-1*(x**2+y**2)))**-1
xi=[0,0]
answer=spo.fmin(function,xi)
print 'the answer is', answer
我正在尝试最小化此功能。然而,运行它会带来
TypeError: function() takes exactly 2 arguments (1 given)
答案 0 :(得分:1)
[^'"](~[^/]*)
参数 func 可以调用scipy.optimize.fmin(func, x0, args=(), xtol=0.0001, ftol=0.0001, maxiter=None, maxfun=None, full_output=0, disp=1, retall=0, callback=None)
在这种情况下func(x,*args)
使用一个参数调用fmin
- function
(x
)。第二个参数必须作为xi
参数传递。
args
http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.optimize.fmin.html
答案 1 :(得分:1)
您是否打算最小化2个以上的变量(' x',' y'),或仅使用一个变量(' y'作为额外参数) ?
import UIKit
class ViewController: UIViewController {
@IBOutlet var Answer: UILabel!
@IBOutlet var y2: UITextField!
@IBOutlet var x2: UITextField!
@IBOutlet var PointOne: UITextField!
@IBOutlet var PointTwo: UITextField!
let y1 = 0
let y2point = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func SolveAction(sender: AnyObject) {
solve()
}
func solve()
{
let x2point:Int? = Int(PointOne.text!)
let x1point:Int? = Int(x2.text!)
let AnswerFinal: Int = x1point! + x2point!
//Help pls
Answer.text = AnswerFinal as String!
}
}
带有1个变量的 def fn1(x, y):
# x is minimization variable
# y is extra argument
return (np.sin(x*y+y)*np.exp(-1*(x**2+y**2)))**-1
def fn2(xy):
# xy is minimization variable; assumed to be 2 elements
x,y = xy
return (np.sin(x*y+y)*np.exp(-1*(x**2+y**2)))**-1
;失败
fmin
带有2个元素数组的{p> In [35]: optimize.fmin(fn1, x0=0, args=(0,))
Warning: Maximum number of function evaluations has been exceeded.
Out[35]: array([ 0.])
(fmin
和函数);返回2个元素数组。
x0