-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPrograma 5_2.py
More file actions
34 lines (31 loc) · 1.12 KB
/
Programa 5_2.py
File metadata and controls
34 lines (31 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-
"""
@author: guardati
Problema 5.2
"""
def calcula_raiz_biseccion(numero, error):
""" Obtiene la aproximación de la raíz cuadrada de un número
por medio del método de la bisección.
Parámetros:
numero: es un número int o float, > 0.
error: es un número float, > 0.
Regresa:
La aproximación de la raíz cuadrada del número con un error < error.
"""
inferior = 0
superior = numero
raiz = (inferior + superior) / 2
while abs(raiz ** 2 - numero) >= error:
if raiz ** 2 > numero:
superior = raiz
else:
inferior = raiz
raiz = (inferior + superior) / 2
return raiz
# =============================================================================
# Algunos usos de la función.
# =============================================================================
raiz1 = calcula_raiz_biseccion(9, 0.1)
raiz2 = calcula_raiz_biseccion(35.5, 0.001)
print(f'\nRaíz de 9, con un error menor a 0.1, es = {raiz1:.4f}')
print(f'Raíz de 35.5, con un error menor a 0.001, es = {raiz2:.5f}')