12-15-2019, 02:18 AM
I'm trying to write a function that generates the general form of the Fourier series coefficients of a periodic waveform that is piece-wise defined.
This is the code I'm using at the moment.
For example, let's say the periodic waveform is defined as:
x, from 0 to 1
1, from from 1 to 2
This is how it I can call the function:
fseries([x,1],x,0,[1,2])
The problem I'm having is when it comes to passing the independent variable. I want to allow for flexibility in choosing any variable letter. At the moment, the code runs but it doesn't seem to recognize the variable letter (see first entry in attached image). Now, if I hard code the variable name in the code (change all var_x to x), it works fine (see second entry in attached image). I've tried different things such as defining the variable as a symbol, substituting, etc, but nothing seems to work. I'm a bit baffled at the moment.
This is the code I'm using at the moment.
PHP Code:
#cas
def fseries(fxn_,var_,ll,uu):
if (type(fxn_) != DOM_LIST):
fxn_ = [fxn_]
uu = [uu]
if (len(fxn_) != len(uu)):
return ("Unequal list sizes")
assume(n, integer)
a_0, a_n, b_n = 0, 0, 0
T = uu[len(uu)-1]-ll
ω = 2*π/T
for i in range(0,len(uu)):
a_0 += 2/T*int(fxn_[i],var_,ll,uu[i])
a_n += 2/T*int(fxn_[i]*cos(n*var_*ω),var_,ll,uu[i])
b_n += 2/T*int(fxn_[i]*sin(n*var_*ω),var_,ll,uu[i])
ll = uu[i]
a_0 = simplify(a_0)
a_n = simplify(a_n)
b_n = simplify(b_n)
return([a_0, a_n, b_n])
#end
For example, let's say the periodic waveform is defined as:
x, from 0 to 1
1, from from 1 to 2
This is how it I can call the function:
fseries([x,1],x,0,[1,2])
The problem I'm having is when it comes to passing the independent variable. I want to allow for flexibility in choosing any variable letter. At the moment, the code runs but it doesn't seem to recognize the variable letter (see first entry in attached image). Now, if I hard code the variable name in the code (change all var_x to x), it works fine (see second entry in attached image). I've tried different things such as defining the variable as a symbol, substituting, etc, but nothing seems to work. I'm a bit baffled at the moment.