0% found this document useful (0 votes)
12 views23 pages

Latihan Membuat Grafik

Uploaded by

ShaDoWReAp221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views23 pages

Latihan Membuat Grafik

Uploaded by

ShaDoWReAp221
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Untuk menghitung luas daerah di bawah kurva

dengan fungsi : f(x) = 2𝑥 +3x+4


f(x) = 2𝑥 +3x+4

Dengan batas dari x =4 sampai x = 9, Type equation here.

digunakan integral:
Area = ∫ 𝑓 𝑥 𝑑𝑥 = ∫ 2𝑥 +3x+4

dapat digunakan pendekatan multigrid sbb:

Area =

Area = A1 + A2+ …..+ An


import matplotlib.pyplot as plt
import numpy as np

dx = 1
x = np.arange(0,10,dx)
a = 2
b = 3
c = 4
y = [a*x1**2 + b*x1 + c for x1 in x]
y1 = [a*(x1-0.5*dx)**2+b*(x1-0.5*dx)+c for x1 in x]

#plot grafik
plt.plot(x, y1, color = 'red', linewidth=2, label='y=2x^2+3x+4')
plt.title('Menghitung Luas di Bawah
Kurva',fontsize=14,fontstyle='italic')
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc='upper left', fontsize=12)
plt.xticks(np.arange(min(x), max(x)+1, dx))
pos = (4.5, 5.5, 6.5, 7.5, 8.5)
h = [y[4],y[5],y[6],y[7],y[8]]
plt.bar(pos, h, width=dx, fill=False, hatch ='.')

#menghitung luas

Tot_A = 0
for i in range(4,9):
A = y1[i+1] * dx
Tot_A = Tot_A + A

plt.text (0,150,'Area di bawah kurva= %.2f' %(Tot_A))

plt.show()
Untuk memplot grafik 3 dimensi pada Matplotlib
dapat dilakukan dengan
mengimport mplot3d toolkit, yang sudah terinstall
bersama instalasi Matplotlib, dengan perintah :
from mpl_toolkits import
Dan dilanjutkan dengan mengimport fungsi grafik
seperti biasa:
import matplotlib.pyplot as plt
mimport numpy as np
Kemudian memanggil fungsi 3D:
Fig= plt.figure()
ax= plt.axes (projection='3d')
from mpl_toolkits import mplot3d

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = plt.axes(projection='3d')

# Data for a three-dimensional line


zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline, 'gray')

# Data for three-dimensional scattered points


zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens');

plt.show()
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

def f(x, y):


return np.sin(np.sqrt(x ** 2 + y ** 2))

x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)

# 3D contour
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');
ax.set_title('3D Contour');
ax.view_init(60, 35)
plt.show()
# Wireframe
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_wireframe(X, Y, Z, color='black')
ax.set_title('wireframe’)

plt.show()
# Surface
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap='viridis', edgecolor='none')
ax.set_title('surface');

r = np.linspace(0, 6, 20)
theta = np.linspace(-0.9 * np.pi, 0.8 * np.pi, 40)
r, theta = np.meshgrid(r, theta)

X = r * np.sin(theta)
Y = r * np.cos(theta)
Z = f(X, Y)

ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap='viridis', edgecolor='none');

plt.show()

You might also like