Why it raises error when print the return values of function for non-linear equations
<p>I use fsolve to solve equations, but when I put the solution into the KMV() function again, it raises an exception. I can not figure it out... </p>

<pre><code>def KMV(x, *args):
    valueToEquity = float(x[0])
    volOfValue = float(x[1])
    equityToDebt, volOfEquity, riskFreeRate, TimeToMaturity = args
    d1 = (np.log(equityToDebt * equityToDebt) + (riskFreeRate + 0.5 *    volOfEquity**0.5)
      * TimeToMaturity) / (volOfEquity * TimeToMaturity**0.5)
    d2 = (np.log(abs(equityToDebt * equityToDebt)) + (riskFreeRate - 0.5 * volOfEquity**0.5)
      * TimeToMaturity) / (volOfEquity * TimeToMaturity**0.5)
    f1 = valueToEquity * norm.cdf(d1) - np.exp(-riskFreeRate * TimeToMaturity) * norm.cdf(d2) / equityToDebt - 1
    f2 = norm.cdf(d1) * valueToEquity * volOfValue - volOfEquity
    return f1, f2



def solver():
    equityToDebt = 1
    volOfEquity = 0.2
    riskFreeRate = 0.03
    TimeToMaturity = 1
    args = equityToDebt, volOfEquity, riskFreeRate, TimeToMaturity
    x0 = [1, 0.2]
    sol = fsolve(KMV, x0, args=args)
    print(sol)
</code></pre>

<p>The solutions I get is </p>

<pre><code>[ 1.29409904  0.17217742]
</code></pre>

<p>However if I use the following code:</p>

<pre><code>print(KMV(sol, args=args))
</code></pre>

<p>The exception is shown as below:</p>

<pre><code>    print(KMV(sol, args = args))
TypeError: KMV() got an unexpected keyword argument 'args'
</code></pre>

<p>Then I changed into another way to call KMV():</p>

<pre><code>print(KMV(sol, args))
</code></pre>

<p>The exception is another one:</p>

<pre><code>ValueError: need more than 1 value to unpack
</code></pre>

<p>If you remove the leading <code>*</code> from <code>args</code> in the declaration of <code>KVM</code> you can call it like that, but other code would fail if you did that.  <code>*args</code> is for <em>variadic</em> functions, that is functions with a variable number of parameters.  </p>

<p>If you want to pass keywords as well then pass a dictionary, often called <code>kwargs</code>.  Note that the left-right order of these is critical.  Here is a simple example:</p>

<pre><code>def KMV(x, *args, **kwargs):
   if 'args' in kwargs:
      args = kwargs['args'],
   return args

args = 1,2,3,4
sol = 42
print(KMV(sol, args))
print(KMV(sol, args = args))
</code></pre>

python scipy