Scipy Optimize Find Constraints of Equation with 14 unknowns

Viewed 34

I'm new at this library but not in Python. I have many equation with 14 unknowns. I have a dataset that contains 640 equation with 14 unknowns and their target value. I'm trying to find optimum 14 parameter for this equations. When I investigated about "how can I do that", I found this library. I runned this example which is on the page's end in order to how it works. Then I tried to implement it suitable to my problem. I tried to first 14 equation of 650 for implementation as start.

def func(x):
    return [
        204 * x[13] + 180 * x[12] + 204 * x[11] + 144 * x[10] + 132 * x[9] + 132*x[8] + 108*x[7] + 144*x[6] + 30*x[5]+ 120*x[4] + 30*x[3] + 168*x[2]+   180*x[1]+   204*x[0]-66,
        72 * x[13] + 78 * x[12] + 72 * x[11] + 66 * x[10] + 60 * x[9] + 72*x[8] + 72*x[7]   +  95*x[6] +     84*x[5]+     95*x[4] + 120*x[3] +  132*x[2]+   132*x[1]+   168*x[0]-45,
        95 * x[13] + 78 * x[12] + 90 * x[11] +84 * x[10] + 66 * x[9] + 60*x[8]  + 108*x[7]  +  84*x[6] +     132*x[5]+    84*x[4] + 95*x[3] +   144*x[2]+   168*x[1]+   132*x[0]-46,
        95 * x[13] + 108 * x[12] + 108 * x[11] +120 * x[10] + 96 * x[9] + 120*x[8] + 108*x[7]   +  132*x[6] +  161*x[5]+ 120*x[4] + 144*x[3] +  168*x[2]+   161*x[1]+   204*x[0]-96,
        144 * x[13] + 90 * x[12] + 96 * x[11] +132 * x[10] + 132 * x[9] + 132*x[8] + 108*x[7]   +  96*x[6] +     108*x[5]+    132*x[4] +    161*x[3] +  180*x[2]+   216*x[1]+   240*x[0]-72,
        120 * x[13] + 30 * x[12] +  30 * x[11]  +66 * x[10] + 66 * x[9] + 84*x[8] + 216*x[7]    +  132*x[6] +  180*x[5]+ 30*x[4] +  66*x[3] +   66*x[2]+    161*x[1]+   240*x[0]-66,
        132 * x[13] + 119 * x[12] + 90 * x[11] +72 * x[10] + 96*x[9] + 96*x[8] + 119*x[7]   +  84*x[6] +     144*x[5]+    144*x[4] +    96*x[3] +   168*x[2]+   252*x[1]+   144*x[0]-24,
        180 * x[13] + 180 * x[12] + 119 * x[11] +96 * x[10] + 108*x[9] + 119*x[8] + 144*x[7]    +  168*x[6] +  66*x[5]+ 180*x[4] +  186*x[3] +  66*x[2]+    180*x[1]+   66*x[0]-61,
        42 * x[13] + 42 * x[12] + 42 * x[11] +36 * x[10] + 36*x[9]  + 36*x[8] + 54*x[7] +  60*x[6] +     48*x[5]+     30*x[4] + 54*x[3] +   54*x[2]+    60*x[1]+    72*x[0]-81,
        180 * x[13] + 180 * x[12] + 66 * x[11]  +132 * x[10] + 144*x[9] + 168*x[8] + 228*x[7]   +  168*x[6] +  216*x[5]+ 144*x[4] + 161*x[3] +  66*x[2]+    216*x[1]+   264*x[0]-114,
        119 * x[13] + 108 * x[12] + 30 * x[11]  +119 * x[10] + 108*x[9] + 108*x[8] + 95*x[7]    +  108*[6] +  95 *x[5]+  108*x[4] + 66*x[3] +   180*x[2]+   216*x[1]+   240*x[0]-90,
        95 * x[13] + 108 * x[12] + 119 * x[11] +119 * x[10] + 95*x[9] + 108*x[8] + 95*x[7]  +  144*x[6] +  228*x[5]+ 119*x[4] + 161*x[3] +  66*x[2]+    180*x[1]+   240*x[0]-46,
        100 * x[13] + 180 * x[12] + 228 * x[11] +180 * x[10] + 168*x[9] + 168*x[8] + 180*x[7]   +  120*x[6] +  240*x[5]+ 144*x[4] + 180*x[3] +  168*x[2]+   30*x[1]+    252*x[0]-115,
        95 * x[13] + 108 * x[12] + 90 * x[11] +120 * x[10] + 120*x[9] + 120*x[8] + 72*x[7]  +  144*x[6] +  168*x[5]+ 120*x[4] + 186*x[3] +  132*x[2]+   120*x[1]+   132*x[0]-95,
        ]

root = fsolve(func, (0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0))

print(root)

It's throws an error like:

 ml, mu, epsfcn, factor, diag)
minpack.error: Result from function call is not a proper array of floats.(216,)

There is there is no shape like 108, or 206 as far as i understand. Why I get this error or how can I find optimum parameters with using this equations dataset? Can anybody help?

1 Answers

You have written 216*[1] instead of 216*x[1], a few times.
This happens in the line of your return statement starting with 119 till the last line of your return statement.

216*[1] creates a list of 216 elements all equal to one. Probably not what you want

edit: This is also probably not the most practical way to solve these equations. Depending on in which form you have your equations, I would create one large matrix of your constraints to get a 640x14 matrix. The target values you can place in a vector of dimensions 640x1. And then you can use numpy.linalg.solve(A,b) to get the unknown parameters of your equation. See numpy.linalg.solve

Related