開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Doing Math with Python: Use Programming to Explore Algebra, Statistics, Calculus, and More! (Amit Saha (著)、No Starch Press)のChapter 7.(Solving Calculus Problems)、Programming Challenges #4: Finding the Length of a Curve(No. 4953)を取り組んでみる。
Programming Challenges #4: Finding the Length of a Curve(No. 4953)
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sympy
def finding_length(f, var, a, b):
derivative = sympy.Derivative(f, var).doit()
length = sympy.Integral(sympy.sqrt(1+derivative**2), (var, a, b))\
.doit().evalf()
return length
if __name__ == '__main__':
f = input('Enter a single variable function: ')
try:
f = sympy.sympify(f)
except sympy.sympifyError as err:
print('Invalid function entered: {0}'.format(err))
else:
var = sympy.Symbol('x')
var = input('Enter the variable: ')
var = sympy.Symbol(var)
print('Enter two points')
a = float(input())
b = float(input())
print('length: {0}'.format(finding_length(f, var, a, b)))
入出力結果(Terminal, IPython)
$ ./sample4.py Enter a single variable function: 2*x**2 + 3 * x + 1 Enter the variable: x Enter two points -5 10 length: 268.372650946022 $
0 コメント:
コメントを投稿