组件变体 - 串联 RLC 分支
R2026b以下示例展示了一个串联 RLC 组件,该组件可实现单个电阻、电感器或电容器,或这些元件的串联组合。该组件使用条件部分来实现控制逻辑。
import foundation.electrical.electrical; % electrical domain class definition
import foundation.electrical.elements.*; % electrical elements
component SeriesRLC
nodes
p = electrical; % +:left
n = electrical; % -:right
end
nodes(Access=protected, ExternalAccess=none)
rl = electrical; % internal node between r and l
lc = electrical; % internal node between l and c
end
parameters
R = {0, 'Ohm'};
L = {0, 'H'};
C = {inf, 'F'};
end
if R > 0
components
r = resistor(R=R);
end
connections
connect(p, r.p);
connect(r.n, rl);
end
else
connections
connect(p, rl); % short circuit p--rl
end
end
if L > 0
components
l = inductor(l=L);
end
connections
connect(rl, l.p);
connect(l.n, lc);
end
else
connections
connect(rl, lc); % short circuit rl--lc
end
end
if value(C, 'F') < inf
components
c = capacitor(c=C);
end
connections
connect(lc, c.p);
connect(c.n, n);
end
else
connections
connect(lc, n); % short circuit lc--n
end
end
end参数 R、L 和 C 分别初始化为 0、0 和 inf。如果模块用户指定非零电阻、非零阻抗或有限电容,则会激活相应的分支。激活的分支会声明相应的成员组件并将其串联连接。每个 else 子句都会使相应的节点短路。

内部节点 rl 和 lc 用于连接成员组件,不应从组件外部访问。将其 Access 属性设置为 protected 或 private。它们的 ExternalAccess 属性为 none,因此这些节点不会在模块图标上显示。