3

I'm writing a simple python script to solve differential equations using Eulers method and right now i have to change the source code every time i want to solve a new equation.

(Safety isn't of any concern at the moment as this is a personal project.)

My question:
Would it be possible to type in an equation as user input and make it to a variable? For example, using e=input("enter equation") where you enter y/x, and making e a variable for later use?

#I would like to be able to type in a equation as user input and turn it to a variable for later use in the if and elif segments, example

    e=input("Enter your equation here")
    e=float(e)


    h=input("Ange steglängd: ")
    h=float(h)

    x=input("Angivet värde på x: ") 
    x=float(x)

    y=input("Värde på y med anseende på x: ") 
    y=float(y)

    z=input("Närmevärde du vill ha för y(x), ange x: ") 
    z=float(z)


    if x<z:
        while x<z:

            #Type in equation below as e

            e=y/x

            y=y+h*e 
            x+=h    

        print(y)

    elif x>z: 
        while x>z:

            e=y/x

            y=y-h*e
            x-=h

        print(y)
Chrizzy
  • 33
  • 1
  • 1
  • 5
  • 1
    It depends on what you want to do with the equation. You could parse, and turn it into an abstract syntax tree. Or you could parse it into postfix notation. – Christian Dean Jan 18 '17 at 17:42
  • I simply plan to use the equation as a variable which later on is used to solve for an answer, quite new to python and not sure how to further explain my needs. – Chrizzy Jan 18 '17 at 18:36
  • Possible duplicate of [input a symbolic function in a python code](http://stackoverflow.com/questions/15369106/input-a-symbolic-function-in-a-python-code) – user Jan 18 '17 at 18:45
  • Check the [second answer](http://stackoverflow.com/a/15369443/4230591) of the duplicate. However, be careful with `sympify()` (and other SymPy functions) since it uses `eval()` and can execute malicious input. – user Jan 18 '17 at 18:47
  • Went through that code earlier today and failed. Granted I'm only a beginner and I'll give it another try – Chrizzy Jan 18 '17 at 18:53
  • 1
    @Chrizzy Does using `y = y + h * e.subs({'x': x, 'y': y})` in your code, fix your problem? If so, please mark it as a duplicate. – user Jan 18 '17 at 19:17
  • 1
    @Chrizzy To be more precise... use `from sympy import *`, then use `e = sympify(input('give equation:'))` and finally change your code to `y = y + h * e.subs({'x': x, 'y': y})`. (I don't want to post it as an answer since it would be duplicating content on the site, which is not good.) – user Jan 18 '17 at 19:21
  • I agree with Fermi's earlier comment: if this works best for you, please mark your question as a duplicate, and unselect my answer. – Prune Jan 18 '17 at 19:37

2 Answers2

2

Tack för exempel -- that allowed me to determine what you need.

Yes, this is possible. Read in the equation as a string variable, such as

equation = input("Enter your equation here")

Then when you want to find a value for e, use the Python eval method:

e = eval(equation)

Be very careful with this method: eval() is powerful, and very discriminating about what it accepts.

Community
  • 1
  • 1
Prune
  • 76,765
  • 14
  • 60
  • 81
  • 1
    If he'll be using a string provided by a user, he **must not** use `eval()`. There is no way to make it safe. – user Jan 18 '17 at 18:11
  • But then again, even SymPy uses `eval()` currently in some of its methods. So, maybe this answer is not bad, as long as it contains a big warning on why it's dangerous. – user Jan 18 '17 at 18:36
1

You can use the eval in combination with lambda functions, like this:

e = eval('lambda x, y: x/y')

To actually have this interactive, use raw_input() to obtain the actual expression ('lambda x, y: x/y' in this case). Or if you prefer not to type the whole lambda wording, and provided that you'll always have the independent variable x and the dependent variable y, in an equation like y = f(x), you could write something like this:

e = eval('lambda x, y: ' + raw_input('enter equation for y=f(x): '))

And then you'd enter the 'x/y' when you're prompted to.

However, are you sure you need it inside the while loop?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Slobodan Ilic
  • 789
  • 8
  • 8