1. A method for optimizing an electrical circuit by tuning one or more component sizes and values, the method comprising: formulating an optimization problem involving one or more waveform shaping requirements; determining one or more behaviors of the electrical circuit including one or more actual waveforms of the circuit; generating one or more error signals that are the difference between one of the actual waveforms and a corresponding desired waveform of the circuit, respectively; determining one or more quality metrics from each of the one or more of the error signals, the quality metric being a measure of how well the corresponding actual and desired waveforms match; determining the values of the one or more of the quality metrics and gradients of the one or more of the quality metrics with respect to the one or more of the component sizes and values; and using the values and gradients of the one or more of the quality metrics to iteratively change the component sizes and values so that the respective actual waveforms more closely match the one or more of the respective desired waveforms.