Skip to content

Commit 4ab2263

Browse files
committed
Changed string formatting method
1 parent 3e60489 commit 4ab2263

2 files changed

Lines changed: 67 additions & 5 deletions

File tree

maths/line_length.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ def line_length(fnc: Callable[[Union[int, float]], Union[int, float]],
1717
1818
>>> def f(x):
1919
... return x
20-
>>> '%.6f' % line_length(f, 0, 1, 10)
20+
>>> f"{line_length(f, 0, 1, 10):.6f}"
2121
'1.414214'
2222
2323
>>> def f(x):
2424
... return 1
25-
>>> '%.6f' % line_length(f, -5.5, 4.5, 100)
25+
>>> f"{line_length(f, -5.5, 4.5):.6f}"
2626
'10.000000'
2727
2828
>>> def f(x):
2929
... return m.sin(5 * x) + m.cos(10 * x) + x * x/10
30-
>>> '%.6f' % line_length(f, 0.0, 10.0, 10000)
30+
>>> f"{line_length(f, 0.0, 10.0, 10000):.6f}"
3131
'69.534930'
3232
"""
3333

@@ -57,6 +57,5 @@ def f(x):
5757
print("The length of the curve from x = -10 to x = 10 is:")
5858
i = 10
5959
while i <= 100000:
60-
length = line_length(f, -10, 10, i)
61-
print("With {} steps: {}".format(i, length))
60+
print(f"With {i} steps: {line_length(f, -10, 10, i)}")
6261
i *= 10

maths/numerical_integration.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Approximates the area under the curve using the trapezoidal rule
3+
"""
4+
5+
from typing import Callable, Union
6+
7+
def trapezoidal_area(fnc: Callable[[Union[int, float]], Union[int, float]],
8+
x_start: Union[int, float],
9+
x_end: Union[int, float],
10+
steps: int = 100) -> float:
11+
12+
"""
13+
Treats curve as a collection of linear lines and sums the area of the
14+
trapezium shape they form
15+
:param fnc: a function which defines a curve
16+
:param x_start: left end point to indicate the start of line segment
17+
:param x_end: right end point to indicate end of line segment
18+
:param steps: an accuracy gauge; more steps increases the accuracy
19+
:return: a float representing the length of the curve
20+
21+
>>> def f(x):
22+
... return 5
23+
>>> '%.3f' % trapezoidal_area(f, 12.0, 14.0, 1000)
24+
'10.000'
25+
26+
>>> def f(x):
27+
... return 9*x**2
28+
>>> '%.4f' % trapezoidal_area(f, -4.0, 0, 10000)
29+
'192.0000'
30+
31+
>>> '%.4f' % trapezoidal_area(f, -4.0, 4.0, 10000)
32+
'384.0000'
33+
"""
34+
x1 = x_start
35+
fx1 = fnc(x_start)
36+
area = 0.0
37+
38+
for i in range(steps):
39+
40+
# Approximates small segments of curve as linear and solve
41+
# for trapezoidal area
42+
x2 = (x_end - x_start)/steps + x1
43+
fx2 = fnc(x2)
44+
area += abs(fx2 + fx1) * (x2 - x1)/2
45+
46+
# Increment step
47+
x1 = x2
48+
fx1 = fx2
49+
return area
50+
51+
52+
if __name__ == "__main__":
53+
54+
def f(x):
55+
return x**3
56+
57+
print("f(x) = x^3")
58+
print("The area between the curve, x = -10, x = 10 and the x axis is:")
59+
i = 10
60+
while i <= 100000:
61+
area = trapezoidal_area(f, -5, 5, i)
62+
print("with {} steps: {}".format(i, area))
63+
i*=10

0 commit comments

Comments
 (0)