Python & fsolve(): dynamical content of a function f(x) that is called by fsolve()
<p>Let us say I have an equation </p>

<pre><code>x + 2*cos(x) = 0
</code></pre>

<p>and I want to solve it. Then I can program the following:</p>

<pre class="lang-py prettyprint-override"><code>def func1(x):  
  out = x + 2*cos(x)
  return out
Solution = fsolve(func1, StartValue)
</code></pre>

<p>StartValue can have an arbitrary value in this example. So far so good! I am programming a simulation that creates a non linear system of equations which I want to solve with fsolve(). 
The challenge is now that (!) before run time the size of the non linear system of equations is not known (!). This means that I can have for example</p>

<p>x + 2*cos(x) = 0</p>

<p>in the same way as I can have</p>

<pre><code>2*x + 4*y = 0
18*y -18 = 0
</code></pre>

<p>In order to solve the last mentioned system of equations (which will generally always be a non linear one in my program) I have found the following solution:</p>

<pre class="lang-py prettyprint-override"><code>def func2(x):  
  out = [2*x[0] + 4*x[1]]
  out.append(18*x[1]-18)  
  return out  
Solution = fsolve(func2, [1, 1])
</code></pre>

<p>This works quite well, too. But there is a certain reason why I cannot use the solution shown for func2(x): It makes my program very slow! 
The function fsolve() calls the function func2(x) iteratively several times until it has found the solution [-2 1]. But my program will handle a linear system of equations
of some hundred to thousend rows. This would mean that in every iteration step all those thousends rows are appended as it is shown in func2(x). Therefore I am looking for a solution that ONCE creates the system of equations as a function func3(x) and afterwards fsolve() only calls the ready built func3(x). Here is a PSEUDO CODE example:</p>

<pre class="lang-py prettyprint-override"><code>func3 = lambda x: 2*x[0] + 4*x[1]
func3.append(lambda x: 18*x[1] - 18)
Solution = fsolve(func3, [1, 1])
</code></pre>

<p>Unfortunately, functions cannot be appended as I show it above in my PSEUDO CODE. Therefore my question: How can I dynamically build my function func3 and then pass the (
!) ONCE READY BUILT (!) function func3 to fsolve() ???</p>

<p>Thank you very much in advance</p>
