python - Simultaneous Equations with given conditions -


to start off have solved problem it's not big deal, i'm asking satisfy own curiosity. question how solve series of simultaneous equations given set of constraints. equations are:

tau = 62.4*d*0.0007 = (b + 1.5*d)*d p = b + 2*d*sqrt(1 + 1.5**2) r = a/p q = (1.486/0.03)*a*(r**(2.0/3.0))*(0.0007**0.5) 

and conditions are:
tau <= 0.29, q = 10000 +- 3, , minimize b

mentioned able come solution using series of nested loops:

    b = linspace(320, 330, 1000)     d = linspace(0.1, 6.6392, 1000)     ansq = []     ansv = []     anstau = []     i_index = []     j_index = []     in range(len(b)):         j in range(len(d)):             tau = 62.4*d[j]*0.0007             = (b[i] + 1.5*d[j])*d[j]             p = b[i] + 2*d[j]*sqrt(1 + 1.5**2)             r = a/p             q = (1.486/0.03)*a*(r**(2.0/3.0))*(0.0007**0.5)             if q >= 10000 , tau <= 0.29:                 ansq.append(q)                 ansv.append(q/a)                 anstau.append(tau)                 i_index.append(i)                 j_index.append(j) 

this takes while, , there in of head saying there must easier/more elegant solution problem. (linux mint 13, python 2.7.x, scipy 0.11.0)

you seem have 2 degrees of freedom here---you can rewrite in terms of b , d or b , tau or (pick 2 favorites). constraint on tau implies directly constraint on d, , can use constraint on q imply constraint on b.

and doesn't (to me @ least, still haven't finished coffee) code doing other plotting 2 dimensional functions on grid you've defined--not solving system of equations. understand "solving" involve setting equal else, , writing 1 variable function of variable.

it appear you've posted snippet, though, i'll assume else data down stream.


ok, see. think isn't minimization problem, it's plotting problem. first thing i'd see ranges implied b , d constraints on tau, , use derive constraint on d. can mesh points meshgrid (as mentioned below) , run on combinations.

since you're applying constraint before apply mesh (as opposed after, in code), you'll sampling parameter space you're interested in. in code generate bunch of junk you're not interested in, , pick out gems. if apply constraints first, you'll left gems!

i'd define functions like:

p = lambda b, d: b + 2*d*np.sqrt(1 + 1.5**2) 

which works like

>>> import numpy np >>> p = lambda b, d: b + 2*d*np.sqrt(1 + 1.5**2) >>> p(1,2) 8.2111025509279791 

then can write function serve b , d you, can like:

def get_func_vals(b, d):     pvals.append(p(b,d)) 

or, better yet, store b , d tuples in function doesn't return yields:

pvals = [p(b,d) (b,d) in thing_that_yields_b_and_d_tuples] 

i didn't test last line of code, , screw these parenthesis, think it's right.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -