Advertisement
kurumi64

Soal no 2 TakeHome UAS Metnum

Jun 9th, 2023 (edited)
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.78 KB | Source Code | 0 0
  1. import matplotlib.pyplot as plt # untuk memunculkan grafik
  2.  
  3. def clamped_cubic_spline(X, fx, f_aksen_awal_kurva, f_aksen_akhir_kurva, n):
  4.     n = len(X) - 1
  5.     h = [X[i+1] - X[i] for i in range(n)]
  6.     alpha = [0] * (n+1)
  7.     for i in range(0, n):
  8.         alpha[i] = (3/h[i]) * (fx[i+1] - fx[i]) - (3/h[i-1]) * (fx[i] - fx[i-1])
  9.  
  10.     l = [1] + [0] * n
  11.     u = [0] * (n+1)
  12.     z = [0] * (n+1)
  13.  
  14.     l[1] = 2 * h[0]
  15.     u[1] = 0.5
  16.     z[1] = alpha[1] / l[1]
  17.  
  18.     for i in range(2, n):
  19.         l[i] = 2 * (X[i] - X[i-1]) - h[i-1] * u[i-1]
  20.         u[i] = h[i] / l[i]
  21.         z[i] = (alpha[i] - h[i-1] * z[i-1]) / l[i]
  22.  
  23.     l[n] = h[n-1] * (2 - u[n-1])
  24.     z[n] = (alpha[n] - h[n-1] * z[n-1]) / l[n]
  25.     c = [0] * (n+1)
  26.     b = [0] * (n+1)
  27.     d = [0] * (n+1)
  28.  
  29.     c[n] = z[n]
  30.     for j in range(n-1, -1, -1):
  31.         c[j] = z[j] - u[j] * c[j+1]
  32.    
  33.     for i in range(n):
  34.         b[i] = (fx[i+1] - fx[i]) / h[i] - h[i] * (c[i+1] + 2 * c[i]) / 3
  35.         d[i] = (c[i+1] - c[i]) / (3 * h[i])
  36.  
  37.     a = fx
  38.  
  39.     ukuran_partisi = (X[-1] - X[0]) / n
  40.     nilai_x = [X[0] + i * ukuran_partisi for i in range(n+1)]
  41.     nilai_y = []
  42.     for x in nilai_x:
  43.         for i in range(n):
  44.             if X[i] <= x <= X[i+1]:
  45.                 y = fx[i] + b[i] * (x - X[i]) + c[i] * (x - X[i])**2 + d[i] * (x - X[i])**3
  46.                 nilai_y.append(y)
  47.                 break
  48.  
  49.     return nilai_x, nilai_y, a, b, c, d
  50.  
  51. # KURVA 1
  52. data_kurva_1 = [
  53.     [1, 2, 5, 6, 7, 8, 10, 13, 17],                 # Data titik Xi
  54.     [3.0, 3.7, 3.9, 4.2, 5.7, 6.6, 7.1, 6.7, 4.5]   # Data titik f(Xi)
  55. ]
  56. x1 = data_kurva_1[0]
  57. fx1 = data_kurva_1[1]
  58. f_aksen_awal_kurva_1 = float(1.0)
  59. f_aksen_akhir_kurva_1 = float(-0.67)
  60. i_kurva_1 = 8
  61.  
  62. # KURVA 2
  63. data_kurva_2 = [
  64.     [17, 20, 23, 24, 25, 27, 27.7],         # Data titik Xi
  65.     [4.5, 7.0, 6.1, 5.6, 5.8, 5.2, 4.1]     # Data titik f(Xi)
  66. ]
  67. x2 = data_kurva_2[0]
  68. fx2 = data_kurva_2[1]
  69. f_aksen_awal_kurva_2 = float(1.0)
  70. f_aksen_akhir_kurva_2 = float(-0.67)
  71. i_kurva_2 = 6
  72.  
  73. # KURVA 3
  74. data_kurva_3 = [
  75.     [27.7, 28, 29, 30],     # Data titik Xi
  76.     [4.1, 4.3, 4.1, 3.0]    # Data titik f(Xi)
  77. ]
  78. x3 = data_kurva_3[0]
  79. fx3 = data_kurva_3[1]
  80. f_aksen_awal_kurva_3 = float(0.33)
  81. f_aksen_akhir_kurva_3 = float(-1.5)
  82. i_kurva_3 = 3
  83.  
  84. # Interpolasi dengan Clamped Cubic Spline
  85. x_kurva_1, y_kurva_1, a1, b1, c1, d1 = clamped_cubic_spline(x1, fx1, f_aksen_awal_kurva_1, f_aksen_akhir_kurva_1, i_kurva_1)
  86. x_kurva_2, y_kurva_2, a2, b2, c2, d2 = clamped_cubic_spline(x2, fx2, f_aksen_awal_kurva_2, f_aksen_akhir_kurva_2, i_kurva_2)
  87. x_kurva_3, y_kurva_3, a3, b3, c3, d3 = clamped_cubic_spline(x3, fx3, f_aksen_awal_kurva_3, f_aksen_akhir_kurva_3, i_kurva_3)
  88.  
  89. plt.title('Kurva')
  90.  
  91. # Cetak hasil interpolasi
  92.  
  93. print("\n")
  94. print("Hasil interpolasi Kurva 1:")
  95. for i in range(len(x_kurva_1)):
  96.     print(f"i = {i}, xi = {x1[i]}, a = {a1[i]:.4f}, b = {b1[i]:.4f}, c = {c1[i]:.4f}, d = {d1[i]:.4f}")
  97.     # print(f"x = {x_kurva_1[i]:.4f}, y = {y_kurva_1[i]:.4f}")
  98.    
  99. print("\n")
  100.  
  101. print("Hasil Interpolasi Kurva 2:")
  102. for i in range(len(x_kurva_2)):
  103.     print(f"i = {i}, xi = {x2[i]}, a = {a2[i]:.4f}, b = {b2[i]:.4f}, c = {c2[i]:.4f}, d = {d2[i]:.4f}")
  104.     # print(f"x = {x_kurva_2[i]:.10f}, y = {y_kurva_2[i]:.10f}")
  105.  
  106. print("\n")
  107.  
  108. print("Hasil interpolasi Kurva 3:")
  109. for i in range(len(x_kurva_3)):
  110.     print(f"i = {i}, xi = {x3[i]}, a = {a3[i]:.4f}, b = {b3[i]:.4f}, c = {c3[i]:.4f}, d = {d3[i]:.4f}")
  111.     # print(f"x = {x_kurva_3[i]:.10f}, y = {y_kurva_3[i]:.10f}")
  112.  
  113. plt.plot(x_kurva_1, y_kurva_1, color='red', label='Kurva 1')
  114. plt.plot(x_kurva_2, y_kurva_2, color='green', label='Kurva 2')
  115. plt.plot(x_kurva_3, y_kurva_3, color='aqua', label='Kurva 3')
  116.  
  117. plt.xlabel('X')
  118. plt.ylabel('f(X)')
  119. plt.title('Interpolasi Clamped Cubic Spline')
  120. plt.legend()
  121. plt.grid(True)
  122. plt.show()
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement