Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

LONDON — Between issuing fiery threats to immolate the United States and being ridiculed in various Hollywood films, North Korea is often caricatured as irrational and ridiculous.

But for all its apocalyptic bluster, the country's pursuit of a nuclear weapons program — including its first intercontinental ballistic missile launch Tuesday — is based on what it believes are a rational set of goals.

The most important of these is self-preservation. The country says it wants a nuclear bomb because it saw what happened when Iraq and Libya surrendered their weapons of mass destruction: their regimes were toppled by Western-backed interventions. It wants to stop others, namely the administration of President Donald Trump, from toppling its totalitarian regime.

"They learned their lesson from Libya and Iraq that the sure-fire way to prevent an attack is to have weapons of mass destruction, rather than just bragging about it," said John Nilsson-Wright, a senior research fellow at Chatham House, a London-based think tank.

In January of last year, North Korea acknowledged that Iraq and Libya were a principal reason for the ramped-up arms program.

"History proves that powerful nuclear deterrence serves as the strongest treasured sword for frustrating outsiders' aggression," according to an editorial by the official KCNA news agency. "The Saddam Hussein regime in Iraq and the [Muammar] Gaddafi regime in Libya could not escape the fate of destruction after being deprived of their foundations for nuclear development and giving up nuclear programs of their own accord."

Meanwhile, the regime knows that Trump's team has not ruled out a military response to the North Korean problem. In a departure from President Barack Obama's policy of strategic patience, Secretary of State Rex Tillerson revealed in March that military action was "on the table."

North Korean leader Kim Jong Un reacts with scientists and technicians of the DPRK Academy of Defence Science after the test-launch of the intercontinental ballistic missile Hwasong-14 in this undated photo released on Wednesday. KCNA via Reuters

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

If the threat of a regional conflict confined to Northeast Asia would not be enough to deter a U.S. attack, then the possibility of North Korea striking the West Coast of the United States might be.

In this sense, developing an intercontinental ballistic missile "is a predictable and rational step for North Korea's military programs," according to Andrea Berger, a senior research associate at the Middlebury Institute of International Studies at Monterey, in California.

"Pyongyang believes that holding continental U.S. targets at risk is essential for deterring the United States from initiating regime change, or from joining a conflict that starts in other ways," she said.

Trump and his team have struck a more combative tone than the Obama White House, with Tillerson saying in March that the past 20 years of diplomatic efforts had "failed."

If, in seeking self-preservation through a nuclear deterrent, North Korea ended up provoking a bullish White House into toppling its regime, its actions might look less wise.

But some experts, such as Nilsson-Wright, believe that "North Korea has called Trump's bluff."

He believes that, after the U.S. weighs the heavy casualties that would result, war is unlikely.

"I would be surprised if Trump is really thinking about military action," he said. "His advisers will be telling him that a conflict would wreak havoc across the peninsula."

North Korea, he added, has "probably handled things in a way that's not so much a miscalculation but a calculated cost-and-benefit analysis."

The successful test-fire of the intercontinental ballistic missile Hwasong-14 at an undisclosed location. AFP - Getty Images

Another reason North Korea might want to build up its nuclear and missile programs is self-preservation from within.

Since its formation following World War II, North Korea has been ruled by the Kim dynasty. Its current ruler, Kim Jong Un, is the grandson of the country's founder, Kim Il Sung.

His primary goal is that "the Kim dynasty must endure," according to Cristina Varriale, a research analyst at the Royal United Services Institute, a London-based think tank.

The state-enforced ideology of the Kim dynasty is called "Juche," a blend of Marxism and ultra-nationalism with a focus on self-reliance of the state.

"One of their main goals is the continuation of their Juche policy," Varriale said.

Kim Jong Un appears to rule the country with an iron fist, clamping down on every aspect of public life and overseeing a regime that is accused of being the world's worst human rights abuser.

But he enjoys less unfettered devotion from the public than his father, Kim Jong Il, or his grandfather, according to Nilsson-Wright at Chatham House.

"So by promising to keep the country safe, and then developing these weapons, he is trying to demonstrate he can deliver on his promises," he said.