Python: How can I use input to receive a function to integrate from the user in scipy? -
it program calculates continuous uniform distribution. example enter x^2, = 0, b = 1. program evaluate x^2 0 1. coefficient 1/(b-a) part of rules of continuous uniform distribution.
formula = input( "what formula? use x variables.") = input("what value of a?") b = input("what value of b?") formula = formula.replace("^", "**") coefficient = 1 / (float(b) - float(a)) coefficient = float(coefficient) ans,err = quad(formula, a, b) print(ans * coefficient)
it telling me can't convert formula string float. problem formula string has variable 'x' in it. if float(formula) gives me error can't convert string float. there way around this?
if understand correctly, user able input formula using variables a
, b
. example be: formula = 'a*b'
, a = 3
, b = 5
, yield 15
.
perhaps use python exec(string) method execute code given in formula.
wiki
Comments
Post a Comment