Euler's method in python
Question: I'm trying to implement [euler's
method](http://en.wikipedia.org/wiki/Euler_method) to approximate the value of
e in python. This is what I have so far:

    
    
    def Euler(f, t0, y0, h, N):
        t = t0 + arange(N+1)*h
        y = zeros(N+1)
        y[0] = y0
        for n in range(N):
            y[n+1] = y[n] + h*f(t[n], y[n])
            f = (1+(1/N))^N
        return y
    

However, when I try to call the function, I get the error "ValueError: shape
<= 0". I suspect this has something to do with how I defined f? I tried
inputting f directly when euler is called, but gave me errors related to
variables not being defined. I also tried defining f as its own function,
which gave me a division by 0 error.

    
    
    def f(N):
        for n in range(N): 
            return (1+(1/n))^n
    

(not sure if N was the appropriate variable to use here...)


Answer: Are you sure you are not trying to implement the Newton's method? Because
Newton's method is used to approximate the roots.

In case you decide to go with Newton's method, here is a slightly changed
version of your code that approximates the square-root of 2. You can change
`f(x)` and `fp(x)` with the function and its derivative you use in your
approximation to the thing you want.

    
    
    import numpy as np
    
    def f(x):
        return x**2 - 2
    
    
    def fp(x):
        return 2*x
    
    def Newton(f, y0, N):
        y = np.zeros(N+1)
        y[0] = y0
        for n in range(N):
            y[n+1] = y[n] - f(y[n])/fp(y[n])
        return y
    
    print Newton(f, 1, 10)
    

gives

`[ 1. 1.5 1.41666667 1.41421569 1.41421356 1.41421356 1.41421356 1.41421356
1.41421356 1.41421356 1.41421356]`

which are the initial value and the first ten iterations to the square-root of
two.

Besides this a big problem was the usage of `^` instead of `**` for powers
which is a legal but a totally different (bitwise) operation in python.

