TypeError:function()只需2个参数(给定1个)(python)

时间:2015-12-05 17:00:26

标签: python python-2.7 numpy scipy

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)

2 个答案:

答案 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 - functionx)。第二个参数必须作为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