Functions

This module contains the algorithms for the nonlinear equations lab.

The materials follow Miranda and Fackler (2004, [MF04]) (Chapter 3). The python code draws on Romero-Aguilar (2020, [RA20]).

labs.nonlinear_equations.nonlinear_algorithms.bisect(f, a, b, tolerance=1.5e-08)[source]

Apply bisect method to root finding problem.

Iterative procedure to find the root of a continuous real-values function f(x) defined on a bounded interval of the real line. Define interval [a, b] that is known to contain or bracket the root of f (i.e. the signs of f(a) and f(b) must differ). The given interval [a, b] is then repeatedly bisected into subintervals of equal length. Each iteration, one of the two subintervals has endpoints of different signs (thus containing the root of f) and is again bisected until the size of the subinterval containing the root reaches a specified convergence tolerance.

Parameters
  • f (callable) – Continuous, real-valued, univariate function f(x).

  • a (int or float) – Lower bound a for x \in [a,b].

  • b (int or float) – Upper bound a for x \in [a,b]. Select a and b so that f(b) has different sign than f(a).

  • tolerance (float) – Convergence tolerance.

Returns

x – Solution to the root finding problem within specified tolerance.

Return type

float

Examples

>>> x = bisect(f=lambda x : x ** 3 - 2, a=1, b=2)[0]
>>> round(x, 4)
1.2599
labs.nonlinear_equations.nonlinear_algorithms.fischer(u, v, sign)[source]

Define Fischer’s function.

\phi_{i}^{\pm}(u, v) = u_{i} + v_{i} \pm \sqrt{u_{i}^{2} + v_{i}^{2}}

Parameters
  • u (float) –

  • v (float) –

  • sign (float or int) – Gives sign of equation. Should be either 1 or -1.

Returns

Return type

callable

labs.nonlinear_equations.nonlinear_algorithms.fixpoint(f, x0, tolerance=0.0001)[source]

Compute fixed point using function iteration.

Parameters
  • f (callable) – Function f(x).

  • x0 (float) – Initial guess for fixed point (starting value for function iteration).

  • tolerance (float) – Convergence tolerance (tolerance < 1).

Returns

x – Solution of function iteration.

Return type

float

Examples

>>> import numpy as np
>>> x = fixpoint(f=lambda x : x**0.5, x0=0.4, tolerance=1e-10)[0]
>>> np.allclose(x, 1)
True
labs.nonlinear_equations.nonlinear_algorithms.funcit(f, x0=2)[source]

Apply function iteration using the fixpoint method.

labs.nonlinear_equations.nonlinear_algorithms.newton_method(f, x0, tolerance=1.5e-08)[source]

Apply Newton’s method to solving nonlinear equation.

Solve equation using successive linearization, which replaces the nonlinear problem by a sequence of linear problems whose solutions converge to the solution of the nonlinear problem.

Parameters
  • f (callable) – (Univariate) function f(x).

  • x0 (float) – Initial guess for the root of f.

  • tolerance (float) – Convergence tolerance.

Returns

xn – Solution of function iteration.

Return type

float