コード例 #1
0
def root_solve_bisection_intervals(function, a, b, divisions, tol):
    start_time = time.time()#
    solutions = []
    step_size = (b-a)/divisions
    i=a
    while i < b:
        solution = root_solve_bisection(function, i, i+step_size, tol)
        if (solution != None):
            solutions.append(solution)
        i += step_size
    return solutions
コード例 #2
0
def root_solve_newton_hybrid(function, function_prime, a, b, tol, maxiter):
    c = root_solve_bisection(function, a, b, tol, 4)
    if c == None:
        return None
    solution = root_solve_newton(function, function_prime, c, tol, maxiter)
    return solution


#def function(x):
#    return x * math.cosh(x) + pow(x, 3) - math.pi
#def function_prime(x):
#    return math.cosh(x) + x * math.sinh(x) + 3 * pow(x, 2)
#print(root_solve_newton_hybrid(function, function_prime, 0, 2, 0.000001,20))
コード例 #3
0
def root_solve_secant_hybrid(function, a, b, tol, maxiter):
    c = root_solve_bisection(function, a, b, tol, 4)
    if c == None:
        return None
    #Find another point close to c, appropriate to the scale of a and b
    if abs(c - a) < abs(c - b):  #If c is closer to a
        d = c + ((b - a) / 10.0)
    else:
        d = c - ((b - a) / 10.0)
    solution = root_solve_secant(function, c, d, tol, maxiter)
    return solution


#def function(x):
#    return x * math.cosh(x) + pow(x, 3) - math.pi
#print(root_solve_secant_hybrid(function, 0, 2, 0.000001,20))
コード例 #4
0
def function_prime(x):
    return math.cosh(x) + x * math.sinh(x) + 3 * pow(x, 2)


print("The true root to the equation x * cosh(x)+ x^3 - pi is 1.0963278")
try:
    solution, maxiter = root_solve_fixed_point(function, 1, 0.000001, 1000,
                                               True)
except:
    solution, maxiter = "N/A", "N/A"
print(
    f"Using the fixed point iteration, the solution was {solution}, which took {maxiter} iterations."
)
solution, maxiter = root_solve_bisection(function,
                                         0,
                                         2,
                                         0.000001,
                                         getIterCount=True)
print(
    f"Using the bisection iteration, the solution was {solution}, which took {maxiter} iterations."
)
solution, maxiter = root_solve_newton(function, function_prime, 1, 0.000001,
                                      30, True)
print(
    f"Using the newton iteration, the solution was {solution}, which took {maxiter} iterations."
)
solution, maxiter = root_solve_secant(function, 1.0, 1.2, 0.000001, 30, True)
print(
    f"Using the secant iteration, the solution was {solution}, which took {maxiter} iterations."
)
コード例 #5
0
'''
coding language:    Python 3.7.0

written by:         Jonah Merrell
date written:       September 18 2019
written for:        Homework2 Task9
course:             Math 4610

purpose:            For this ask, compare the results of the functional iteration and Bisection iteration on
                    the problem x cosh(x) + x<sup>3</sup> = pi
'''
import sys, os
sys.path.append(os.path.abspath('../../mylibrary'))
from _mymodules import root_solve_bisection, root_solve_fixed_point
import math


def function(x):
    return x * math.cosh(x) + pow(x, 3) - math.pi


print("Attempting to solve equation with root_solve_fixed_point:")
try:
    print(root_solve_fixed_point(function, 1, 0.0001, 1000))
except OverflowError:
    print("An OverflowError occured")

print("")
print("Attempting to solve equation with root_solve_bisection:")
print(root_solve_bisection(function, 0, 2, 0.0001))