2

I tried this code but i m getting an error. And If possible pls tell me how to lambdify the defined funct CODE

from sympy.abc import x
from sympy import *

init_printing()
def func(y):
    if y>0:
        return 1
    elif y<0:
        return -1
    else:
        return 0
s = fourier_series(func(x), (x, -1, 1))
s = s.truncate(n=4)
s

"TypeError: cannot determine truth value of Relational" this is the error im getting Please help, Thank you.

Athul Dev
  • 61
  • 5

1 Answers1

3

Note that sympy can only work with functions that are fully defined as a sympy expression. To emulate if - elif - else, Piecewise can be used. There is also a special function, Heaviside, that directly corresponds to your given function. Default, Heaviside(0) is undefined. However, the value for 0 can be provided as a second argument.

Using the Heaviside function doesn't seem to work in this case:

from sympy import Heaviside, fourier_series
from sympy.abc import x

s = fourier_series(Heaviside(x, 0), (x, -1, 1))
print(s)

This results in an unevaluated integral with which sympy can't do further operations:

FourierSeries(Heaviside(x, 0), (x, -1, 1), (Integral(Heaviside(x, 0), (x, -1, 1))/2, SeqFormula(cos(_n*pi*x)*Integral(cos(_n*pi*x)*Heaviside(x, 0), (x, -1, 1)), (_n, 1, oo)), SeqFormula(sin(_n*pi*x)*Integral(sin(_n*pi*x)*Heaviside(x, 0), (x, -1, 1)), (_n, 1, oo))))

Fortunately, with Piecewise, everything works as desired:

from sympy import lambdify, Piecewise, fourier_series
from sympy.abc import x

s = fourier_series(Piecewise((1, x > 0), (-1, x < 0), (0, True)), (x, -1, 1))

The function can be lambdified calling lambdify(x, s.truncate(n=4)), and then be used for example to draw curves via matplotlib:

import numpy as np
import matplotlib.pyplot as plt

for k in range(1, 7):
    s_np = lambdify(x, s.truncate(n=k))
    xs = np.linspace(-1, 1, 500)
    plt.plot(xs, s_np(xs), label=f'$n={k}$')
plt.autoscale(enable=True, axis='x', tight=True)
plt.legend()
plt.show()

resulting plot

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Thank you so much , that really helped , s = fourier_series(Piecewise((1, x > 0), (-1, x < 0), (0, True)), (x, -1, 1)). In this line what does (0,True) signifies? – Athul Dev Jun 14 '20 at 03:55
  • 1
    As explained in the [already linked manual](https://docs.sympy.org/latest/modules/functions/elementary.html#piecewise), `Piecewise( (expr1,cond1), (expr2,cond2), …, (exprN,True) )` tests the conditions one by one, and the expression corresponding to the first condition that results True will be the returned. It is similar to `if cond1 return expr1 elif cond2 return expr2 ... else return exprN`. In the code, the last condition is always `True`, so `0` will be returned in case neither `x > 0` nor `x< 0`. – JohanC Jun 14 '20 at 06:04
  • Really sorry, Im new to stackoverflow, Your ans really helped man! – Athul Dev Jun 15 '20 at 14:15
  • s = fourier_series(Piecewise((-2.0*t, -2.0 – qqqqq Nov 13 '20 at 04:20
  • 2
    Sympy gets confused with `-2.0 – JohanC Nov 13 '20 at 06:22
  • @qqqqq Sympy together with matplotlib also get confused lamdifying the series when `k = 1` (giving a constant 2 which when called on an array gives a constant instead of a constant array). You can draw the curves for k starting at 2. – JohanC Nov 13 '20 at 07:23