Метод бисекции тригонометрических функций в Python ⇐ Python

Программы на Python
Anonymous
Метод бисекции тригонометрических функций в Python

Сообщение Anonymous »

Я не знаю метода деления диапазона пополам. Таким образом, неправильный диапазон создает бесконечный цикл.

Код: Выделить всё

import math
import numpy as np

iteration = 0
eps = 1.0e-5 # the residual tolerance

x_upper =-np.pi/5
x_lower =-2*np.pi/5

guess = (x_upper + x_lower)*0.5 # Set guess as the mid point
eps = 1.0e-2
residual = - np.tan(guess) + 1/np.exp(guess) - 2.0
converged = math.fabs(residual) < eps

while not(converged):
residual = - np.tan(guess) + 1/np.exp(guess) - 2.0
print(guess)
if (residual < 0):
x_lower = guess
print(guess)
else:
x_upper = guess
print(guess)
guess = (x_upper + x_lower) * 0.5
converged = math.fabs(residual) < eps
iteration += 1
print(guess)

print("x =",guess)
print("LHS of the equation =",- np.tan(guess) + 1/np.exp(guess))
print("It took",iteration,"guesses to get that answer")
Я пытаюсь исправить это, изменив диапазон. Но все равно бесконечный цикл.

Подробнее здесь: https://stackoverflow.com/questions/790 ... -in-python

Вернуться в «Python»