Python Forum
Parameters aren't seen inside function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parameters aren't seen inside function
#1
Hello,

The question is related to the use of the Lcapy library (circuit analysis).
The 1st version of the Vin declaration (which is commented out) does not work: freq and pi are not visible in the Circuit function.

from lcapy import Circuit
import numpy as np
from matplotlib.pyplot import subplots, savefig
from math import pi
freq = 130e6
cct = Circuit('''
##... Vin 1 0 {1*sin(2*pi*freq*t)}
... Vin 1 0 {sin(130e6*2*3.14159*t)}
... Rin 1 2 50
... Cs  2 4 1e-12
... L   2 3 0.32e-6
... Rs  3 4 5
... Cl  4 0 3e-12''')

t = np.linspace(0, 2e-7, 1000)
vcl = cct.Cl.v.evaluate(t)

fig, ax = subplots(1)
ax.plot(t, vcl, linewidth=2)
ax.set_xlabel('Time (s)')
ax.set_ylabel('Output voltage (V)')
ax.grid(True)

savefig('antenna_output.png')
How to make them visible inside function?

Thanks.

Sancho.
Reply
#2
You define the circuit using symbols. Numbers are symbols. Some trig functions have been defined as symbols. pi is defined as a symbol, so that would work if you stopped using math.pi.

If you want to use a variable I think you need to define it as a symbol or expression. Take a look at the documentation to see if this is what you want.

http://lcapy.elec.canterbury.ac.nz/expressions.html
Reply
#3
Hello,

If I properly understood your suggestion, I have to define parameters as expression.
Doesn't work. In the example below when I build a ircuit, using numeric values, it works.
Once I try to use parameters instead of values, it doesn't.

from lcapy import Circuit, j, omega, expr
import numpy as np
from math import pi

Rin = expr(50)
Cs  = expr(1e-12)
La  = expr(0.32e-6)
Rs  = expr(5)
Rl  = expr(100)

cct = Circuit('''
... Rin 1 2 Rin
... Cs  2 4 Cs
... La  2 3 La
... Rs  3 4 Rs
... Rl  4 0 Rl''')

H = cct.transfer(1,0,4,0)
A = H(j*omega).simplify()
Am = A.magnitude

f1 = 130
f2 = 140
f3 = 150
omega_arr = np.array([f1, f2, f3])*1.0e6*2*pi
out = np.zeros(len(omega_arr))
for i in range(len(omega_arr)):
    out[i] = Am.evaluate(omega_arr[i])
Reply
#4
I don't think that is how expr is used. The link I provided has an expression example under User Defined Symbols. I suggest you try something similar.
Reply
#5
From the link it isn't clear how to use symbols.
For example, if I create symbol for Rin:
Rin = symbol('Rin')
Then, how to associate this symbol with a real value (eg 50.0) so that Rin is perceived as 50.0 in the circuit definition

cct = Circuit('''
... Rin 1 2 Rin
... Cs  2 4 Cs
... La  2 3 La
... Rs  3 4 Rs
... Rl  4 0 Rl''')
Reply
#6
I was thinking something like this:
freq = 60
v = expr(sin(2*pi*freq*t))
cct = Circuit(
... Vin 1 0 v
... Rin 1 2 50
... Cs  2 4 1e-12
... L   2 3 0.32e-6
... Rs  3 4 5
... Cl  4 0 3e-12''')
And if you want to use math.pi, use it as math.pi. I think "from math import pi" hides the "pi" symbol.
Reply
#7
Vin is no longer relevant.
What is interests me: how to pass Rin, Cs, L, Rs, Rl as values, defined outside of Circuit, into Circuit.
Reply
#8
The same way. Using symbols or expressions. I think you failed for the same reason you couldn't use pi. There is a mixup on what are Python variables and what are lcapy symbols.

Do you know much about SymPy? A brief read of the lcapy documentation and an even briefer scan of sympy documentation has me thinking that knowing how to make symbols and expressions in sympy, and what symbols and expressions mean, is key to you solving your problem.
Reply
#9
Finally here is the solution: use subs function, that creates new circuit and substitute the parameters:

from lcapy import network, Circuit, j, omega, symbol
import numpy as np
from math import pi

cct = Circuit('''
... Rin 1 2
... Cs  2 4
... La  2 3
... Rs  3 4
... Rl  4 0''')

cct1 = cct.subs({'Rin': 50, 'Cs':1e-12, 'La': 0.3e-6, 'Rs': 1.0, 'Rl': 100})

H = cct1.transfer(1,0,4,0)
A = H(j*omega).simplify()
Am = A.magnitude

f1 = 130
f2 = 140
f3 = 150
omega_arr = np.array([f1, f2, f3])*1.0e6*2*pi
out = np.zeros(len(omega_arr))
for i in range(len(omega_arr)):
    out[i] = Am.evaluate(omega_arr[i])
Sincerely,

Sancho.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I write a function with three parameters? MehmetAliKarabulut 1 426 Mar-04-2021, 10:47 PM
Last Post: Larz60+
  How to make global list inside function CHANKC 6 553 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  RuntimeError: Optimal parameters not found: Number of calls to function has reached m bntayfur 0 2,106 Aug-05-2020, 04:41 PM
Last Post: bntayfur
  Function parameters and values as string infobound 1 452 Jul-24-2020, 04:28 AM
Last Post: scidam
  Creating function inside classes using type wolfmansbrother 3 706 Mar-20-2020, 01:21 AM
Last Post: wolfmansbrother
  Animate graph nodes inside a function adamG 0 1,105 Sep-23-2019, 11:18 AM
Last Post: adamG
  Conditional importing inside a function Roger 1 2,500 Jul-03-2019, 10:02 PM
Last Post: Gribouillis
  How to pass a dictionary as an argument inside setup function of unittest nilaybnrj 1 1,491 May-11-2019, 03:18 AM
Last Post: keames
  function 2 inside function 1 parameters SheeppOSU 4 1,035 Apr-20-2019, 09:34 PM
Last Post: snippsat
  Parameters for function in bind() kom2 1 756 Apr-18-2019, 03:59 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020