15.12.2012 Views

scipy tutorial - Baustatik-Info-Server

scipy tutorial - Baustatik-Info-Server

scipy tutorial - Baustatik-Info-Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15<br />

10<br />

5<br />

0<br />

5<br />

10<br />

Least-squares fit to noisy data<br />

15<br />

0.00 0.01 0.02 0.03 0.04 0.05 0.06<br />

SciPy Reference Guide, Release 0.8.dev<br />

Fit<br />

Noisy<br />

True<br />

1.5.5 Sequential Least-square fitting with constraints (fmin_slsqp)<br />

This module implements the Sequential Least SQuares Programming optimization algorithm (SLSQP).<br />

min F (x)<br />

subject to Cj(X) = 0, j = 1, ..., MEQ<br />

Cj(x) ≥ 0, j = MEQ + 1, ..., M<br />

XL ≤ x ≤ XU, I = 1, ..., N.<br />

The following script shows examples for how constraints can be specified.<br />

"""<br />

This script tests fmin_slsqp using Example 14.4 from Numerical Methods for<br />

Engineers by Steven Chapra and Raymond Canale. This example maximizes the<br />

function f(x) = 2*x*y + 2*x - x**2 - 2*y**2, which has a maximum at x=2,y=1.<br />

"""<br />

from <strong>scipy</strong>.optimize import fmin_slsqp<br />

from numpy import array, asfarray, finfo,ones, sqrt, zeros<br />

def testfunc(d,*args):<br />

"""<br />

Arguments:<br />

d - A list of two elements, where d[0] represents x and<br />

d[1] represents y in the following equation.<br />

sign - A multiplier for f. Since we want to optimize it, and the <strong>scipy</strong><br />

optimizers can only minimize functions, we need to multiply it by<br />

-1 to achieve the desired solution<br />

Returns:<br />

2*x*y + 2*x - x**2 - 2*y**2<br />

"""<br />

try:<br />

sign = args[0]<br />

1.5. Optimization (optimize) 21

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!