Generally, you will need to define your own cost/objective function based on your specific application, as well as specifying the all constraints related to the capacitor and distribution system.
In the Global Optimization Toolbox of MATLAB, there is also a built-in PSO solver for derivative-free unconstrained optimization or optimization problem with bounds. See