Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to solve a PDE equation (like Schrodinger equation) using finite difference in Mathematica. I don't understand what this code does!

ListCorrelate[{1, -2, 1}/Subscript[h, n]^2, 
  U[t], {1, 2}, {Subscript[u, n - 1][t]}]/8

Anyone can help me? What does {1, 2} mean? and what's the role of {Subscript[u, n - 1][t]} ?

share|improve this question

closed as off-topic by bobthechemist, Michael E2, Nasser, rasher, m_goldberg Apr 12 at 21:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – bobthechemist, Michael E2, Nasser, rasher, m_goldberg
If this question can be reworded to fit the rules in the help center, please edit the question.

2  
can you try pasting the code again? It is has syntax errors. !Mathematica graphics –  Nasser Apr 12 at 10:45
    
You can learn about the function ListCorrelate in the Mathematica documentation. However, your question is ambiguous: do you want to know what ListCorrelate does? Do you want to know what Subscript does? Do you want to know how the code you posted relates to solving PDEs? Do you want to know what {1,2} represents? You need to be more precise. –  David Skulsky Apr 12 at 11:43
    
I don't understand what {1,2} represents. I tried to read the Mathematica Documentation –  user13653 Apr 12 at 12:59

1 Answer 1

One of the really nice things about Mathematica is that it lets you do symbolic computation. Perhaps the most important things it does to help you with that is to leave expressions that it doesn't know how to compute unevaluated, instead of throwing an exception or returning some useless value like Java's null or crashing. A lot of times just evaluating an expression will give you a lot of information. This is almost one of those times. Let's take a lot at what this expression returns:

In[10]:= ListCorrelate[{1, -2, 1}/Subscript[h, n]^2, U[t], 
            {1, 2}, {Subscript[u, n - 1][t]}]/8

Out[10]= ListCorrelate[{Subscript[h, n]^(-2), -2/Subscript[h, n]^2, 
            Subscript[h, n]^(-2)}, U[t], {1, 2}, {Subscript[u, -1 + n][t]}]/8

It did a little simplification of the first argument (what ListCorrelate's documentation calls ker, for "kernel"), dividing through by Subscript[h, n]^2, but left everything else alone. It also gave us a message, which is really its way of apologizing for not knowing what to do with the input. Let's look at it. Maybe that will help us figure out what's wrong:

ListCorrelate::kldims: The kernel {1/Subscript[h, n]^2,-(2/Subscript[h, n]^2,1/Subscript[h, n]^2} and list U[t] are not both non-empty lists with the same tensor rank.

OK, ListCorrelate expects the second argument, which it calls list in the docs, to actually be a list. U[t] is just a scalar, so we should replace it with a list, like so:

ClearAll[a, b, c, d, aList];
aList = {a, b, c, d};

Also, let's get rid of the Subscript for now, since it's just noisy in InputForm. Heck, for now, let's just replace Subscript[h, n] with 1, since it will be a common term, and also get rid of the division by 8. In order to keep track of what's coming from where, though, we should replace the elements in the ker argument with something more distinctive:

ClearAll[x, y, z, aKer];
aKer = {x, y, z};

Let's go ahead and replace that last term with e and ditch the division by 8 at the end, because we're pretty sure we know what dividing by 8 means. Let's see what happens:

In[15]:= ListCorrelate[{x, y, z}, {a, b, c, d}, {1, 2}, e]
Out[15]= {a*x + b*y + c*z, b*x + c*y + d*z, c*x + d*y + e*z}

We're taking the Dot-product of length three "slices" of aList with the length three aKer. The first element of the first "slice" is aligned with the first element of aKer, which might be what you'd expect naturally, but the interesting thing is what happens at the other end of the list, where we have

`c*x + d*y + e*z`

Here, the first element of aKer is lined up with the second-to-last element of aList, and the "overhanging" part of aKer is multiplied by e, which we provided as the last argument, which the docs call p (for "padding"; padding lists comes up a lot in Mathematica). It looks like the 1 in {1, 2} tells ListCorrelate to start things by aligning the first element of aKer with the first element of aList, and to stop things when the second element of aKer is aligned to the last element of aList. Let's experiment and see what happens when we use {2, 2} in the place of that last argument:

In[20]:= ListCorrelate[{x, y, z}, {a, b, c, d}, {2, 2}, e]
Out[20]= {e*x + a*y + b*z, a*x + b*y + c*z, b*x + c*y + d*z, c*x + d*y + e*z}

We have a whole new term at the beginning, and one where the second element of aKer is aligned with the first element of aList. Evidently, that third argument is used to tell ListCorrelate where to align the first and last elements of the list argument with the first element of the ker argument.

This should give you a much better idea of what the code in question does, especially if you know a little bit about using finite difference methods to solve partial differential equations like the Schrödinger equation.

EDIT to add: Note that if you just omit the last argument, p, the list will be padded cyclicly, making it perfectly suited to implementing periodic boundary conditions in your finite difference solver.

share|improve this answer
    
Oh! Fantastic! So, can i use this method to set boundary or periodic conditions to my equations, right? –  user13653 Apr 12 at 14:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.