rlc.fun 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #import std
  2. #import nat
  3. #import flo
  4. #comment -[Thses functions use a restricted form of RLC circuits to
  5. demonstrate complex arithmetic and recursion. The circuit is
  6. represented as a tree with `s or `p as non-terminal nodes, and pairs
  7. of strings and reals as terminal nodes. The strings begin with R, L or
  8. C, and the real is its resistance, inductance, or capacitance.
  9. impedance takes an argument of the form (w,t) where w is an angular
  10. frequency and t is a tree of type %cseXD representing a circuit. The
  11. result is a tree of type %cjXsjXD, where each node contains the
  12. (complex) impedance of its subtree.
  13. current_division takes a complex input current and a real angular
  14. frequency to a function that takes a circuit tree as above to a list
  15. of assignments of the form <component-name: (current,voltage)..>,
  16. where component-name is a string and current and voltage are complex.]-
  17. #library+
  18. impedance =
  19. %cjXsjXDMk+ %ecseXDXCR ~&arv^?(
  20. ~&ard2falrvPDPMV; ^V\~&v ^/~&d `s?=d(
  21. ~&vdrPS; c..add:-0,
  22. ~&vdrPS; :-0 c..div^/c..mul c..add),
  23. ^:0+ ^/~&ardh case~&ardlh\0! {
  24. `R: c..add/0+0j+ ~&ardr,
  25. `L: c..mul/0+1j+ times+~&alrdr2X,
  26. `C: c..mul/0-1j+ div/1.+ times+~&alrdr2X})
  27. current_division("i","w") =
  28. %jWmMk+ impedance/"w"; ~&/"i"; ~&arv^?(
  29. `s?=ardl/~&falrvPDPML ^ML/~&f ^p\~&arv c..mul^*D/~&al -+
  30. c..vid^*D\~& c..add:-0,
  31. ~&arvdrPS; c..div/*1.+-,
  32. ^ANC/~&ardl ^/~&al c..mul+ ~&alrdr2X)
  33. phaser = # returns magnitude and phase in degrees of a complex number
  34. ^/..cabs times/180.+ div\pi+ ..carg