{ "cells": [ { "cell_type": "markdown", "id": "b5785991", "metadata": {}, "source": [ "# exampleMultiRoot\n", "Example of using multiroot finder based on GSL algorithm.\n", "\n", "Find the root of Rosenbrock system of equations:\n", "f1(x,y) = a(1-x)\n", "f2(x,y) = b(y-x^2)\n", "with:\n", "a = 1, b=10\n", "\n", "The MultiRootFinder is based on GSL and it requires the MathMore library\n", "installed.\n", "\n", "Usage:\n", "\n", "```cpp\n", " >.x exampleMultiRoot.C()\n", "```\n", "\n", "or\n", "\n", "```cpp\n", ">.x exampleMultiRoot(algoname,printlevel)\n", "```\n", "\n", "where algoname is for an algorithm not using the derivatives:\n", " hybridS (default) , hybrid, dnewton, broyden\n", "\n", "\n", "\n", "\n", "**Author:** Lorenzo Moneta \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:24 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "c2198dff", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:30.084413Z", "iopub.status.busy": "2026-05-19T20:24:30.084190Z", "iopub.status.idle": "2026-05-19T20:24:30.096951Z", "shell.execute_reply": "2026-05-19T20:24:30.096092Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RConfigure.h\"\n", "\n", "#ifdef R__HAS_MATHMORE\n", "#include \"Math/MultiRootFinder.h\"\n", "#else\n", "#error libMathMore is not available - cannot run this tutorial\n", "#endif\n", "#include \"Math/WrappedMultiTF1.h\"\n", "#include \"TF2.h\"\n", "#include \"TError.h\"\n", "\n", "\n", "using namespace ROOT::Math;" ] }, { "cell_type": "markdown", "id": "7aeb2b9f", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": 2, "id": "e253c76e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:30.098373Z", "iopub.status.busy": "2026-05-19T20:24:30.098248Z", "iopub.status.idle": "2026-05-19T20:24:30.422863Z", "shell.execute_reply": "2026-05-19T20:24:30.422480Z" } }, "outputs": [], "source": [ "const char * algo = nullptr;\n", "int printlevel = 1;" ] }, { "cell_type": "code", "execution_count": 3, "id": "414a1025", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:30.427659Z", "iopub.status.busy": "2026-05-19T20:24:30.427506Z", "iopub.status.idle": "2026-05-19T20:24:30.652717Z", "shell.execute_reply": "2026-05-19T20:24:30.651670Z" } }, "outputs": [], "source": [ "ROOT::Math::MultiRootFinder r(algo);" ] }, { "cell_type": "markdown", "id": "ded3a32a", "metadata": {}, "source": [ "defining the function\n", "use Rosenbrock functions" ] }, { "cell_type": "code", "execution_count": 4, "id": "bf0b1b79", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:30.660349Z", "iopub.status.busy": "2026-05-19T20:24:30.660212Z", "iopub.status.idle": "2026-05-19T20:24:30.875376Z", "shell.execute_reply": "2026-05-19T20:24:30.874547Z" } }, "outputs": [], "source": [ "TF2 * f1 = new TF2(\"f1\",\"[0]*(1-x)+[1]*y\");\n", "TF2 * f2 = new TF2(\"f2\",\"[0]*(y-x*x)\");\n", "f1->SetParameters(1,0);\n", "f2->SetParameter(0,10);" ] }, { "cell_type": "markdown", "id": "1fe90e3c", "metadata": {}, "source": [ "wrap the functions" ] }, { "cell_type": "code", "execution_count": 5, "id": "9ad37d05", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:30.877074Z", "iopub.status.busy": "2026-05-19T20:24:30.876949Z", "iopub.status.idle": "2026-05-19T20:24:31.084592Z", "shell.execute_reply": "2026-05-19T20:24:31.083514Z" } }, "outputs": [], "source": [ "ROOT::Math::WrappedMultiTF1 g1(*f1,2);\n", "ROOT::Math::WrappedMultiTF1 g2(*f2,2);\n", "r.AddFunction(g1);\n", "r.AddFunction(g2);\n", "r.SetPrintLevel(printlevel);" ] }, { "cell_type": "markdown", "id": "a47f551f", "metadata": {}, "source": [ "starting point" ] }, { "cell_type": "code", "execution_count": 6, "id": "810b0def", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:24:31.086195Z", "iopub.status.busy": "2026-05-19T20:24:31.086056Z", "iopub.status.idle": "2026-05-19T20:24:31.294343Z", "shell.execute_reply": "2026-05-19T20:24:31.293476Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "GSLMultiRootFinder::Solve:hybrids max iterations 100 and tolerance 1e-06\n", "GSL Algorithm used is : hybrids\n", "Number of iterations = 19\n", "Root values = x[0] = 1 x[1] = 1 \n", "Function values = f[0] = 0 f[1] = -6.17162e-11 \n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Info in : The iteration converged\n" ] } ], "source": [ "double x0[2]={-1,-1};\n", "r.Solve(x0);" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }