O‘ZBEKISTON RESPUBLIKASI
RAQAMLI TEXNOLOGIYALAR VAZIRLIGI
MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETINURAFSHON FILIALI
Kompyuter injiniringi fakulteti
Dasturiy injineringi yo’nalishi
Guruh nomi :311-22 guruh
F.I.SH: Toirov Sahobiddin Axrorjon o’g’li
1. Oraliqni Teng Ikkiga Bo'lish Usuli (Bisection Method)
Bisection usuli tenglamani yechish uchun eng oddiy va ishonchli usullardan biridir. Bu usul yordamida yechimni [a, b] oraliqda izlaymiz, bunda f(a) va f(b) qiymatlari har xil belgilarga ega bo'lishi kerak (ya'ni f(a) * f(b) < 0). Usul quyidagi qadamlarni o'z ichiga oladi:
1.Oraliqni yarmiga bo'lish.
2.Yarim oraliqdagi qiymatni hisoblash.
1.Dastur kodi:
def bisection_method(f, a, b, tol):
if f(a) * f(b) >= 0:
print("Bisection method requires that f(a) and f(b) have opposite signs")
return None
c = a
while (b - a) / 2.0 > tol:
c = (a + b) / 2.0
if f(c) == 0:
return c
elif f(a) * f(c) < 0:
b = c
else:
a = c
return c
# Misol: x^2 - 4 = 0
f = lambda x: x**2 - 4
root = bisection_method(f, 1, 3, 0.001)
print("Root is:", root)
2. Nyuton-Raphson Usuli (Newton-Raphson Method)
Nyuton usuli yuqori tezlikda yaqinlashuvchi iteratsion metod hisoblanadi. Bu usul quyidagi qadamni o'z ichiga oladi:
Tangent chiziq orqali yangi taxminiy qiymatni topish.
Tangent chiziqdan foydalanib yangi taxminiy qiymatni hisoblash.
2.Dastur kodi:
def newton_raphson(f, df, x0, tol, max_iter=100):
xn = x0
for n in range(max_iter):
fxn = f(xn)
if abs(fxn) < tol:
print("Found solution after", n, "iterations.")
return xn
dfxn = df(xn)
if dfxn == 0:
print("Zero derivative. No solution found.")
return None
xn = xn - fxn / dfxn
print("Exceeded maximum iterations. No solution found.")
return None
# Misol: x^2 - 4 = 0
f = lambda x: x**2 - 4
df = lambda x: 2*x
root = newton_raphson(f, df, 3, 0.001)
print("Root is:", root)
|