1
\$\begingroup\$

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.

enter image description here

This is the data that was used:

enter image description here


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.

enter image description here

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.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – The Photon Feb 12 '18 at 3:43
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – The Photon Feb 12 '18 at 3:46
  • \$\begingroup\$ @ThePhoton Yes, it changes indeed. - Oh Euler's method, woaw. Never heard of it. \$\endgroup\$ – Harry Svensson Feb 12 '18 at 3:47
4
\$\begingroup\$

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ "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". \$\endgroup\$ – Harry Svensson Feb 12 '18 at 3:49
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – jramsay42 Feb 12 '18 at 3:51
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – The Photon Feb 12 '18 at 3:54
0
\$\begingroup\$

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. enter image description here 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)

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ – Harry Svensson Feb 12 '18 at 4:35
  • 1
    \$\begingroup\$ quantization error with error from not averaging previous I current. you can average previous and present Ic \$\endgroup\$ – Tony Stewart EE75 Feb 12 '18 at 4:36
  • \$\begingroup\$ 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. \$\endgroup\$ – Harry Svensson Feb 12 '18 at 6:37
  • \$\begingroup\$ OK then you shud be using smaller time increments or average current from the previous and present value as I suggested.. "Quantization error" \$\endgroup\$ – Tony Stewart EE75 Feb 12 '18 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.