ConquestAce commited on
Commit
d7f261d
1 Parent(s): 927d553

Upload bisection.ipynb

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