Optimization And Energy Management Of Battery Energy Storage Systems In Residential And Industrial Applications