Solving simultaneous equations in python -
i have following test program. query 2 folded: (1) how solution giving 0 , (2) appropriate use x2= np.where(x > y, 1, x)
kind of conditions on variables ? there constrained optimization routines in scipy ?
a = 13.235 b = 70.678 def system(x, a,b): x=x[0] y=x[1] x2= np.where(x > y, 1, x) f=np.zeros(3) f[0] = 2*x2 - y - f[1] = 3*x2 + 2*y- b return (x) func= lambda x: system(x, a, b) guess=[5,5] sol = optimize.root(func,guess) print(sol)
edit: (2a) here x2= np.where(x > y, 1, x)
condition, 2 equations becomes 1 equation. (2b) in variation requirement is: x2= np.where(x > y, x^2, x^3)
. let me comments on these 2 well. !
first up, system
function identity, since return x
instead of return f
. return should same shape x
had better have
f = np.array([2*x2 - y - a, 3*x2 + 2*y- b])
next function, written has discontinuity x=y, , causing there be problem initial guess of (5,5). setting initial guess (5,6) allows the solution [13.87828571, 14.52157143] found rapidly.
with second example, again using initial guess of [5,5] causes problems of discontinuity, using [5,6] gives solution of [ 2.40313743, 14.52157143].
here code:
import numpy np scipy import optimize def system(x, a=13.235, b=70.678): x = np.where(x[0] > x[1], x[0]**2, x[0]**3) y=x[1] return np.array( [2*x - y - a, 3*x + 2*y - b]) guess = [5,6] sol = optimize.root(system, guess) print(sol)
Comments
Post a Comment