0

I want to write some Lua code to calculate the total impedance of an electrical circuit. Here is my code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{luacode} % for 'luacode' environment

\begin{luacode}
function calculate_impedance(R, L, C, f)

    local omega = 2 * math.pi * f
    local Z_R = R
    local Z_L = 1i * omega * L
    local Z_C = -1i / (omega * C)
    local Z_total = Z_R + Z_L + Z_C
    
    return Z_total
end
\end{luacode}

\begin{document}
 
\section{Introduction}
This document calculates the impedance of an electrical circuit with resistive, inductive, and capacitive components.

\section{Parameters}
\begin{itemize}
    \item Resistance $ R = 100 \, \Omega $
    \item Inductance $ L = 0.1 \, H $
    \item Capacitance $ C = 10 \, \mu F $
    \item Frequency $ f = 50 \, Hz $
\end{itemize}

\section{Calculation}
\begin{luacode}
R = 100  -- Resistance in ohms
L = 0.1  -- Inductance in henries
C = 10e-6  -- Capacitance in farads
f = 50  -- Frequency in hertz

Z = calculate_impedance(R, L, C, f)
\end{luacode}

The total impedance $ Z $ of the circuit is:

\begin{equation}
Z = \luacode{tex.print(string.format("%.2f + %.2fi", Z:real(), Z:imag()))}
\end{equation}

\section{Conclusion}
The impedance of the circuit is calculated successfully. 

\end{document}

Compiler gives the following error:

I suspect you have forgotten a `}', causing me to read past where you wanted me to stop.

What is the wrong with my code?

7
  • 4
    1i as in local Z_L = 1i * omega * L is invalid Lua code. Variables cannot start with numbers. Commented Oct 14, 2024 at 20:18
  • 2
    i is the imaginary unit. I don't know how to manage complex numbers with lua, but writing 1i is not the good way for that. Commented Oct 14, 2024 at 20:38
  • 2
    I was too slow to implement a small adhoc Lua module for complex numbers... Good you found a solution! Commented Oct 14, 2024 at 22:43
  • 2
    I’m voting to close this question because the OP's issue was solved in a comment. Commented Oct 15, 2024 at 6:24
  • 2
    If you found a fix, could you please post an answer to your own question? Commented Oct 15, 2024 at 7:00

2 Answers 2

4

I have found a solution for calculating the impedance of a RLC circuit using the luacomplex package. For example, I have a solution for series RLC circuit.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{amsmath} % for 'align*' environment
\usepackage{siunitx} % for '\qty' and '\unit' macros
\usepackage{luacomplex}
\usepackage[nomessages]{fp}
\usepackage{circuitikz}
\ctikzset{bipoles/thickness=1.2}
\usepackage{enumitem}
    
\begin{document}

    %\renewcommand{\imgUnit}{\mathrm{j}}
    \def\R{10}
    \def\L{0.1}
    \def\C{0.0001}
    \def\f{50}
    \FPeval{\omega}{2*pi*\f}%
    \cpxNew{R}{\R,0}
    \cpxNew{X_L}{0,\omega*\L}
    \FPeval{\xc}{1/(\omega*\C)}%
    \cpxNew{X_C}{0,-\xc}
    \cpxOp{Z}{R+X_L+X_C}

\section{Introduction}

This document calculates the impedance of series and parallel R-L-C circuits with resistive, inductive, and capacitive components.

\section{Parameters}

\begin{itemize}[noitemsep]
    \item Resistance  $R = \qty{\R}{\ohm}$
    \item Inductance  $L = \qty{\L}{\henry}$
    \item Capacitance $C = \qty{\C}{\farad}$
    \item Frequency   $f = \qty{\f}{\hertz}$
\end{itemize}

\subsection{Series R-L-C Circuit}

    \begin{figure}[h!]
    \centering
    \begin{circuitikz}
    \draw[line width=1]
      (1,4) to [sinusoidal voltage source, l_=$V_S$, i=$I$] (1,1)
      (1,4) to [resistor, l_=$R$] ++(6,0) to [inductor, l_=$L$] ++(0,-4) to [capacitor, l_=$C$] +(-6,0) to [short](1,0) to [short](1,1);
    %\draw[help lines] (0,0) grid (10,10)   ;
    \end{circuitikz}
    \end{figure}

In a series RLC circuit, the total impedance $Z$ can be calculated using the formula:
\[Z=R+j(X_L-X_C)\] 
Calculate Inductive Reactance ($X_L$):
\[X_L=2\pi f L\]  
Calculate Capacitive Reactance ($X_C$):
\[X_C=\frac{1}{2\pi f C}\]
For the values ​​given above, we find:
\begin{align*}
R   &= \qty{\R}{\ohm} \\ 
X_L &= (\cpxPrint{X_L})\,\unit{\ohm} \\
X_C &= (\cpxPrint{X_C})\,\unit{\ohm} \\
Z   &= R+X_L+X_C=(\cpxPrint{Z})\,\unit{\ohm}\,.
\end{align*}
    
\subsection{Parallel R-L-C Circuit}

In a parallel RLC circuit, the total impedance $Z$ can be calculated using the formula:
\[
\frac{1}{Z}=\frac{1}{R}+\frac{1}{jX_L}+\frac{1}{jX_C}\,.
\]

\end{document}

enter image description here enter image description here

3
  • 2
    Off topic: Why is \[ ... \] preferable to $$ ... $$?. Commented Oct 15, 2024 at 14:08
  • 1
    +1. I've taken the liberty of making your code more "idiomatic LaTeX". Feel free to revert. Commented Oct 15, 2024 at 15:52
  • 1
    @Teepeemm - I've taken the liberty of making the OP's code more "idiomatic LaTeX", which includes replacing all instances of $$...$$ with \[...\]. :-) Commented Oct 15, 2024 at 15:53
3

The main issue raised in your query -- the nature of the errors in the Lua code -- has already been solved in the comments section below the query.

As a separate matter, I'd also like to encourage you to be more systematic and purposeful in how you go about typesetting scientific units and their associated quantities. The siunitx package provides the \qty macro for just this purpose. The \qty macro can be used in both text mode and math mode.

Observe how the units are typeset in upright font, the use of \micro (which generates a text-mode rather than a math-mode version of the Greek lowercase letter "micro") instead of \mu, and the automated insertion of a thinspace between the quantity und units groups. This outcome is not just nice to have, but is what's expected, typographically speaking.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{luacode}
\usepackage{siunitx} % for '\qty' macro

\begin{document}

\begin{itemize}
    \item Resistance  $R = \qty{100}{\ohm}$
    \item Inductance  $L = \qty{0.1}{\henry}$
    \item Capacitance $C = \qty{10}{\micro\farad}$
    \item Frequency   $f = \qty{50}{\hertz}$
\end{itemize}

\end{document}
1
  • Thank you for suggestion @Mico. Commented Oct 15, 2024 at 13:43

You must log in to answer this question.