# Iterative simulation of RC low pass filter, strange correction required

To get further understanding in how iterative simulators work, like circuitJS for an example, I decided to make my own of a simple RC low pass filter.

The voltage I am interested in is the one across the capacitor, it can either be calculated with the following formula:

$V_C=V_{E}(1-e^{-\frac{t}{RC}})$

Or it can be approximated by this algorithm... that I scrambled together:

VC = 0;
for(int i=0;i<N;++i){
VC += time_step*(VE-VC)/(R*C);
}


time_step would be the time in seconds between the updates of the simulator. With this algorithm, the t is the product of time_step and N. It is a simple first order digital LP filter.

So I implemented that algorithm in google sheets to see how good it was. And it was fairly correct.

## This is the data that was used:

As you can see, it's not a perfect fit.

If I however modify the algorithm a little bit to this:

VC = 0;
for(int i=0;i<N;++i){
VC += alpha*time_step*(VE-VC)/(R*C);
}


and set alpha to 0.824, which comes from the ratio between the correct value and the iterative value at t = 10 µs. Then I get a perfect match with 0 error.

I calculated alpha = 0.824 from $\frac{RC}{\text{time_base}}(1-e^{\frac{-\text{time_base}}{RC}})$ with R = 25, C = 1, time_base = 10

Why do I need to add that correction multiplication "alpha" when I am iterating?

Extra information: As time_base goes down to 0, alpha goes to 1. In other words, as I step through finer and finer steps in time, the less the error becomes.

• This is more a computational science question than EE, but short answer: what you did is called Euler's method and its errors are very well known. – The Photon Feb 12 '18 at 3:43
• You just got lucky that an exponential (1st order linear o.d.e.) is so regular that the result could be corrected by a single multiplying factor. But try changing the time step, for example, and I bet the required alpha will change. – The Photon Feb 12 '18 at 3:46
• @ThePhoton Yes, it changes indeed. - Oh Euler's method, woaw. Never heard of it. – Harry Svensson Feb 12 '18 at 3:47

You're basically numerically integrating the differential equation

$\frac{dV_c}{dt} = \frac{V_e-V_c}{RC}$

using a first order approximation to the derivative i.e. setting dt to time_step, this has limited accuracy. It becomes exact, as you noted, by reducing the time step to zero. Alternatively, you could use a higher order approximation, if you research some of those you'll find where that scaling factor might come from.

• "if you research some of those you'll find where that scaling factor might come from.", the only buzzword I got so far is "Euler's method" which I got from The Photon, I presume that if I go down Euler's rabbit hole, then I'll find "where that scaling factor might come from". – Harry Svensson Feb 12 '18 at 3:49
• Sorry, I was being deliberately vague because researching it will help further your understanding in how iterative simulators work. The wikipedia pages on numerical integration and numerical differentiation are probably good places to start. – jramsay42 Feb 12 '18 at 3:51
• Or get your hands on a basic numerical methods textbook. Numerical Recipes in [your favorite programming language] is very popular, but it does have its critics. – The Photon Feb 12 '18 at 3:54

## The key is quantization error.

If you choose dt=1% of expect T the resulting V error at T is 0.07%

You can use a simple spreadsheet with correct results using constants for dt,R,C,Vcc and compute Ic, Vc from ;

Ic=(Vcc-Vc)/R
dV=Ic*dt/C

Vc(t)=dV(t)+Vc(t-1)

Of course I could have just as easily used the correct exponential. I could have averaged the current with the previous value, but I just stepped it and computed dV and added to the previous value so there is some latency error but small enuf for Gov't work. ( no alpha)

• Your time step is 1 µs => alpha = 0.98, at row 24 you're at the 20 µs point which gave you a value of 2.79 for VC. In reality it is 2.753, $\frac{2.753}{2.79}=0.986$. 0.986 > 0.98, so yeah, your method is indeed helpful. Not bad. – Harry Svensson Feb 12 '18 at 4:35
• quantization error with error from not averaging previous I current. you can average previous and present Ic – Tony Stewart EE75 Feb 12 '18 at 4:36
• Wait a second, your method is identical to mine. The only reason for why 0.986>0.98 is because I measured yours at the 20th iteration, rather than the first. - In the algorithm I used, the "Ic=(Vcc-Vc)/R" and "dV=Icdt/C " were mashed together. You can see that under the "algorithm" part in my question. And then I did the "*Vc(t)=dV(t)+Vc(t-1)" as well. - When I force the alpha to 1 I get identical values to yours. - Just thought I'd share this with you. - No harsh feelings. – Harry Svensson Feb 12 '18 at 6:37
• OK then you shud be using smaller time increments or average current from the previous and present value as I suggested.. "Quantization error" – Tony Stewart EE75 Feb 12 '18 at 6:39