Skip to content

roots #

One Dimensional Root-Finding

The module vsl.roots contains functions for the root finding methods and related declarations.

fn newton #

fn newton(f func.FnFdf, x0 f64, x_eps f64, fx_eps f64, n_max int) !f64

Find the root of a function using Newton's algorithm with the Armijo line search to ensure the absolute value of the function decreases along the iterations.

fn newton_bisection #

fn newton_bisection(f func.FnFdf, x_min f64, x_max f64, tol f64, max_iter int) !f64

Find the root of a function by combining Newton's method with the bisection method

fn Bisection.new #

fn Bisection.new(f func.Fn, params BisectionParams) &Bisection

Bisection.new creates a new Bisection object with the given parameters

fn Brent.new #

fn Brent.new(f func.Fn) &Brent

Brent.new creates a new Brent object

struct Bisection #

struct Bisection {
	f func.Fn @[required]
mut:
	last_iter ?&BisectionIteration // last iteration
pub mut:
	xmin      f64 // lower bound
	xmax      f64 // upper bound
	epsrel    f64 // relative error tolerance
	epsabs    f64 // absolute error tolerance
	n_max     int // maximum number of iterations
	n_f_calls int // number of function calls
	n_iter    int // number of iterations
}

Bisection implements a bisection method for finding the root of a function

fn (Bisection) next #

fn (mut solver Bisection) next() ?&BisectionIteration

next returns the next iteration of the bisection method.

fn (Bisection) solve #

fn (mut solver Bisection) solve() ?&BisectionIteration

solve solves for the root of the function using the bisection method.

struct BisectionIteration #

struct BisectionIteration {
pub mut:
	x         f64
	fx        f64
	n_f_calls int
	n_iter    int
}

struct BisectionParams #

@[params]
struct BisectionParams {
pub:
	xmin   f64
	xmax   f64
	epsrel f64 = 1e-6
	epsabs f64 = 1e-6
	n_max  int = 100
}

BisectionParams contains the parameters for the bisection method

struct Brent #

struct Brent {
pub:
	f func.Fn @[required]
pub mut:
	x1      f64
	x2      f64
	tol     f64
	n_max   int
	n_calls int
	n_iter  int
}

Brent implements Brent's method for finding roots of a function in a given interval. The function must be continuous and the interval must contain a root.

struct BrentIteration #

struct BrentIteration {
	x       f64
	fx      f64
	n_calls int
	n_iter  int
}

BrentIteration is a single iteration of Brent's method