I’m writing a function for an implicit scheme for solving a specific differential equation. The function looks like this:
import numpy as np
def scheme(N,T):
y = np.zeros(N+1) # Array for implicit scheme
h = T/N # Step length
for i in range(N):
y[i+1] = y[i] + h*(1+4*y[i])
print y
I save the file and later import it the usual way, but when I run the scheme function, y = [0 ... 0] where ... are N-1 zeros. It seems like the values are lost in the scope of the for-loop.
If I instead write the whole function in the interpreter (which in my case is Spyder), everything works as it should.
Why doesn’t it work when importing the function from the module?
is it possible that
TandNare both integers andT < N? In that caseh = 0(andystays all zeros), because it is an integer division (1/2 == 0).Try to replace this line with
and see the results.
can be rewritten as
which means that for
y[i] = 0, the newy[i+1]will beh. If the integer divisionT/Nmakes it zero, then it is what you get.