Solusi Latihan Uas Bu Athaya

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

No 1

import numpy as np
import matplotlib.pyplot as plt

'''
x + 4y = 18
0.3x - y = -11
2x + y = 30
x + y = 32
'''

# x + 4y = 18 dengan 0.3x - y = -11


A = np.array([(1, 4), (0.3, -1)])
Ay = np.array([18, -11])
Xa = np.linalg.solve(A, Ay)

# x + 4y = 18 dengan 2x + y = 30
B = np.array([(1, 4), (2, 1)])
By = np.array([18, 30])
Xb = np.linalg.solve(B, By)

# x + 4y = 18 dengan x + y = 32
C = np.array([(1, 4), (1, 1)])
Cy = np.array([18, 32])
Xc = np.linalg.solve(C, Cy)
# 0.3x - y = -11 dengan 2x + y = 30
D = np.array([(0.3, -1), (2, 1)])
Dy = np.array([-11, 30])
Xd = np.linalg.solve(D, Dy)

#0.3x - y = -11 dengan x + y = 32


E = np.array([(0.3, -1), (1, 1)])
Ey = np.array([-11, 32])
Xe = np.linalg.solve(E, Ey)

#2x + y = 30 dengan x + y = 32
F = np.array([(2, 1), (1, 1)])
Fy = np.array([30, 32])
Xf = np.linalg.solve(F, Fy)

print('x + 4y = 18 dengan 0.3x - y = -11 ', Xa)


print('x + 4y = 18 dengan 2x + y = 30 ', Xb)
print('x + 4y = 18 dengan x + y = 32 ', Xc)
print('0.3x - y = -11 dengan 2x + y = 30 ', Xd)
print('0.3x - y = -11 dengan x + y = 32 ', Xe)
print('2x + y = 30 dengan x + y ', Xf)

x_list = np.linspace(-30, 30, 10)

y1 = [(18 - x)/4 for x in x_list] #f(x) = (18 - x) / 4


y2 = [(0.3*x + 11) for x in x_list]
y3 = [(30 - 2*x) for x in x_list]
y4 = [(32 - x) for x in x_list]

plt.plot(x_list, y1, color = 'red', label = 'y = (18 - x)/4')


plt.plot(x_list, y2, color = 'yellow', label = 'y = 0.3x + 11')
plt.plot(x_list, y3, color = 'green', label = 'y = 30 - 2x')
plt.plot(x_list, y4, color = 'blue', label = 'y = 32 - x')

plt.title("Grafik Empat Fungsi Linier", fontsize = 14, fontname = "Arial")


plt.xlabel("X", fontsize = 12, fontname = "Arial")
plt.ylabel("Y", fontsize = 12, fontname = "Arial")

legend = plt.legend(loc = "upper right", fontsize = 10)

name_list = [Xa, Xb, Xc, Xd, Xe, Xf]


for i in name_list:
xi = f"{i[0]:.2f}"
yi = f"{i[1]:.2f}"
plt.text(i[0], i[1], f"{xi, yi}", fontstyle = 'italic')

plt.grid('grid', linestyle = '-', color = 'purple')


plt.show()
No 2
import matplotlib.pyplot as plt
import numpy as np

# Buat data yang akan diplot


x = np.arange(0, 2*3.14, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)
# y3 = np.tan(x)

# Plot Grafik
plt.title("Grafik Sinus Dan Cosinus")

data_a = plt.plot(x, y1,'o', label = 'Sinus')


data_b = plt.plot(x, y2, 'y', label = 'Cosinus')
# data_c = plt.plot(x, y3, '-', label = 'Tangen')

plt.setp(data_a, color = 'y', linestyle = '--', linewidth = 1)


plt.setp(data_b, color = 'g', linestyle = '-.', linewidth = 3)
# plt.setp(data_c, color = 'b', linestyle = ':', linewidth = 1)

legend = plt.legend(loc = 'upper right')


legend.get_frame().set_facecolor('tomato')

plt.xlabel(r"$ Sumbu \theta $", fontsize = 12, fontname = "Arial")


plt.ylabel("Sumbu Y", fontsize = 12, fontname = "Arial")
plt.text(2, 1, r'$ Y = sin(\theta)$')
plt.text(2, 0.75, r'$ Y = cos(\theta)$')
# plt.text(2, 1.25, r'$ Y = tan(\theta)$')

plt.yticks([-1, 0, 1])
plt.xticks([0, 2*3.14*.25, 2*3.14*.5, 2*3.14*.75, 2*3.14], [r'${0}^o$', r'${90}^o$',
r'${180}^o$', r'${270}^o$', r'${360}^o$'])
plt.show()
No 3
from scipy.interpolate import interp1d
from scipy.interpolate import CubicSpline
import matplotlib.pyplot as plt
import numpy as np

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12]


y = [120, 250, 122, 100, 75, 68, 34, 50, 55, 124, 315]

#linear
f_l = interp1d(x, y)

f_c = CubicSpline(x, y, bc_type = 'natural')


x_cub = np.linspace(0, 12, 100)
y_cub = f_c(x_cub)

plt.plot(x, y, 'y')
plt.plot(x_cub, y_cub, 'r')
plt.plot(11, f_c(11), 'bo', label = f'cubic spline {f_c(11):.2f}')
plt.plot(11, f_l(11), 'ro', label = f'linear {f_l(11):.2f}')

plt.legend(loc = "upper right")

plt.show()
No 4
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x = [2, 6, 4, 3, 14, 6, 1, 9, 18]

tabel = pd.DataFrame({'Pegawai (jml)': x})

print('Deskripsi Statik')
print(tabel.describe())

jml_pegawai = tabel['Pegawai (jml)']

plt.boxplot([jml_pegawai], labels = ["Jumlah Pegawai"], showmeans = True)

plt.show()

You might also like