Conventional control strategy of a shunt active power filter (SAPF) for harmonic and unbalance mitigation is to locally compensate the harmonic and unbalance component of load current, which may not be economical enough for a multinode system with distributed nonlinear loads (NLLs) and unbalanced loads (UBLs). Actually the SAPF will influence voltage at neighboring nodes, therefore it is promising to optimize SAPF control method from a system perspective. For this purpose, first this paper analyzes the drawbacks of a local compensation method and proposes the optimization problem for the system-wide control of SAPF in a simple single-phase system. Next, the idea is extended to a complex multinode three-phase system, and a matrix model for harmonic and unbalance analysis is established. Finally, a matrix-based least square method is utilized to solve the optimization problem, thus a generalized SAPF optimal compensation method for system-wide voltage quality improvement is obtained. Based on the proposed method, a small amount of SAPFs can be scientifically utilized to improve the overall voltage quality of a multinode system with a large amount of distributed NLLs and UBLs, with fast response, good performance, and reduced cost. Simulations and experimental results are provided to verify the effectiveness of the proposed method.