增量分段线性化

import numpy as np
import matplotlib.pyplot as plt
def func_x2(x):
    return x*np.abs(x)

def func_sin(x):
    return np.sin(x)


def inc_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 in range(s+1):
        x[i] = x_lb + i*(x_ub-x_lb)/s
        y[i] = func(x[i])
    return x, y

s i n sin sin

# Real
step = 0.1
x_lb, x_ub = 0, 10
x = np.arange(x_lb, x_ub+step, step)
# Inc
inc_x, inc_y = inc_init(20, x_lb, x_ub, func_sin)
# 
plt.plot(x, func_sin(x), 'k-.')
plt.plot(inc_x, inc_y, 'r-*')
plt.legend(['Real', 'Inc'])
plt.show()

在这里插入图片描述

x 2 x^2 x2

# Real
step = 0.1
x = np.arange(2, 10+step, step)
# Inc
inc_x, inc_y = inc_init(10, 2, 10, func_x2)
# 
plt.plot(x, func_x2(x), 'k-.')
plt.plot(inc_x, inc_y, 'r-*')
plt.legend(['Real', 'Inc'])
plt.show()

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值