Skip to content

Commit 91e1760

Browse files
author
Jonathan Rocher
committed
Added a few examples of scipy solvers
1 parent 7f37790 commit 91e1760

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

scipy/fsolve.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
""" Testing fsolve
2+
"""
3+
from numpy import cos, exp, sin, pi
4+
from scipy.optimize import fsolve
5+
6+
7+
def equations(x,a,b,c):
8+
x0, x1, x2 = x
9+
eqs = \
10+
[3 * x0 - cos(x1*x2) + a,
11+
x0**2 - 81*(x1+0.1)**2 + sin(x2) + b,
12+
exp(-x0*x1) + 20*x2 + c]
13+
return eqs
14+
15+
a = -0.5
16+
b = 1.06
17+
c = (10 * pi - 3.0) / 3
18+
# Optimization start location.
19+
initial_guess = [0.1, 0.1, -0.1]
20+
# Solve the system of non-linear equations.
21+
root = fsolve(equations, initial_guess, args=(a, b, c))
22+
print equations(root, a, b, c)

scipy/linalg_solve.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from numpy import *
2+
from scipy.linalg import inv, solve, lu_factor, lu_solve, det, lstsq
3+
from numpy.linalg import cond
4+
5+
A = array([[ 1. , 2.67499884, 7.15561882 ],
6+
[ 1. , 2.44819414, 5.99365454],
7+
[ 1. , 2.30846879, 5.32902817]], dtype = float64)
8+
9+
print "Near singular matrix if cond is large or det is close to 0", cond(A), det(A)
10+
11+
inverse_matrix = inv(A)
12+
if not allclose(A.dot(inverse_matrix), identity(3)):
13+
raise ValueError("Matrix inversion in viscosity computation failed.")
14+
15+
Y = array([1e15, 1e3, 1.], dtype = float64)
16+
#Y = array([5, 3, 1.], dtype = float64)
17+
18+
# Manual way
19+
X = dot(inverse_matrix,Y)
20+
21+
# More efficient way: the solver doesn't compute the full inverse, since it is not needed.
22+
X = solve(A,Y)
23+
24+
# LU decomposition actually returns just like solve: it probably implements LU behind the scene.
25+
lu, piv = lu_factor(A)
26+
X = lu_solve((lu, piv), Y)
27+
28+
# The least square solver offers a different algorithm to compute this in order
29+
# to minimize |Y-AX|.
30+
X, residues, rank, singular_values = lstsq(A, Y)
31+
32+
if not allclose(dot(A,X), Y):
33+
raise ValueError("The solution from the inversion is incorrect: %s"
34+
% dot(A,X))

scipy/optimize.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
""" Demo scipy curve_fit
2+
"""
3+
from scipy.optimize import curve_fit
4+
from scipy.stats import norm
5+
6+
from numpy import exp, sin, pi, linspace, abs
7+
8+
def function(x, a , b, f, phi):
9+
result = a * exp(-b * sin(f * x + phi))
10+
return result
11+
12+
# Create a noisy data set.
13+
actual_params = [3, 2, 1, pi/4]
14+
x = linspace(0,2*pi,25)
15+
exact = function(x, *actual_params)
16+
noisy = exact + 0.3*norm.rvs(size=len(x))
17+
# Use curve_fit to estimate the function parameters from the noisy data.
18+
initial_guess = [1,1,1,1]
19+
estimated_params, err_est = curve_fit(function, x, noisy, p0=initial_guess)
20+
print "params", estimated_params
21+
#array([3.1705, 1.9501, 1.0206, 0.7034])
22+
# err_est is an estimate of the covariance matrix of the estimates
23+
# (i.e. how good of a fit is it)
24+
print "Estimated covariance diagonal:", err_est.diagonal()
25+
print "Real errors:", abs(actual_params-estimated_params)

0 commit comments

Comments
 (0)