Calculus in python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Calculus in python (/thread-2473.html) Pages:
1
2
|
Calculus in python - pythonforumrocks - Mar-19-2017 I don't have a great understanding of calculus (or python) but I need to find the average value of a curve for a project. I've been using a widget from wolframalpha.com. Can I duplicate that in python? Here's the widget: http://www.wolframalpha.com/widgets/view.jsp?id=8256cd442898bb89d60fc9dcbe645198 RE: Calculus in python - ichabod801 - Mar-20-2017 The best I could find with a quick Google for the symbolic solving of integrals was sympy (http://www.scipy-lectures.org/advanced/sympy.html). You could of course do a simulation of the limit (dividing the area under the curve into a bunch of small rectangles, and calculating the total area of the rectangles), but that may not suit your needs. RE: Calculus in python - zivoni - Mar-20-2017 sympy has support for both indefinite and definite integrals: Or you can use numerical integration provided by SciPy.
RE: Calculus in python - pythonforumrocks - Mar-20-2017 I don't see how the range ("from" and "to" on the wolframalpha.com widget) should be specified. Here's my formula: f(x) = ( a * b ) / ( c * 1.05^x ) from = 1 to = b http://www.wolframalpha.com/widgets/view.jsp?id=8256cd442898bb89d60fc9dcbe645198 I will have values for a, b, and c. RE: Calculus in python - zivoni - Mar-20-2017 You can simplify your function as k * 1.05^(-x), where k = a * b / c. ( mathjax support would be handy here) Lets suppose that k = 5 and you want to integrate over interval [1,5]. You can use scipy's quad() - its arguments are function's definition (as a python function) and lower and upper limit: output is the result and an estimation of error.You can use sympy's integrate() , where first argument is a function definition and second is a tuple, with elements being variable you integrate by, lower limit, upper limit. With sympy you can write your function almost "naturally", but you need to specify that x has a special meaning (a symbol). If you are interested only in numerical result, scipy's quad is probably easier to use. And finally, you can use a piece of paper (and a brain) and realize that the primitive function of k * 1.05^(-x) is -k / log(1.05) * 1.05^(-x). After that its just As python is not a domain-specific language for math/symbol manipulation, using python for calculus would require atleast basic understanding of python and basic understanding of a calculus, you likely need to spend some effort and study both a little.
RE: Calculus in python - pythonforumrocks - Mar-20-2017 I'm getting a different output from the wolframalpha.com widget compared to your functions above. With: k, a, b = 5, 1, 5 and: k = a * b / c I get 4.32605 from the widget by using: f(x) = ( a * b ) / ( c * 1.05^x ) from = 1 to = b Could you point out my mistake and/or misunderstanding? RE: Calculus in python - zivoni - Mar-20-2017 Hmm, I was little unclear with notation. After first line i just stopped to care about original values of a, b, c - they were used only to compute k and forgotten (with k = a * b / c function k * 1.05^(-x) is same as (a*b)/c * 1.05^(-x) ) And rest was just integration of function k * 1.05^(-x) on interval [a, b], whatever values of k, a, b are. And for integral of 5 * 1.05^(-x) on interval [1, 5] wolfram alpha gives same result (17.3042) - just fill there 5 * 1.05^(-x), from 1, to 5. RE: Calculus in python - pythonforumrocks - Mar-20-2017 I have to confess I'm a bit lost. What would you say is the simplest python expression of what I've been doing in the Wolfram Alpha widget? RE: Calculus in python - zivoni - Mar-20-2017 I think that something like integrate.quad(lambda x: k * 1.05**(-x), a, b)is probably the simplest way possible - to compute a definite integral you need to have a function to integrate, a lower bound, an upper bound. And you must express that you want to compute the definite integral from these three things. So I am not sure if it even can be significantly simpler ... Anyway, I can try to rewrite what I think you tried to compute - just "verbatim" rewrite without any changes from scipy import integrate a, b, c = 2, 5, 2 # constants def my_func(x): # "defining" your function f_x = a * b / ( c * 1.05**x ) # value of f(x) return f_x print( integrate.quad(my_func, 1, b) ) # you want to integrate function my_func on interval [1, b] and print resultAnd output of it is: I hope that it helps. And if not, perhaps someone else would volunteer ...
RE: Calculus in python - pythonforumrocks - Mar-21-2017 Well, my goal is to get the same output from a python expression that I get from Wolfram Alpha but I'm getting different output. With: a, b, c = 2, 5, 2 I get ~17.30 from this (a one-liner is better for my application): integrate.quad(lambda x: (a*b/c) * 1.05**(-x), 1, b) I get ~4.32 from Wolfram Alpha by using these inputs on the page (with a, b, and c replaced with their values above): f(x) = ( a * b ) / ( c * 1.05^x ) from = 1 to = b http://www.wolframalpha.com/widgets/view.jsp?id=8256cd442898bb89d60fc9dcbe645198 Your function seems to be giving me exactly 4x what Wolfram Alpha is giving me. What am I missing? BTW, I think your code should read the following with 1 as the lower bound instead of a: integrate.quad(lambda x: k * 1.05**(-x), 1, b) |