S Lazy-H
  • Home
  • About
  • Posts
  • Contact
  • Slide Rules
  • A Biker’s Tale

Circuit Simulation

R Programming
electronics
Author

Sam Hutchins

Published

March 9, 2024

As today is a bit of a semi-blustery, cold, snowy day here, I thought I’d address a comment in the fine article I found in my inbox this morning, namely from the Nuts & Volts Magazine about numerical circuit simulation.

The comment was along the lines of, “Which simulator did you use?” The article included at the end the statment:

Aside from not needing a simulator, the beauty of the numerical method described in this article is the insight it gives to circuit operation and the easy adaptability to problems involving more than just electronic components…

The article was about using numerical methods to derive a circuit’s operation, instead of using a simulator, of which there are many, such as QUCS (Quite Universal Circuit Simulator), my favorite.

That gave me the idea that the referenced code blocks could be easily done using (R Core Team 2023), the R programming language for statistics, which is about as numerical as you can get! The code structure is mostly the same as the original article, just adapted to R.

To keep this post short, I selected only the first two examples. The first is about the charge on a capacitor, and the time it takes to get to a particular level. The second describes using a square wave voltage to depict the capacitor’s charge levels.

I also wished to show how to display the results using the built-in plot() function. There are other ways to display the results in R besides plot(), but to keep this simple, I am only using the one.

For storing the results of the first example prior to the plotting, I used a simple vector, defined as stor <- c(1:90). The second example shows both the square wave input voltage and the capacitor charge, so I used a matrix, defined as stor1 <- matrix(c(1:90),nrow=90,ncol=2).

To understand the procedure, you really must read the article. The beginning variables were:

  • R = 5.0 # 5 k Ohms
  • C = 5.0 # 5 uF
  • VC = 0
  • t = -10
  • dt = 0.25

For the second example, the author changed several variables as such to define a square wave input:

  • R <- 1 # 1 k Ohms
  • C <- 10 # 10 uF

He also changed the if() loop, which I adapted to R as so:

  • Vin <- 5 + 4 * sign(sin(2 * pi * 0.2 * t)).

To save space, I wished to display both plots in the same window. That is easily accomplished with par(mfrow=c(2,1)) which displays both plots, one below the other. The code to accomplish all this is here:

Code
system("clear")
rm(list=ls())

stor <- c(1:90)
stor1 <- matrix(c(1:90),nrow=90,ncol=2)

# Example 1
R <- 5.0 # 5 k Ohms
C <- 5.0 # 5 uF
VC <- 0
t <- -10
dt <- 0.25

while(t<90) {
    if(t>=0) { Vin <- 10 # Input 1
    } else{ Vin <- 0 }
    IC <- (Vin - VC) / R
    VC <- VC + IC * dt / C
    stor[t] <- VC
    t <- t + dt
}
par(mfrow=c(2,1))
show(plot(stor,xlab="Time(ms)",ylab="Capacitor Charge (VC)",type="l"))
axis(1, at=c(0,10,20,30,40,50,60,70,80,90))

# Example 2
R <- 1 # 1 k Ohms
C <- 10 # 10 uF
VC <- 0
t <- -10
dt <- 0.25
while(t<90) {
    if(t>=0) { Vin <- 5 + 4 * sign(sin(2 * pi * 0.02 * t)) # Input 1
    } else{ Vin <- 0 }
    stor1[t,1] <- Vin
    IC <- (Vin - VC) / R
    VC <- VC + IC * dt / C
    stor1[t,2] <- VC
    t <- t + dt
}
show(plot(stor1[,2],xlab="Time(ms)",ylab="Capacitor Charge (VC)",type="l",col="red",ylim=c(0,10)))
axis(1, at=c(0,10,20,30,40,50,60,70,80,90))
lines(stor1[,1],col="blue") # plot second line on same plot

The input square wave is blue, the capacitor charge is red. During development, it is always handy to place system(“clear”); rm(list=ls()) at the top of the file. This prevents previous results from interfering with changes made to the source code. I can’t count the times I’ve been frustrated by the same indications even after changes, mostly caused by variables already defined elsewhere. That’s also why in the above code, I’ve repeated some parts.

That’s about it for this post. And again, please read the original article to get the full benefit, and to carry on with your experimentation.

We thank God for His Blessings and all that we have, and His provision for our needs through His Son Jesus! Bye for now.

References

R Core Team. 2023. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
© S Lazy-H 2019 -