It's responsive even for larger n. Then, I try to plot the equation. The plotting function is a large/complicated piecewise function, alternating between the form $f=A \cos(\alpha x)+B\sin(\alpha x)$, and $f=A \cosh(\beta x)+B\sinh(\beta x)$ (where beta is a function of alpha). The matrices "lowPropagator" and "highPropagator" allow me to find the function and its derivative at each point where the form of the piecewise function changes, so I can use a matrix inverse to solve for A and B, and then plug all of this into a giant Piecewise[]. So there's a lot of computation going on in the function. The function is in the form:

For some values, the function takes forever to draw the graph. The black evaluation bar doesn't pop up, so alt+. doesn't do anything. Basically, if I choose the input values incorrectly, most of the time Mathematica becomes completely unresponsive and I have to restart. I think Mathematica IS still running and calculating, because sometimes after 30 seconds or so it will return a result. Out of curiosity I left it running overnight with a different value and it was still frozen when I came back, so, something is not right.

So the biggest issue is that these two functions are fast on their own, at ALL input values, but at certain input values the combination of the two takes forever. My question is, how can I fix this to make the function run at a decent speed, and if I can't, how can I make it fail gracefully so that the notebook doesn't crash?

Relevant things:

It fails at large n and large d or "dist".

It IS taking a matrix with hyperbolic functions in it, to the nth
power. So, the values can get pretty huge (understatement), and this
is exactly what happens with the values at the crash. However, the
root finding function on its own doesn't seem to crash at these
values. Neither does the plotting function. It's only both together
that causes problems.

I figure evaluation might be the problem, maybe the root and/or the
graph are trying to be found many times inside the plot function, but
adding "//Evaluate" to the end of whatever f and αnear equal
doesn't solve the problem.

Let me know if I should post the full code. The relevant bits add up to ~180 lines, with a good amount of spacing/comments. The full code produces graphics like this:

(The red line corresponds to the α result of FindRoot. The purpose of FindRoot is to find all solutions that are exponentially decaying on both sides of the graph, so the red line is exponentially decaying on both sides, but the dashed line [which corresponds to the "start" value in FindRoot] isn't a solution, since it diverges on the right side. I'm trying to find solutions to the time independent schrodinger equation, for a single particle, with n wells, and this is for the end of chapter 2 of "A First Course in Computational Physics" by DeVries [not homework, & the book doesn't ask for solutions this general].)

Update with full code

So, I haven't been able to simplify the code to much significant extent. If I simplify it too much, to the point where it seems to work but isn't calculating what I want it to calculate, the problem seems to disappear! I do think the problem could be with evaluation though. If you comment out the "Show" at the end and just write "f2" in its place, (f2 is the function that is a result of getWaveFunction, with FindRoot passed in as an argument), it prints out, "totalFunction$30552[#1] &" (or some similar number on the end). Since totalFunction doesn't exist in the scope it's at... I'm guessing that this is an evaluation problem, where Plot re-evaluates something it doesn't really have to a bunch of times.

It would be better to post a minimal example showing the problem. On the other hand, your problem is precisely that this happens with these functions, at this number of wells, so I appreciate that you may not be able to do that. Perhaps add the full code and an example showing the problem at the end? (I doubt I will have time to do this this week, but perhaps someone else will--and I'd certainly like to help).
–
aclMar 3 '13 at 21:10

As a side remark, perhaps an important lesson here is that this kind of approach to solving problems using computers doesn't scale beyond simple examples...
–
aclMar 3 '13 at 21:12

@acl Thanks. updated it w/ full code. Could you check my theory at the end of the post? Seems like it might be a red flag but I'm not sure what I can do about it.
–
NeuroFuzzyMar 4 '13 at 0:41

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.