""" S vyuzitim Babylonskej metody vypocita odmocninu cisla n. """ assert n > 0, 'Cislo n musi byt vacsie ako nula' assert x0 > 0, 'Pociatocna hodnota x0 musi byt vacsia ako nula' assert e > 0, 'Presnost e musi byt vacsia ako nula' while True: # nova aproximacia je priemerom hodnot x0 a n / x0 x = 0.5 * (x0 + n / x0) logger.info( 'x = {0}, x0 = {1}, Abs(x - x0) = {2}'.format(x, x0, Abs(x - x0))) # skonci ak je dosiahnuta pozadovana presnost if Abs(x - x0) < e: return x x0 = x if __name__ == '__main__': n = int_input('Zadajte cislo n') x0 = float_input('Zadajte pociatocnu hodnotu x0') e = float_input('Zadajte presnost e', default=0.01) r = babylon(n, x0, e) print('Odmocnina je: {0}'.format(r))
f = lambda x: eval_expr(fn, x=x) while True: # stred intervalu <a,b> x = 0.5 * (a + b) logger.info('a = {0}, b = {1}, x = {2}'.format(a, b, x)) # skonci ak je stred intervalu # korenom funkcie inak uprav interval if f(x) == 0: return x elif f(a) * f(x) < 0: b = x else: a = x # skonci pri dosiahnuti presnosti if b - a <= e: return x if __name__ == '__main__': a = float_input('Zadajte cislo a') b = float_input('Zadajte cislo b') fn = expr_input('Zadajte funkciu fn') e = float_input('Zadajte presnost e', default=0.01) r = bisection(a, b, fn, e) print('Koren je: {0}'.format(r))
sum_a += a[i, j] * x1[j] for j in range(i + 1, a.rows): sum_b += a[i, j] * x[j] # vypocet dalsej aproximacie x1[i] = (1 / a[i, i]) * (b[i] - sum_a - sum_b) # vypocet rozdielu prvej a druhej aproximacie (norma) error = norm_error(x1, x) logger.info('x1 = {0}\nx = {1}\nerror = {2}\n'.format( list_as_float(x1), list_as_float(x), float(error))) # skonci pri dosiahnuti presnosti if error <= e: return list_as_float(x1) x = list(x1) if __name__ == '__main__': a = matrix_input('Zadajte maticu a') b = list_input('Zadajte vektor pravej strany b') x = list_input('Zadajte vektor aproximacie x') e = float_input('Zadajte presnost e', default=0.01) r = gaussseidel(a, b, x, e) print('Vysledny vektor:\n{0}'.format(r))