Is it ever rational to commit suicide?

Yes: when the ends that matter to one are better served by suicide than by staying alive. Jan Palach killed himself to make a powerful point against the Soviet invasion of his country -- plausibly believing that nothing else he could have done would have had as great an effect (see question 1518). Victims of the Gestapo have killed (or tried to kill) themselves in order to avoid betraying their comrades. Admiral Chester Nimitz and his wife Joan killed themselves in old age, seeking to end their lives on their own terms rather than incapacitated in some medical facility. Each of these people had an end to which they gave more weight than to their own survival -- the end of ending Soviet domination, the end of defeating the Nazis, the end of dying on one's own terms. There is nothing irrational in ranking these ends above an additional period of life for oneself.

I would add this, however. While it certainly can be rational to commit suicide, people who are considering suicide aren't always in a good position to think about it rationally. That's for the obvious reason that many (perhaps most) people who are seriously thinking about killing themselves are depressed, and part of what depression does is make it hard to think clearly. A depressed person might believe that there's no hope, and that the pain will never end, but that's often not true. So yes: suicide can be rational. But if you know someone who's thinking about it, helping them get help may serve what they would understand as their own rational ends if only they were in a better position to see them.

Read another response by Thomas Pogge, Allen Stairs
Read another response about Suicide