Data Analysis Mathematics Linear Algebra Statistics
New Version 8.1!

Supports .NET 6.0. Try it for free with our fully functional 60-day trial version.

QuickStart Samples

# Root Bracketing Solvers QuickStart Sample (IronPython)

Illustrates the use of the root bracketing solvers for solving equations in one variable in IronPython.

```import numerics

from math import *

# The RootBracketingSolver and derived classes reside in the
# Extreme.Mathematics.EquationSolvers namespace.
from Extreme.Mathematics.EquationSolvers import *
# Function delegates reside in the Extreme.Mathematics
# namespace.
from Extreme.Mathematics import *
from Extreme.Mathematics.Algorithms import *

#/ Illustrates the use of the root bracketing solvers
#/ in the Extreme.Mathematics.EquationSolvers namespace of the Extreme
#/ Optimization Mathematics Library for .NET.

# Root bracketing solvers are used to solve
# non-linear equations in one variable.
#
# which is known to contain a root. This interval
# is made smaller and smaller in successive
# iterations until a certain tolerance is reached, # or the maximum number of iterations has been
# exceeded.
#
# The properties and methods that give you control
# over the iteration are shared by all classes
# that implement iterative algorithms.

#
# Target function
#

# The function we are trying to solve must be
# provided as a Func<double, double>. For more
# FunctionDelegates QuickStart sample.
f = cos

# All root bracketing solvers inherit from
# RootBracketingSolver, an abstract base class.

#
# Bisection method
#

# The bisection method halves the interval during
# each iteration. It is implemented by the
# BisectionSolver class.
print "BisectionSolver: cos(x) = 0 over [1,2]"
solver = BisectionSolver()
solver.LowerBound = 1
solver.UpperBound = 2
# The target function is a Func<double, double>.
# See above.
solver.TargetFunction = f
result = solver.Solve()
# The Status property indicates
# the result of running the algorithm.
print "  Result:", solver.Status
# The result is also available through the
# Result property.
print "  Solution:", solver.Result
# You can find out the estimated error of the result
# through the EstimatedError property:
print "  Estimated error:", solver.EstimatedError
print "  # iterations:", solver.IterationsNeeded

#
# Regula Falsi method
#
# The Regula Falsi method optimizes the selection
# of the next interval. Unfortunately, the
# optimization breaks down in some cases.
# Here is an example:
print "RegulaFalsiSolver: cos(x) = 0 over [1,2]"
solver = RegulaFalsiSolver()
solver.LowerBound = 1
solver.UpperBound = 2
solver.MaxIterations = 1000
solver.TargetFunction = f
result = solver.Solve()
print "  Result:", solver.Status
print "  Solution:", solver.Result
print "  Estimated error:", solver.EstimatedError
print "  # iterations:", solver.IterationsNeeded

# However, for sin(x) = 0, everything is fine:
print "RegulaFalsiSolver: sin(x) = 0 over [-0.5,1]"
solver = RegulaFalsiSolver()
solver.LowerBound = -0.5
solver.UpperBound = 1
solver.TargetFunction = sin
result = solver.Solve()
print "  Result:", solver.Status
print "  Solution:", solver.Result
print "  Estimated error:", solver.EstimatedError
print "  # iterations:", solver.IterationsNeeded

#
# Dekker-Brent method
#
# The Dekker-Brent method combines the best of
# both worlds. It is the most robust and, on average, # the fastest method.
print "DekkerBrentSolver: cos(x) = 0 over [1,2]"
solver = DekkerBrentSolver()
solver.LowerBound = 1
solver.UpperBound = 2
solver.TargetFunction = f
result = solver.Solve()
print "  Result:", solver.Status
print "  Solution:", solver.Result
print "  Estimated error:", solver.EstimatedError
print "  # iterations:", solver.IterationsNeeded

#
# Controlling the process
#
print "Same with modified parameters:"
# You can set the maximum # of iterations:
# If the solution cannot be found in time, the
# Status will return a value of
# IterationStatus.IterationLimitExceeded
solver.MaxIterations = 20
# You can specify how convergence is to be tested
# through the ConvergenceCriterion property:
solver.ConvergenceCriterion = ConvergenceCriterion.WithinRelativeTolerance
# And, of course, you can set the absolute or
# relative tolerance.
solver.RelativeTolerance = 1e-6
# In this example, the absolute tolerance will be
# ignored.
solver.AbsoluteTolerance = 1e-24
solver.LowerBound = 157081
solver.UpperBound = 157082
solver.TargetFunction = f
result = solver.Solve()
print "  Result:", solver.Status
print "  Solution:", solver.Result
# The estimated error will be less than 0.157
print "  Estimated error:", solver.EstimatedError
print "  # iterations:", solver.IterationsNeeded```