|
Revision 1422, 1.6 kB
(checked in by dmitrey.kroshko, 2 months ago)
|
minor change
|
| Line | |
|---|
| 1 |
""" |
|---|
| 2 |
Let us solve the overdetermined nonlinear equations: |
|---|
| 3 |
a^2 + b^2 = 15 |
|---|
| 4 |
a^4 + b^4 = 100 |
|---|
| 5 |
a = 3.5 |
|---|
| 6 |
|
|---|
| 7 |
Let us concider the problem as |
|---|
| 8 |
x[0]**2 + x[1]**2 - 15 = 0 |
|---|
| 9 |
x[0]**4 + x[1]**4 - 100 = 0 |
|---|
| 10 |
x[0] - 3.5 = 0 |
|---|
| 11 |
|
|---|
| 12 |
Now we will solve the one using solver scipy_leastsq |
|---|
| 13 |
""" |
|---|
| 14 |
from scikits.openopt import LSP |
|---|
| 15 |
from numpy import * |
|---|
| 16 |
|
|---|
| 17 |
f = lambda x: ((x**2).sum() - 15, (x**4).sum() - 100, x[0]-3.5) |
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
def DF(x): |
|---|
| 25 |
r = zeros((3,2)) |
|---|
| 26 |
r[0,0] = 2*x[0] |
|---|
| 27 |
r[0,1] = 2*x[1] |
|---|
| 28 |
r[1,0] = 4*x[0]**3 |
|---|
| 29 |
r[1,1] = 4*x[1]**3 |
|---|
| 30 |
r[2,0] = 1 |
|---|
| 31 |
return r |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
x0 = [1.5, 8] |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
p = LSP(f, x0, df = DF, xtol = 1.5e-8, ftol = 1.5e-8) |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
p.checkdf() |
|---|
| 44 |
|
|---|
| 45 |
r = p.solve('scipy_leastsq', plot=1, iprint = -1) |
|---|
| 46 |
""" |
|---|
| 47 |
or using converter lsp2nlp: |
|---|
| 48 |
r = p.solve('nlp:ipopt',plot=1), r = p.solve('nlp:algencan'), r = p.solve('nlp:ralg'), etc |
|---|
| 49 |
(some NLP solvers require separate installation) |
|---|
| 50 |
""" |
|---|
| 51 |
print 'x_opt:', r.xf |
|---|
| 52 |
print 'funcs Values:', f(r.xf) |
|---|
| 53 |
print 'f_opt:', r.ff, '; sum of squares (should be same value):', (asfarray(f(r.xf)) ** 2).sum().flatten()[0] |
|---|