President Reagan signed legislation Friday increasing Medicare benefits and taxes to cover the costs of catastrophic illness, but warned that unless the cost of the program is kept under control it could "harm the very people we are trying to help."

"If administered with prudence, this program can provide countless Americans with peace of mind," the president said at a ceremony in the Rose Garden."I must add a word of caution," he said. "Every administration since the Medicare program was passed has worried about the seemingly uncontrollable cost increases in our government health care programs."

"Whoever the president in office, program costs have exceeded the best congressional budget estimates. Unless we are careful, it is possible that aspects of this legislation will do the same."

The new law will expand Medicare's coverage of hospital and doctor bills and improve access to health services by low-income elderly persons as well as pregnant women and infants. The centerpiece, estimated to cost $31 billion, limits out-of-pocket expenses for doctor and hospital costs incurred by the elderly.

The key provision of the law guarantees unlimited free hospital care for Medicare patients after payment of an initial annual deductible of $564 starting in 1989. Medicare currently provides only the first 60 days free.

The president was joined by Health Secretary Otis Bowen, who was heartily applauded at the signing ceremony that culminated his two-year effort to craft a Medicare expansion measure Congress would approve and the administration would accept.

The compromise bill passed the House 238-72 on June 2, with Speaker Jim Wright calling it "the most important health initiative in recent years." It sailed through the Senate 86-11 on June 9.