1

I have a work to do on numerical analysis that consists on implementing algorithms for root-finding problems. Among them are the Newton method which calculates values for a function f(x) and it's first derivative on each iteraction.

For that method I need a way to the user of my application enter a (mathematical) function and save it as a variable and use that information to give values of that function on different points. I know just the very basic of Python programming and maybe this is pretty easy, but how can I do it?

NoGoodAtMath
  • 111
  • 4

1 Answers1

1

If you trust the user, you could input a string, the complete function expression as it would be done in Python, then call eval() on that string. Python itself evaluates the expression. However, the user could use that string to do many things in your program, many of them very nasty such as taking over your computer or deleting files.

If you do not trust the user, you have much more work to do. You could program a "function builder", much like the equation editor in Microsoft Word and similar programs. If you "know just the very basic of Python programming" this is beyond you. You might be able to use a search engine to find one for Python.

One more possibility is to write your own evaluator. That would also be beyond you, and you also might be able to find one you can use.

If you need more detail, show some more work of your own then ask.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50