{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Solving Nonlinear Equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To solve a nonlinear equation, we used the package ``NLsolve``." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "using NLsolve" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Our problem is to solve for $r$ in\n", "\n", "$$\n", " 1 - e^{-25 r} = 12.5 \\left( 1 - e^{-r} \\right).\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The above equation is equivalent to\n", "\n", "$$\n", " 1 - e^{-25 r} - 12.5 \\left( 1 - e^{-r} \\right) = 0.\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Writing the above equation as $f(x) = 0$, the function below defines $f$ as the input to ``nlsolve`` of the package ``NLsolve``." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "f!" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"\n", " function f!(F, x)\n", "\n", "defines F[1] as the input to nlsolve.\n", "\"\"\"\n", "function f!(F, x)\n", " r = x[1]\n", " F[1] = 1 - exp(-25*r) - 12.5*(1 - exp(-r)) \n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us pass $r = 0.05$ as the initial value for the solver." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Results of Nonlinear Solver Algorithm\n", " * Algorithm: Trust-region with dogleg and autoscaling\n", " * Starting Point: [0.05]\n", " * Zero: [0.0673745373743581]\n", " * Inf-norm of residuals: 0.000000\n", " * Iterations: 4\n", " * Convergence: true\n", " * |x - x'| < 0.0e+00: false\n", " * |f(x)| < 1.0e-08: true\n", " * Function Calls (f): 5\n", " * Jacobian Calls (df/dx): 5" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sol = nlsolve(f!, [0.05])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see that the solution is 0.0673745373743581." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us verify." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "f (generic function with 1 method)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(r) = 1 - exp(-25*r) - 12.5*(1 - exp(-r))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us see what the output type of ``nlsolve`` really is." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "NLsolve.SolverResults{Float64, Float64, Vector{Float64}, Vector{Float64}}\n", " method: String \"Trust-region with dogleg and autoscaling\"\n", " initial_x: Array{Float64}((1,)) [0.05]\n", " zero: Array{Float64}((1,)) [0.0673745373743581]\n", " residual_norm: Float64 9.36140054363932e-13\n", " iterations: Int64 4\n", " x_converged: Bool false\n", " xtol: Float64 0.0\n", " f_converged: Bool true\n", " ftol: Float64 1.0e-8\n", " trace: NLsolve.SolverTrace\n", " states: Array{NLsolve.SolverState}((0,))\n", " f_calls: Int64 5\n", " g_calls: Int64 5\n" ] } ], "source": [ "dump(sol)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1-element Vector{Float64}:\n", " 0.0673745373743581" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sol.zero" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-9.36140054363932e-13" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(sol.zero[1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The residual is very small, so we accept the solution." ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.12", "language": "julia", "name": "julia-1.12" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.12.4" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false } }, "nbformat": 4, "nbformat_minor": 4 }