President Reagan and Mexican President-elect Miguel de la Madrid today pledged to cooperate as closely as possible in trying to solve the two nations' economic difficulties, while acknowledging that each must do so in its own way.

In guarded references to Reagan administration concerns about a leftward turn in Mexican rhetoric and emergency economic measures under outgoing President Jose Lopez Portillo, both men emphasized the need to tolerate differing approaches and sometimes disagreements between neighbors.

Declaring that both nations are suffering from "severe economic problems," Reagan said, "each nation must confront the crisis in its own way.

"But as your friend, we stand ready to work with you, recognizing that in times such as these friendships are put to the test," Reagan added in a toast to de la Madrid after three hours of meetings, including lunch.

"Let there be no doubt that our affection will not only withstand these difficulties, but triumph over them," he said. "Indeed, out of economic adversity may spring new incentives to expand the vast economic potential that exists on this proud continent."

"Nations need to solve their own problems," de la Madrid responded. "Good friends do not always think alike, but they place understanding and comprehension above their differences.

"Mexico is interested in seeing the United States a prosperous neighbor in a free and democratic society," he added. "I am aware that the United States has the same interest with respect to Mexico."

But neither disclosed what de la Madrid may do when he takes office Dec. 1 about exchange controls or other drastic economic steps Lopez Portillo recently took to cope with the plummeting value of the peso and Mexico's huge foreign debt.

About $12 billion in American money, including $5 billion belonging to U.S. citizens and corporations, has been frozen in Mexican bank accounts. American banks also are owed about $25 billion of Mexico's $75 billion foreign debt, the largest of any developing nation.

Mexico must find $8.3 billion during the next year to cover an expected gap between income, including that from its growing oil production, and payments due on its loans. It has been negotiating a $3.92 billion three-year loan agreement with the International Monetary Fund and has received, with U.S. help, a $1.85 billion credit package from the Bank for International Settlements.

The Reagan administration also has paid in advance for $1 billion in Mexican oil and provided another $1 billion in cheap credits for purchase of U.S. agricultural products. Reagan did not indicate any further U.S. financial aid to Mexico.

The two leaders also were silent about Central America, in which the Reagan administration hopes to divert Mexico from its previously sympathetic response to Nicaragua and left-wing insurgent movements. It also is considering a Mexican-Venezuelan proposal for negotiations to relieve tensions in Central America.

Later, a senior administration official who participated in the talks said, "We will stay in close touch with Mexico about this."

Reagan's meeting with de la Madrid was sandwiched into the middle of the president's five-day western trip. He flew from here shortly before noon to Tijuana, two miles inside Mexico, to meet the Mexican president-elect.

The motorcade in which the men later rode passed many of the border businesses just inside the United States that have been hit hard by the falling value of the Mexican peso and the imposition of exchange controls. The stores in San Ysidro and Chula Vista had catered primarily to Mexican customers now unable to shop there.

By contrast, Reagan and de la Madrid met here at the Del Coronado, a white-washed Victorian resort on a palm-studded Pacific Ocean beach. Secretary of State George P. Shultz, White House national security affairs adviser William P. Clark and U.S. Ambassador to Mexico John Gavin joined in discussions with aides to de la Madrid.

He was elected in July to a six-year term as Mexico's president. A 47-year-old technocrat, he holds a law degree in Mexico and a degree in public administration from Harvard.