LTspice Tips - Mathematical Integration

How to Plot an Integral in the LTspice WaveForm Viewer

This is something of a workaround solution since as far as I could determine there is no integration function directly available in the LTspice WaveForm Viewer. There is however the idt() function and equivalent sdt() function available for things like arbitrary sources (default library components “bi” and “bv”) in schematics, and for the cost of a little extra manipulation and schematic clutter this can accomplish the task. Note that a transient ( .tran ) simulation is necessary since the integration variable is time in all cases. Taking one of the examples from the previous article and expanding on it, integration can be used to determine the total energy (Joules) stored in a capacitor at any given time in the simulation. An explanation of how to manually enter/edit functions in the WaveForm Viewer can be found in the next article.

Download LTspice File - Capacitor_Energy_Via_Integration.asc (718 Bytes)

Since V(Vout) is the voltage across the capacitor C1 , then the instantaneous power of C1 is *V(Vout)I(C1 ) as shown in light blue of the top plot. The time integral of the instantaneous power gives the total energy, which is provided directly by the voltage output of arbitrary voltage source B2 at the node named NRG via the equation *V={idt(V(Vout)I(C1))} . A unit correction kludge, 1J/1V , is applied to V(NRG) in the plot to convert the final scale from the volts of the B2 source to the actually desired unit of Joules.

A “Quick Window Integral” Method in LTspice WaveForm Viewer

There is a way to ask the LTspice WaveForm Viewer to provide a quick definite integral calculation of certain types of signals (instantaneous power being one of them). This can actually be quite useful if the definite integral is desired to be performed over a time period bounded by something other than zero as the lower limit. Simply set the x-axis of the plot in question to the desired time period, and then Ctrl+left_click on the desired signal’s label in WaveForm Viewer.

Capacitor_Energy_0ms_to_10ms
Capacitor_Energy_7ms_to_10ms

For the full time period of 0 to 10 ms the reported integral value is just over 5 uJ, the same as given by the plot of the integral at 10ms. For the reduced (and no longer starting at zero) time period of 7 to 10 ms, the reported integral value is now about 2.55 uJ, indicating that more than half of the total energy accumulated at 10 ms is provided by the last three pulses at the progressively higher voltage.

A Note of Caution

It’s important to understand that the integration being done is a definite integral with the lower limit of integration starting at time zero and the upper limit being the current time in the simulation. For example, when looking at the value of the integral waveform at 5 ms it’s the value of the definite integral bounded from 0 to 5 ms even if the simulation runs much farther in time past 5 ms, at 6 ms it’s the value of the definite integral bounded from 0 to 6 ms, etc. This can potentially lead to some confusion, especially for infinite repeating signals, as the integral waveform is really just a running sum of the area under the curve up to that point in time in the simulation.

Here, take the case of integrating a cos() signal vs. integrating a sin() signal of the same frequency and amplitude. If the integral in either case is adjusted for amplitude shift due to the 2pif constant, the integral of the cos() function should be an equal amplitude and frequency sin() function, and the integral of the sin() function should be an equal amplitude and frequency -cos() function. This is true in both cases; however, the latter case has a positive DC shift equal to the amplitude of the integrated sin() function.

Download LTspice File 1 - Integral.asc (552 Bytes)

Download LTspice File 2 - Integral_2.asc (552 Bytes)

So, what’s happening in the second case to explain this? Exactly what’s expected mathematically, the integral’s waveform is providing a running sum of the area under the curve up to that point in time in the simulation in both cases. Observe visually that the cos() signal on the left started at its peak value of 1V at time zero and decreased from there, providing exactly half its positive waveform and half its negative waveform in the first 0.5 ms, whereas the sin() signal on the right started at 0V at time zero and immediately presented the entire positive half of its signal in the first 0.5 ms. Put another way, because of the way the sin() signal on the right is bounded, its definite integral with a set lower limit of time zero always yields zero or a positive value.

Wolfram or any other math package that can calculate definite integrals can confirm these results are correct as well.

wolfram_sin

wolfram_cos

Comments

Any questions or comments please go to our TechForum: TechForum