import numpy as np
import sympy as sym
import matplotlib.pyplot as plt
from IPython.display import display,Math
print('Examples')
display(Math('\\quad f(x) = \\begin{cases} 0,& \\text{if} \\: x \\leq 0 \\\ -2x,& \\text{if} \\: x>0 \\end{cases}'))
display(Math('\\quad f(x) = \\begin{cases} 0,& -\\infty < x \\leq 0 \\\ -2x,& 0>x>+\\infty\\end{cases}'))
print('Exercise')
display(Math('\\quad f(x) = \\begin{cases} 0,& x \\leq 0 \\\ -2x,& x>0 \\: \\& \\: x<3 \\\ .1x^3, & x\\geq 3 \\end{cases}'))
from sympy.abc import x
piece1 = 0
piece2 = -2*x
piece3 = x**3/10
fx = sym.Piecewise((piece1,x<0), (piece2,(x>=0) & (x<3)), (piece3,x>=3))
print(fx)
fxx = sym.lambdify(x,fx)
xx = np.linspace(-3,5,1234)
plt.plot(xx,fxx(xx))
plt.show()
Exercise
display(Math('f(x) = \\begin{cases} x^3,& \\text{for} \\: x \\leq 0 \\\ \\log_2(x),& \\text{otherwise}\\end{cases}'))
f1 = x**3
f2 = sym.log(x,2)
fx = sym.Piecewise((f1,x<=0),(f2,x>0))
print(fx)
fxx = sym.lambdify(x,fx)
display(Math('f(x) = ' + sym.latex(fx)))
with plt.xkcd():
plt.plot(xx,fxx(xx),'k')
plt.xlim([-2,2])
plt.xlim([-2,2])
plt.ylim([-10,3])
plt.show()