forked from doingmathwithpython/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlength_curve.py
More file actions
27 lines (22 loc) · 812 Bytes
/
length_curve.py
File metadata and controls
27 lines (22 loc) · 812 Bytes
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
'''
length_curve.py
Find the length of a curve between two points
'''
from sympy import Derivative, Integral, Symbol, sqrt, SympifyError, sympify
def find_length(fx, var, a, b):
deriv = Derivative(fx, var).doit()
length = Integral(sqrt(1+deriv**2), (var, a, b)).doit().evalf()
return length
if __name__ == '__main__':
f = input('Enter a function in one variable: ')
var = input('Enter the variable: ')
l = float(input('Enter the lower limit of the variable: '))
u = float(input('Enter the upper limit of the variable: '))
try:
f = sympify(f)
except SympifyError:
print('Invalid function entered')
else:
var = Symbol(var)
print('Length of {0} between {1} and {2} is: {3} '.
format(f, l, u, find_length(f, var, l, u)))