MATLAB-在不知道x实际值的情况下构建图形

时间:2020-07-29 07:13:17

标签: matlab math octave

我要在Matlab中完成以下任务:

构建以下功能的图形

x+sin(3x)/(x^2+1)

如何在不知道 x 实际值的情况下构建图形?

1 个答案:

答案 0 :(得分:1)

您可以使用ezplot来做到这一点:

import numpy as np
import pandas as pd
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

x = np.array([[44360.125],
              [56385.958333333336],
              [61500.5],
              [61227.375],
              [60049.333333333336],
              [51396.916666666664],
              [49225.208333333336],
              [63211.083333333336],
              [64631.916666666664],
              [62546.708333333336],
              [62825.125]])

x_ax = np.array([pd.Timestamp('2018-01-01 00:00:00'),
                 pd.Timestamp('2018-01-02 00:00:00'),
                 pd.Timestamp('2018-01-03 00:00:00'),
                 pd.Timestamp('2018-01-04 00:00:00'),
                 pd.Timestamp('2018-01-05 00:00:00'),
                 pd.Timestamp('2018-01-06 00:00:00'),
                 pd.Timestamp('2018-01-07 00:00:00'),
                 pd.Timestamp('2018-01-08 00:00:00'),
                 pd.Timestamp('2018-01-09 00:00:00'),
                 pd.Timestamp('2018-01-10 00:00:00'),
                 pd.Timestamp('2018-01-11 00:00:00'),
                 pd.Timestamp('2018-01-12 00:00:00'),
                 pd.Timestamp('2018-01-13 00:00:00'),
                 pd.Timestamp('2018-01-14 00:00:00'),
                 pd.Timestamp('2018-01-15 00:00:00'),
                 pd.Timestamp('2018-01-16 00:00:00'),
                 pd.Timestamp('2018-01-17 00:00:00'),
                 pd.Timestamp('2018-01-18 00:00:00'),
                 pd.Timestamp('2018-01-19 00:00:00'),
                 pd.Timestamp('2018-01-20 00:00:00'), 
                 pd.Timestamp('2018-01-21 00:00:00'),
                 pd.Timestamp('2018-01-22 00:00:00'),
                 pd.Timestamp('2018-01-23 00:00:00'),
                 pd.Timestamp('2018-01-24 00:00:00')])

x_ax=x_ax[:11]
x_ax

kmeans = KMeans(n_clusters=1, random_state=0).fit(x)
center = kmeans.cluster_centers_

distance = ((x - center)**2)**.5
order_index = np.argsort(distance, axis = 0)
indexes = order_index[-5:]
values = x[indexes]

fig, ax = plt.subplots(1,1) # Added
plt.plot(x_ax,x)
plt.scatter(x_ax[indexes], values, color='r')
plt.show()

enter image description here