Scientific Python in GX-TXT · Module 5 · Lesson 29

Looking for a zero means finding a value for which a function is worth zero. Minimizing means finding a value that makes a goal as small as possible. In this lesson we address both problems with Python callbacks and known analytical solutions.

Objectives

  • Distinguish root, residual and minimal.
  • Choose a range that encloses a root.
  • Read convergence and accuracy carefully.
  • Distinguish a local minimum from a global minimum guarantee.

Two simple problems

For \(f(x)=x^2-2\), the positive root is \(\sqrt{2}\). In the range [1, 2], \(f(1)=-1\) and \(f(2)=2\): opposite signs, along with function continuity, guarantee at least one zero in the range. This choice of the range is called bracketing. A different range may contain another root, or none.

For \(g(x)=(x-3)^2+1\), the square is always non-negative: the global minimum is 1, reached at \(x=3\). This warranty comes from the formula, not simply from the termination of an algorithm. For more complicated functions a numerical method can find a minimum local without showing that it is the best on the whole domain.

Runner performance

In the corso_python Session, import root_finding_and_optimization.py, save the script in the Python Script Workspace and run it from Run → Open Script Runner → Run Job.

Root-finding and minimization script in the GX-TXT editor with the Rich Dark theme.
Root-finding and minimization script in the GX-TXT editor with the Rich Dark theme.
import numpy as np
from gxtxt import gxtxt_api_call, gxtxt_api_require, gxtxt_callback_register, gxtxt_callback_release

gxtxt_api_require("NUMERIC_API_v1@1:math.numeric+script_callback_functions")
def function(x):
    return x * x - 2.0
def objective(x):
    return (x - 3.0) ** 2 + 1.0
root_cb = gxtxt_callback_register(function)
min_cb = gxtxt_callback_register(objective)
try:
    root = gxtxt_api_call("NUMERIC_API_v1", "root_find", [root_cb, [1.0, 2.0]])
    minimum = gxtxt_api_call("NUMERIC_API_v1", "minimize_1d", [min_cb, [0.0, 5.0]])
finally:
    gxtxt_callback_release(min_cb)
    gxtxt_callback_release(root_cb)
print("Converging Root:", root["converged"])
print("Radice:", root["root"], "residuo:", root["fval"])
print("Difference from sqrt(2):", abs(root["root"] - np.sqrt(2)))
print("Converging minimum:", minimum["converged"])
print("Location and value:", minimum["x"], minimum["fval"])

The two functions return a climb and are recorded separately. root_find receives callback and root range; minimize_1d receives callback and minimal search range. The finally lock releases both handles after calls. Do not make Bridge calls from within callbacks.

Job output showing the root, residual and minimum.
Job output showing the root, residual and minimum.

The root obtained is about 1,41421356237. The fval residue is about 1,2 × 10⁻¹³ and the difference between np.sqrt(2) is about 4,1 × 10⁻¹⁴ in this execution. The minimum found is in \(x=3\), with value 1. Both answers report converged: True.

Tolerance and interpretation

Tolerance regulates the numerical stopping criterion, but it is not automatically a universal guarantee on scientific error. A small value of f(x) is a useful control; the link between residual and error on the position also depends on the slope of the function near zero. For the minimum consider scale, domain and meaning of the goal.

The completion of the Job indicates that the script is finished; check separately converged, result, residual and hypotheses. In real applications it is also useful to save solver options, range and information. If a function is not continuous or does not present the necessary signs to the extremes, reasoning on bracketing does not apply in the same way.

Exercise

  1. Hand check the signs of f(1) and f(2) and locate the other root of f.
  2. Calculate g(2) and compare it to g(3).
  3. A research on [0, 5] always shows that a minimum found is global on the whole straight?

Worked solution

  1. \(f(1)=-1\), \(f(2)=2\); the other root is \(-\sqrt{2}\), outside the chosen range.
  2. \(g(2)=2\), while \(g(3)=1\).
  3. No. The algorithm searches for the range indicated; a global conclusion requires additional function and domain properties. In our example the shape of the square provides such property.

In the next lesson we will integrate numerically a differential equation and compare the solution with a known formula.

Download the Module 5 scripts (ZIP)

#GX-TXT #numerical methods #Python

Other steps of this project

Leave a Reply

Your email address will not be published. Required fields are marked *