{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Un esempio di algoritmo\n", "=======================" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Il problema\n", "Dato $q$, trovare $r >0$, tale che $r\\, = \\, \\sqrt{q}$, con un errore più piccolo di $\\epsilon$.\n", "\n", "### Il procedimento\n", "\n", "Se $r = \\sqrt{q}$ allora $r \\cdot r = q$. \n", "\n", "- Partiamo da un valore stimato $g$. \n", "- Se $g^2 > q$ allora $g > r$. Inoltre $r = q/r > q/g$.\n", "Quindi $g > r > q/g$. (Controllate cosa succede se $g^2 < q$.)\n", "- Se $\\vert g\\cdot g - q \\vert < \\epsilon$ accettiamo $g$ come risposta.\n", "Altrimenti prendiamo come nuova stima per $g$ il punto medio fra $g$ e $q/g$, $g_1 = 1/2 \\cdot (g + q/g)$.\n", "- Ripetiamo finchè la stima finale $g_f$ è tale che $\\vert g_f \\cdot g_f - q \\vert < \\epsilon$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def mysqrt(q,g,epsilon): # I tre argomenti devono essere passati tutti.\n", " while(abs(g*g - q) > epsilon):\n", " g = (g + q/g)/2.\n", " return g\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "mysqrt() missing 1 required positional argument: 'epsilon'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmysqrt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m16.\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: mysqrt() missing 1 required positional argument: 'epsilon'" ] } ], "source": [ "mysqrt(16.,3)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.000000000000241" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mysqrt(16.,3.,1.e-7)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def mysqrt2(q,g,epsilon=1.e-5): # L'argomento epsilon è opzionale. Se non viene passato, assume il valore 1.e-5\n", " while(abs(g*g - q) > epsilon):\n", " g = (g + q/g)/2.\n", " return g\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.000000000052429" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mysqrt2(16.,6.)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.000020480052429" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mysqrt2(16.,6.,1.e-3)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mysqrt2(16.,6.,1.e-15)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Il nostro algoritmo ha:\n", "\n", "- un punto di partenza (fornito dall'utente)\n", "- una sequenza definita di operazioni\n", "- un criterio per interrompere il procedimento" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }