Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 38
# # Generate the exponential dataset with minor hub near the saturation. # # Author: Chong Zan Kai # Last update: 7-Feb-2018 # import matplotlib.pyplot as plt import numpy as np from sage.misc.prandom import gauss import pandas as pd # # Create the variables and the equation, i.e. f(t) = 1 - e^(-t) + u(t-delay) + -u(t-delay - pi/2) * sin( (t-delay)*2 ) * 0.3 # for t >= 0. # f, t = var('f t') delay = 3 f(t) = (1 - e^(-t) ) * unit_step(t) + (unit_step(t - delay) + -unit_step(t - delay - pi/2) ) * - sin((t - delay) *2 ) * 0.3 # # Building the data points # x_arr = np.linspace(0, 10, 200) y_arr = np.array([ f(x).n() for x in x_arr] ) y_gauss_arr = np.array( [y + gauss(0, 0.2) for y in y_arr] ) # # Build the dataframe # df = pd.DataFrame({'x':x_arr, 'y':y_arr, 'y_gauss': y_gauss_arr}) df.head() # Write to files df.to_csv('data.csv', index = False) # f_x = open('x.txt', 'w') # f_x.write('%s' % x_arr.tolist()) # f_x.close() # f_y = open('y.txt', 'w') # f_y.write('%s' % y_arr.tolist()) # f_y.close() # f_y_gauss = open('y_gauss.txt', 'w') # f_y_gauss.write('%s' % y_gauss_arr.tolist()) # f_y_gauss.close() # # Plot to graph # plt.plot(x_arr, y_arr, 'r-', label = 'Equation' ) plt.plot(x_arr, y_gauss_arr, 'bo', label = 'With Gaussian noise' ) plt.ylim(-0.2, 1.3) plt.xlim(0, 8 ) plt.title('Exponential dataset with minor hub') plt.xlabel('t') plt.ylabel('f') plt.legend(loc = 'lower right') plt.savefig('Graph.png') plt.show()
x y y_gauss 0 0.000000 0.000000000000000 -0.242034190448092 1 0.050251 0.0490095478443424 -0.305477333152103 2 0.100503 0.0956171599087779 0.223260498701876 3 0.150754 0.139940553979831 -0.266497339883467 4 0.201005 0.182091678548535 0.152274874333325 [<matplotlib.lines.Line2D object at 0x7ff459a24290>] [<matplotlib.lines.Line2D object at 0x7ff459a24710>] (-0.200000000000000, 1.30000000000000) (0, 8) <matplotlib.text.Text object at 0x7ff459ac0790> <matplotlib.text.Text object at 0x7ff459c860d0> <matplotlib.text.Text object at 0x7ff459c20b90> <matplotlib.legend.Legend object at 0x7ff459a24b50>