File size: 3,627 Bytes
d7f261d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import mercury as mr\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = mr.Text(value=\"Piotr\", label=\"What is your name?\")\n",
    "print(f\"Hello {name.value}\")\n",
    "# set application properites with App object\n",
    "app = mr.App(show_code = True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider one of the most basic problems:\n",
    "\n",
    "Finding the root of a function, i.e: $f(x) = 0$ .\n",
    "\n",
    "## Technique:\n",
    "\n",
    "We start with a boundary $[a,b]$ and then hope that there exists a point $p$ in that boundary where $f(p) = 0$. We half the end points of the boundary depending on if $f(a_i) \\text{ or } f(b_i)$ is negative or positive, until we reach the point $p$\n",
    "\n",
    "## Example\n",
    "Suppose we want to look for the zero for $f(x)= \\sin(x) + 0.5$ between $[-1,2]$. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "26\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "-0.523598775267601"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def f(x): \n",
    "    return np.sin(x)+0.5\n",
    "\n",
    "def validate_interval(f,x0,x1):\n",
    "    return f(x0)*f(x1) < 0\n",
    "\n",
    "\n",
    "def bisection(f, interval, n, tol):\n",
    "    x0, x1 = interval[0], interval[1] #extract interval \n",
    "    if not validate_interval(f, x0, x1): #check interval can be solved for roots\n",
    "        return \"Not valid interval\"\n",
    "\n",
    "    counter = 1\n",
    "    while True:\n",
    "        p = x0 + ((x1-x0)/2)\n",
    "        y = f(p)\n",
    "        if -tol < y < tol:\n",
    "            print(counter)\n",
    "            return p\n",
    "        if validate_interval(f,x0,p):\n",
    "            x1 = p\n",
    "        else:\n",
    "            x0 = p\n",
    "        counter += 1\n",
    "\n",
    "\n",
    "\n",
    "bisection(f,[-1,2], 50, 0.000000001) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def graph(x):\n",
    "    #need function\n",
    "    #need tangents\n",
    "    #root\n",
    "    fig, ax = plt.subplots(figsize=(10, 10))\n",
    "    y=f(x)\n",
    "    plt.plot(x,y)\n",
    "   \n",
    "    plt.ylabel('some numbers')\n",
    "    plt.axis('tight')\n",
    "    plt.grid(True)\n",
    "    plt.show()\n",
    "    ax.spines['top'].set_visible(False)\n",
    "    ax.spines['right'].set_visible(False)\n",
    "\n",
    "graph(np.arange(-np.pi,np.pi,np.pi/32))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.10.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}