Version 8.1 | ||
---|---|---|

Supports .NET 7.0 and earlier. Try it for free with our fully functional 30-day trial version. |

QuickStart Samples

# Newton-Raphson Equation Solver QuickStart Sample (IronPython)

Illustrates the use of the NewtonRaphsonSolver class for solving equations in one variable and related functions for numerical differentiation in IronPython.

C# code Visual Basic code F# code Back to QuickStart Samples

import numerics from math import sin, cos # The NewtonRaphsonSolver class resides in the # Extreme.Mathematics.EquationSolvers namespace. from Extreme.Mathematics.EquationSolvers import * # Function delegates reside in the Extreme.Mathematics # namespace. from Extreme.Mathematics import * # The FunctionMath class resides in the # Extreme.Mathematics.Calculus namespace. from Extreme.Mathematics.Calculus import * from Extreme.Mathematics.Algorithms import * #/ Illustrates the use of the Newton-Raphson equation solver #/ in the Extreme.Mathematics.EquationSolvers namespace of the Extreme #/ Optimization Mathematics Library for .NET. # The Newton-Raphson solver is used to solve # non-linear equations in one variable. # # The algorithm starts with one starting value, # and uses the target function and its derivative # to iteratively find a closer approximation to # the root of the target function. # # 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 # information about this delegate, see the # FunctionDelegates QuickStart sample. f = sin # The Newton-Raphson method also requires knowledge # of the derivative: df = cos # Now let's create the NewtonRaphsonSolver object. solver = NewtonRaphsonSolver() # Set the target function and its derivative: solver.TargetFunction = f solver.DerivativeOfTargetFunction = df # Set the initial guess: solver.InitialGuess = 4 # These values can also be passed in a constructor: solver2 = NewtonRaphsonSolver(f, df, 4) print "Newton-Raphson Solver: sin(x) = 0" print " Initial guess: 4" 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 # When you want just the zero without any additional information, # and you don't need to set any parameters (see below), then # you can call the FindZero extension method on the delegate # that defines your function: result = FunctionMath.FindZero(f, df, 4) print " Solution:", result # # When you don't have the derivative... # # You can still use this class if you don't have # the derivative of the target function. In this # case, use the static CreateDelegate method of the # FunctionMath class (Extreme.Mathematics.Calculus # namespace) to create a Func<double, double> # that represents the numerical derivative of the # target function: solver.TargetFunction = Special.BesselJ0 solver.DerivativeOfTargetFunction = FunctionMath.GetNumericalDifferentiator(Special.BesselJ0) solver.InitialGuess = 5 print "Zero of Bessel function near x=5:" 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 = 10 # 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-14 # In this example, the absolute tolerance will be # ignored. solver.AbsoluteTolerance = 1e-4 solver.InitialGuess = 5 result = solver.Solve() print " Result:", solver.Status print " Solution:", solver.Result # The estimated error will be less than 5e-14 print " Estimated error:", solver.EstimatedError print " # iterations:", solver.IterationsNeeded

Copyright Â© 2003-2023, Extreme Optimization. All rights reserved.

*Extreme Optimization,* *Complexity made simple*, *M#*, and *M Sharp* are trademarks of ExoAnalytics Inc.

*Microsoft*, *Visual C#, Visual Basic, Visual Studio*, *Visual Studio.NET*, and the *Optimized for Visual Studio* logo

are registered trademarks of Microsoft Corporation.