CUBA CELEBRATES the 30th anniversary of its revolution Sunday in a changing world that has put Fidel Castro at odds with his Soviet benefactors and many Cubans who yearn for a better life.

Though credited with gains in education, health care, housing and basic social services, Castro's Cuba is still a poor country. Its economy remains heavily dependent on a single export commodity, sugar, and on some $12 million a day in Soviet aid.The recent thaw in the Cold War has undermined Cuba's strategic importance to the Soviet Union, where liberalization of the economy and the social order have put pressure on a reluctant Castro to modify his orthodox communism, something he vowed never to do.

As a result, diplomats and analysts in the United States and Latin America say Cuba's charismatic dictator is looking outward, seeking better relations with countries in the Western Hemisphere. Notably, he recently agreed to withdraw 50,000 Cuban troops from Angola under a U.S-brokered peace plan.

From his island nation only 90 miles from Florida, Castro has survived CIA assassination plots, hemispheric isolation, a U.S. trade embargo and a U.S.-backed invasion attempt since his rebel forces took power Jan. 1, 1959.

He continued in power after the Cuban missile crisis in 1962, during which he, Nikita Khrushchev and the U.S. brought the world to the brink of nuclear war.

He has walked his own path in defiance of seven U.S. presidents, most recently President Reagan, who still considers Castro "the source" of revolutions in Central America.

"I think we can resist. We have for 30 years," a confident Castro said during a recent trip to Mexico. He denounced the United States as an neo-colonial "empire" that would find it easier to "swallow a steel sea urchin . . . the size of the moon" than to gobble up Cuba.

He has seemed to invite and then spurn a real thaw in his relations with Washington, most recently under Jimmy Carter, who opened the U.S. Interests Section in Havana and was willing to do more. Instead, in the year Carter stood for re-election Castro sent several thousand criminals and mentally ill to the U.S. as part of the boatlift from Mariel.

At 62, Castro still has great popularity at home and in the Third World. He has been highly successful at marshalling nationalistic pride and anti-imperialist sentiments to prolong his steel grip on power.

But he faces growing discontent from a new generation of Cubans who are unenthusiastic about his regime and uncommitted to the Marxist-Leninist principles he embraced after seizing power.

Despite programs that have brought Cubans the lowest official infant mortality rate (16 per 1,000 births) and the highest official life expectancy rate (74 years) in Latin America, Cuba remains very much a closed society with little or no room for dissent.

Under increasing international pressure, notably from Europeans, Castro has tried to show an improving human rights picture in Cuba. He permitted a United Nations human rights team to visit his country last fall.

The report has not been completed, but UN officials are said to have found violations to be declining. Amnesty International, the human rights group, estimates Cuba still holds at least 600 political prisoners, including some jailed for trying to leave the country.

The older generation remembers with devotion the towering guerrilla leader who waged a seemingly impossible battle from the rugged mountain chain of Sierra Maestra to defeat the corrupt, U.S.-backed dictatorship of President Fulgencio Batista.

But many Cubans are too young to remember and take Castro's achievements for granted. Most Cubans have relatives in the United States, and the lure of American society and consumer goods is strong.

Analysts say hundreds of thousands of Cubans would likely leave if given the chance. As proof, they point to the 1980 Mariel boatlift, when 125,000 Cubans deserted the revolution after Castro opened the door just a crack.

Experts say Cuba's relations with the Soviet Union also have become increasingly strained under Mikhail Gorbachev, whose policies of glasnost, or openness, and perestroika, or restructuring, have not been welcomed by Castro.

During a recent 2 1/2-hour news conference in Mexico City, where Castro attended the inauguration of Mexico's new president, Carlos Salinas de Gortari, the Cuban leader denied he feels pressure from the Soviets to move toward market incentives and greater tolerance for political opposition.

Castro said he intends to keep the Cuban revolution ideologically pure, denouncing the introduction of capitalism and consumerism into socialist society as "negative tendencies seriously damaging" to such systems.

He said that even standing alone, Cuba could survive.

Others disagree. Castro's revolution is "clearly an economic disaster," said Susan Kaufman Purcell, director of the Latin America Project at the Council on Foreign Relations in New York City.

Nearly three years ago, before glasnost and perestroika became household words, Castro seemed to recognize he had problems and launched his "campaign for the rectification of errors and negative tendencies."

It was aimed at rooting out profit-motive tendencies in Cuban society, official corruption and deceit in the government and laziness and shoddy workmanship among the labor force, whose guaranteed jobs eliminate incentives.

"Castro has gone in exactly the opposite direction from Gorbachev with this rectification campaign," said Purcell. She argued that the Cuban leader worries Gorbachev's economic programs could fail, weaken socialism globally and "de-legitimize" Castro.

Still, Castro is being pressed toward liberalization by an acute shortage of hard currency and uncertainty over how much aid the Soviets, with problems of their own, will provide in the future.

The Cuban economy has been hurt by the falling price of oil in recent years. The Cubans sell some of the oil they receive from the Soviet Union in return for their sugar. The price drop has meant they receive less foreign currency, which they use to buy modern machinery and other goods from the West.

He has succeeded in restoring diplomatic relations with much of Latin America, broken off when he tried to "export" his revolution during his first years in power. His continued support of revolution in Central America is likely to negate chances for improved relations with a Bush administration, though Castro has said he considers President-elect George Bush more "pragmatic" than President Reagan and hopes relations can improve.