Fan: Python dasturlash tili 2021-2022 – o’quv yili
Topshiriq 1. Quyidagi ifodani ε=0.0001 aniqlikda while operatori ishtirokida dasturini tuzing va natijani ilova qiling.
Masalaning python dasturlash tilidagi yechimi:
import math
def factarial(n: int) -> int:
"""
n faktorial qiymatini hisoblaydi
:param n: int
:return: int
"""
if n in (0, 1):
return 1
return n * factarial(n - 1)
def f(x: float, n: int) -> float:
"""
(x^n)/(n!) ifodaning qiymani "eps" aniqlikda hisoblaydi
:param x: float
:param n: int
:return: float
"""
eps = 0.0001
result = math.pow(x, n) / factarial(n)
if result < eps:
return 0
return round(result, 4)
def main(x):
"""
ifodalar qiymatlarini qo'shib boradi
:param x: float
:return: float
"""
n = int()
result = float()
while True:
current_value = f(x, n)
if current_value == 0:
break
result += current_value
n += 1
return result
if __name__ == '__main__':
try:
arg = float(input('x = '))
print(f'Ifodaning qiymat: {main(arg)}')
except ValueError:
print('Noto\'g\'ri qiymat kiritildi.')
Dastur natijasi:
Topshiriq 2. Mathplotlib kutubhonasi. y1(x) - y6(x) funksiyalar jadvali berilgan. Agar a=2, b=3, c=1, d=-4 berilgan bo’lsa funksiyani [-10, 10] oraliqda h=0.5 qadam bilan grafigi va dasturi ilova qilinsin. y1(x), y2(x), y3(x) lar bitta dekart kordinata sistemasida, y4(x), y5(x), y6(x)-lar ikkinchi dekart kordinata sistemasida tasviri shakillansin.
Masalani python dasturlash tilida yechish:
Birinchi bo’lib mathplotlib kutubxonasini o’rnatib olishimiz zarur. Buning uchun kommadalar qatoriga pip install matplotlib buyrug’ini beramiz.
Bunda mathplotlib kutubxonasi hamda bu kutubxona ishlashi uchun zarur bo’lgan boshqa kutubxonalar avtomatik o’rnatiladi.
Dastur kodi:
import math
import matplotlib.pyplot as plt
import numpy as np
a, b, c, d = 2, 3, 1, -4
x = [i / 2 for i in range(-20, 21)]
x_points = np.array(x)
y1 = np.array([b * math.cos(i) for i in x])
y2 = np.array([math.cos(b * i) for i in x])
y3 = np.array([b + math.cos(i) for i in x])
y4 = np.array([c * (i ** 3) for i in x])
y5 = np.array([a * (i ** 2) + c * (i ** 3) for i in x])
y6 = np.array([a * i + c * (i ** 3) for i in x])
fig1, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 10), layout='constrained')
ax1.plot(x_points, y1, 'r', label='y1')
ax1.plot(x_points, y2, 'g', label='y2')
ax1.plot(x_points, y3, 'b', label='y3')
ax1.legend()
ax1.set_title('y1, y2, y3 funksiyalar')
ax1.set_xlabel('x o\'qi')
ax1.set_ylabel('y o\'qi')
ax2.plot(x_points, y4, 'r', label='y4')
ax2.plot(x_points, y5, 'g', label='y5')
ax2.plot(x_points, y6, 'b', label='y6')
ax2.legend()
ax2.set_title('y4, y5, y6 funksiyalar')
ax2.set_xlabel('x o\'qi')
ax2.set_ylabel('y o\'qi')
plt.savefig('graph.png')
plt.show()
Dastur natijasi:
orginal grafik rasmi
Guruh: 19.06 Bajardi: Zoidjonov Ahliyorjon
Do'stlaringiz bilan baham: |