1
I have a little exercise to do: compare the result of the Cosh(x) approximation by Taylor series with the function you have in the Python library.
The hyperbolic cosine Taylor series is:
Cosh(x) = 1 + x 2/2! + x 4/4! + x 6/6! + ... + x 2n/2n!
For now, I’ve made the code below:
import math
valores = raw_input().split(); n = int(valores[0]); x = float(valores[1])
def potencia(x, k):
    if k == 1:
        return x
    else:
        return x * potencia(x, k - 1)
def fatorial(k):
    if k == 0:
        return 1
    else:
        return k * fatorial(k - 1)
def main():
    i = 0
    soma = 0
    while i < n:
        soma = soma + ((potencia(x, 2 * i)) / fatorial(2 * i))
        i = i + 1
    print(n, " : %6.3f = %6.3f" %(soma, math.cosh(x)))
main()
And you keep making that mistake:
Traceback (most recent call last):
  File "coshx.py", line 25, in <module>
    main()
  File "coshx.py", line 21, in main
    soma = soma + ((potencia(x, 2 * i)) / fatorial(2 * i))
  File "coshx.py", line 9, in potencia
    return x * potencia(x, k - 1)
The last two lines of error repeat endlessly. What I’m doing wrong?
Thanks for the help, I realized what I was missing. I learned about recursion a short time ago, so I wanted to test some things.
– Vinicius