import numpy as np
import matplotlib.pyplot as plt
deffunc_x2(x):return x*np.abs(x)deffunc_sin(x):return np.sin(x)definc_init(s, x_lb, x_ub, func):"""
Incremental piecewise linearization
:param s: Number of segments
:param x_lb: Low bound of x
:param x_ub: Up bound of x
:param func: function
:return: x, y
"""
x = np.zeros((s+1,))
y = np.zeros((s+1,))for i inrange(s+1):
x[i]= x_lb + i*(x_ub-x_lb)/s
y[i]= func(x[i])return x, y