Are two forms:
1) the easiest - is not trying to use mathematical notation, and translate everything from mathematical notation to the language you are using, in this case Python
(If you think about it, the notation we use for mathematics is a kind of programming language too)
In that case, equacao = "Xn = 3,7(xn-1)";
tries to denote a function that depends on the output of the previous series. These cases, we can solve in structured programming with the use of recursive functions - but it is always necessary a stop code that you did not give. Assuming the criterion is "X1 = 1", the above expression can be written as the Python function:
def X(n):
if n == 1:
return 1
return 3.7 * X(n - 1)
That is, exactly as in the case of mathematical notation, the function uses the result of itself in an earlier iteration. The difference for mathematics is that in mathematics we tend to see "okay, I imagine the previous value is there and it’s so much" - in programming language, the previous value has that being computable - so you can’t even express this function without having an initial value. (Okay, actually it can, but if it were called it would fall into an infinite recursion case and end the program).
2) Use a subsystem that implements objects that behave like mathematical symbols. In the case of Python, there is the project Sympy. It allows you to specify objects in Python with behavior very close to that of mathematical symbols (but, still within Python syntax - for example, multiplication will require the operator "*" ). With Sympy it is possible to create Python objects equivalent to equations like the one you present, and it can make algebraic use of it, and calculate a numerical value by calling specific methods.
Sympy is a big project, done by programming and math professionals over several years - it’s practically another language on top of the Python language.
Hence it is clear that you try to make a system that can itself interpret its mathematical expression that makes use of symbols and make the calculations from it is something quite a lot complex. It may be the project of a life, in fact.
In short: if you have few equations of this type, and you need a way of calculating the numerical results of it using a computer, translate one by one to the Python "notation".
If you are going to work with mathematical formulas, and want to be able to work algebraically with equations of this type, stop to study Sympy - install, do the tutorials and understand it. Only then will you be able to do things with it, because ee combines two complex notation systems - Python and Mathematics in a third.
And - unless it’s a specific project for that, forget the approach of trying to do the interpretation and calculation of symbolic expressions of mathematics for yourself - that would be reinventing the formula 1 car, and you would have to start from the wheel.
Read about recursion.
– Woss
OK, thank you very much!
– Juliano Henrique