# Why do MATLAB and LTspice give me different results for a band-pass filter?

Here is the circuit diagram. R = 1 kΩ; C = 10 µF; L = 5 mH.

$$H(j \omega) = \frac{R} {\frac{1}{j \omega C} + j \omega L + R}$$ Amplitude Phase

The above is what I get from MATLAB, using the code below:

syms C w f
R = 1000;
% Capacitor Impedance
C = 10^(-5);
ZC = 1/(i*w*C);
% Inductor Impedance
L = 5 * 10^(-3);
ZL = i*w*L;
% Frequency Response
H(w) = R/(R + ZC + ZL)

y(f) = subs(eval(H),w,2*pi*f)
degrees(f) = atand(imag(y(f))/real(y(f))) % Phase Response
f = logspace(-3,6,10000);
% first plot figure
f1 = figure('Name','Amplitude Response','NumberTitle','off');
semilogx(f, abs(y(f)), '.b') % Amplitude response
vpa(abs(y(15.26)), 8)
grid on
% second plot figure
f2 = figure('Name','Phase Response','NumberTitle','off');
semilogx(f,degrees(f), '.r')
vpa(degrees(15.26), 8)
grid on


Now, this is what I get from LTspice:  When freq = 15.26 Hz, LTspice says the amplitude is 692.246 mV. But MATLAB tells me it is 692.252 mV. Although it's close, it's different.

• LTSpice and Matlab go about solving the problems differently. LTSpice does not know what an LPF is; it uses numeric approximation to solve for the system voltages and currents. That means it'll find a solution that is within tolerance, moreover, the graph doesn't have a datapoint at every frequency, it interpolates between datapoints. Matlab, on the other hand, was told exactly what equation to solve and can return an 'exact value'. Oct 6, 2022 at 12:16
• Don't use automatically generated node numbers; explicitly label nodes with a sensible name like "output". Oct 6, 2022 at 12:47
• Try using a decade sweep (instead of linear) if you're trying to make a Bode plot. Also, right-click on the inductor and set the ESR to zero. It's 1milliohm by default. Oct 6, 2022 at 13:12
• I don't think I'd be worrying about a 0.0008% difference in a filter simulation. That said, I don't get anything like the waveforms you're showing for that schematic. Are you sure those are the values you used to get that plot? Oct 6, 2022 at 13:50
• It has nothing to do with numerical analysis. Think about Andy's hint. If it's too difficult, here's another one: what's the difference between lin and log in the .AC card? Also, 1000G points is not only ridiculous but, LTspice will limit that, internally, to 65536 (or 65535). Oct 6, 2022 at 14:52 And, if I used my simulator of choice (micro-cap) and told it to use 1 million points when calculating the bode plot, I get this: - As you can see, at 15.26 Hz, the voltage is exactly what Matlab says it is.

You're seeing a few types of numerical issues: quantization of the frequency sample points, interpolation between those points, as well as rounding in the plot display.

I built your circuit in the CircuitLab circuit simulator and set it to run with 10000 points per decade. You can click it and run the plot for yourself: simulate this circuit – Schematic created using CircuitLab

But if you really care about the Nth digit of precision, you can click "Export Plot CSV" and get a table printed at full resolution. Here's a small excerpt:

Frequency,MAG(V(out))
...
15.2510589584334,0.692040481479363
15.2545710488632,0.692123586753699
15.2580839480749,0.692206683691613   <=== These are the two nearest
15.2615976562546,0.692289772287345   <=== datapoints to 15.26 Hz
15.2651121735887,0.692372852535137
15.2686275002635,0.692455924429232
15.2721436364654,0.692538987963877
15.2756605823807,0.692622043133319
15.2791783381961,0.692705089931808
...


If for some reason you really needed to know the response at precisely 15.26 Hz, you could make 15.26 Hz be the start point of your frequency domain simulation, and you would find:

Frequency,MAG(V(out))
15.26,0.692251995944294


This agrees with the 692.252 mV you quoted earlier, and in fact gives you quite a few more digits to chase :)

• ...or simply use .ac list 15.26. Oct 7, 2022 at 7:34

The error is less than 9 PPM.

The simulator simulates the circuit without knowing what circuit it is. The component models may contain some non-idealities to simulate real-world components, depending on which models you used.

On the other hand, you are explicitly writing the equation to Matlab.

The error you are getting in LTspice is caused by selecting a linear sweep. The total number of points is limited to 65536, which normally wouldn't be a problem because 15.26 Hz is less than 1 pixel away from 0 Hz when not zoomed in. But you have selected a log frequency display, which stretches out the plot at low frequencies and makes the extrapolation between points visible.

The first point after 0 Hz is at ~15.259 Hz, and the next is at ~30.518 Hz. The tiny voltage error at 15.26 Hz is caused by LTspice's method of extrapolating between these points.

To increase resolution at low frequencies you should either lower the stop frequency, or select octave or decade sweep (which is more appropriate for displaying a log frequency plot).

You should also bear in mind that floating point accuracy is not infinite. Depending on how the analysis is done, noticeable errors can occur. We don't usually worry about this because most real components have quite loose tolerances, and real-world circuits are full of parasitic elements that can't be simulated exactly.