1

I am really new to python and numpy as it is the first real programming language I use. Before I started using it I only worked with Mathematica to do my assignments, but since we have to do numerical computations now Mathematica isn't really the right thing to use anymore. I really liked numpy so far because it feels much lighter and far more instinctive than Mathematica. But now I came to a point where I really couldn't go any further with some trying plus information from the internet.

So, my problem is about the Crank-Nicolson method applied to Schrödingers Equation. Leaving out all my definitions of constants and my Matrices, I started with an initial function:

u = np.asarray([
(0 if xx==-L or xx==L else (1/(np.sqrt(np.sqrt(np.pi)*l))))*(np.e**
(1j*k*(xx-y)-(((xx+y)**2)/(2*l**2)))) for xx in x])

which is discretized within a grid x. Furthermore:

q = np.zeros((N-1),dtype=complex)
for i in range(N-1):
    if i==0:
        q[i] = M[i,i+1]/M[i,i]
    elif i==N-2:
        q[i] = 0
    else:
        q[i] = M[i,i+1]/(M[i,i]-M[i,i-1]*q[i-1])

and also:

def dot(x): 
    dot = Mkj.dot(x)
    return dot


qq = np.zeros((N-1),dtype=complex)
for i in range(N-1):
    if i==0:
        qq[i] = dot(u)[i]
    else:
        qq[i] = ((dot(u)[i]-M[i,i-1]*qq[i-1])/(M[i,i]-M[i,i-1]*q[i-1]))

and at last:

v = np.zeros((N-1),dtype=complex)

for i in range(N-1):
    if i==N-2:
        v[i] = qq[i]
    else:
        v[i] = qq[i]-q[i]*v[i+1]

This is now my initial function after one time expansion.

My problem is that i have no idea how i repeat this calculation over various step with looping the outcoming function, because i have no idea how to define a function over looped arrays.

Thanks in advance, Dear Greetings,

Streichholzritter

Farmer Joe
  • 6,020
  • 1
  • 30
  • 40
  • while still not fully understanding your question, have you tried using a list comprehension? e.g. if you want to multiply all elements of a list x = [1,2,3,4,5] you would write x = [ value*2 for value in x ] resulting in x = [2,4,6,8,10] – Erich Jul 13 '17 at 17:01
  • 2
    I'd suggest you try to isolate at maximum what your question is and think on a minimal example. For example, surely how you fill the arrays isn't relevant? – Zah Jul 13 '17 at 17:05
  • 1
    None of your code is actually runnable. Please create an [MCVE](https://stackoverflow.com/help/mcve). – Nils Werner Jul 13 '17 at 18:13
  • It is not clear what a "looped array" is... – AGN Gazer Jul 29 '17 at 06:53
  • For someone "really new to python and numpy as it is the first real programming language I use" you have an awful lot of strong opinions about Python and Mathematica... and yet you have trouble with defining a function... I am confused... – AGN Gazer Jul 29 '17 at 06:58

0 Answers0