-
Notifications
You must be signed in to change notification settings - Fork 0
/
arclength.py
executable file
·38 lines (27 loc) · 1.21 KB
/
arclength.py
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
"""
Author: Peter Watson
script to calculate arc length
uses a summation instead of finding the derivative of f(x) and using the traditional formula
uses a summation approximation where the length of the curve is estimated by the
length of the hypotenuses of right triangles, where one leg (parallel to x axis) is delta_x
and one leg (parallel to y axis) is (f(x sub i) - f(x sub i - 1))
currently accurate to 7 significant figures -- note: prints out number rounded to 7 decimal places
"""
import math
#define your function here
def f(x):
return (math.log((1 / math.cos(x))))
def arc_length(a, b, f):
n = 10000000 #as n increases, accuracy of estimate increases
delta_x = (b - a) / n
length = 0.0
#summation of square root of (delta_x^2 + (f(x sub i) - f(x sub i - 1))^2)
#equivalent statement: summation of square root of (delta_x^2 + delta_y^2)
for i in range (1, n):
length = length + (((delta_x ** 2) + ((f(a+ (delta_x * i)) - f(a + (delta_x * (i - 1)))) ** 2)) ** (1 / 2))
print (length)
roundedLength = round(length, 7)
print ("length rounded to 7 decimal places:")
print (roundedLength)
#call the function here
(arc_length(0, (math.pi / 4), f))