Skip to content

Commit 70a0381

Browse files
committed
Create the_Simpson’s_method.ipynb
Create a simple solution for the Simpson's numerical method
1 parent 4707303 commit 70a0381

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"Simpson's Rule is a numerical method that approximates the value of a definite integral by using quadratic functions. This method is named after the English mathematician Thomas Simpson (1710−1761)."
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"Let's check this method for the next function: $$f(x) = ({e^x / 2})*(cos(x)-sin(x))$$ with $\\varepsilon = 0.001$"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"import math \n",
24+
"import numpy as np\n",
25+
"\n",
26+
"def simpsone(a, b, n, func):\n",
27+
" h = float((b-a)/n)\n",
28+
" s = (func(a) + func(b)) * 0.5\n",
29+
" for i in np.arange(0, n-1):\n",
30+
" xk = a + h*i\n",
31+
" xk1 = a + h*(i-1)\n",
32+
" s = s + func(xk) + 2*func((xk1+xk)/2)\n",
33+
" x = a + h*n\n",
34+
" x1 = a + h*(n-1)\n",
35+
" s += 2 *func((x1 + x)/2)\n",
36+
" return s*h/3.0"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"metadata": {},
42+
"source": [
43+
"## Some input data"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 2,
49+
"metadata": {},
50+
"outputs": [
51+
{
52+
"name": "stdout",
53+
"output_type": "stream",
54+
"text": [
55+
"Result: -8.404153016168566\n"
56+
]
57+
}
58+
],
59+
"source": [
60+
"f = lambda x: (math.e**x / 2)*(math.cos(x)-math.sin(x))\n",
61+
"\n",
62+
"n = 10000 \n",
63+
"a = 2.0\n",
64+
"b = 3.0\n",
65+
"\n",
66+
"print(\"Result: \", simpsone(a, b, n, f))"
67+
]
68+
}
69+
],
70+
"metadata": {
71+
"kernelspec": {
72+
"display_name": "Python 3",
73+
"language": "python",
74+
"name": "python3"
75+
},
76+
"language_info": {
77+
"codemirror_mode": {
78+
"name": "ipython",
79+
"version": 3
80+
},
81+
"file_extension": ".py",
82+
"mimetype": "text/x-python",
83+
"name": "python",
84+
"nbconvert_exporter": "python",
85+
"pygments_lexer": "ipython3",
86+
"version": "3.7.6"
87+
}
88+
},
89+
"nbformat": 4,
90+
"nbformat_minor": 4
91+
}

0 commit comments

Comments
 (0)