Hallo guys, Berjumpa dengan Miniblog dari Inzaghi's Blog! Jika sebelumnya sudah membahas tentang NumPy dan Matplotlib, kali ini kita akan menerapkan ke dalam Fungsi Trigonometri Sin dan Cos dan sedikit menggunakan Library SciPy.
Berikut ini adalah Program Python untuk Menampilkan Rumus Plot Trigonometri menggunakan NumPy, SciPy, dan Matplotlib.
1. Import Library
import numpy as npimport scipy.linalg as laimport matplotlib.pyplot as plt
2. Pangkat Dua dan Tiga
# 1. Set x to a value, e.g., 5x = 5# 2. Compute the square and cube of xsquare_of_x = x**2cube_of_x = x**3print(f"Square of x: {square_of_x}")print(f"Cube of x: {cube_of_x}")
Output :
Square of x: 25
Cube of x: 125
3. Fungsi Trigonometri (Sinus dan Kosinus)
# 3. Set theta to an angle value (in radians)theta = np.pi / 4 # This sets theta to 45 degrees (pi/4 radians)# 4. Calculate sin(theta) and cos(theta)sin_theta = np.sin(theta)cos_theta = np.cos(theta)print(f"sin(theta): {sin_theta}")print(f"cos(theta): {cos_theta}")
Output :
sin(theta): 0.7071067811865476
cos(theta): 0.7071067811865476
4. Membuat Vektor Baris
# 5. Create a row vector meshPoints with 500 values evenly spaced between -1 and 1meshPoints = np.linspace(-1, 1, 500)# 6. The expression to get the 53rd element of meshPointselement_53 = meshPoints[52] # Python uses 0-based indexingprint(f"The 53rd element of meshPoints: {element_53}")
Output :
The 53rd element of meshPoints: -0.7915831663326653
5. Menampilkan dan Menyimpan Plot Grafik
# 7. Produce a plot of a sinusoid and save it as a JPEG fileplt.plot(meshPoints, np.sin(2 * np.pi * meshPoints))plt.xlabel('x')plt.ylabel('sin(2πx)')plt.title('Sinusoid Plot')plt.grid(True)plt.savefig('sinusoid_plot.jpg') # Save the plot as a JPEG fileplt.show()
Output :
6. Hasil di Jupyter Notebook
Mohon maaf apabila ada kesalahan sedikit pun pada Kode Program ini.
Terima Kasih 😀😊😘👌👍 :)
Tags
Kode Program