def main(): # 2분법을 이용하여 허용 응력과 안전률을 고려한 최소 직경을 구함 # 더 적은 초기값 x_l_init = root_finding.epsilon_global * 2 # 더 큰 초기값 x_h_init = 1.0 # 2분법 호출 result = root_finding.bisection(problem_to_solve, x_l_init, x_h_init, 1e-9) # 결과 표시 print "result =", result
def main(): # # x_l_init = root_finding.epsilon_global * 2 # x_h_init = 1.0 # result = root_finding.bisection(problem_to_solve, x_l_init, x_h_init, 1e-9) # print "result =", result
def test_bisection(self): """ 이분법 함수를 검증하는 method 여기서의 method는 어떤 class에 속한 함수를 뜻함 매개변수의 self는 이 class를 붕어빵 틀로 삼아 만들어진 붕어빵인 객체를 의미함 :return: """ # 1변수 방정시의 해를 이분법으로 찾음 result = rf.bisection(f, 0.01, 100, epsilon=1e-8) #이렇게 찾아진 결과는 f(x) = 0 을 만족해야 함 # 실수 연산은 미소한 오차가 있을 수 있으므로 유효숫자 6번째 자리 까지 확인함 self.assertAlmostEqual(0.0, f(result), places=6)
def main(): root_finding.epsilon = 1e-9 r_init = root_finding.epsilon delta_r = 1e-6 result = root_finding.sequential(problem_to_solve, r_init, delta_r) print "result =", result print "f(result)=", problem_to_solve(result) result_bisection = root_finding.bisection(problem_to_solve, result - delta_r, result) print "result_bisection =", result_bisection print "f(result_bisection) =", problem_to_solve(result_bisection) help(root_finding.sequential)
def proj_weightedl1ball(y, weight, radius, lamb, method): """ Projection onto the weigthed l1 norm ball: min_{x} ||x-y||_2^2 s.t. \sum_i weight_ix_i\leq radius x_i >= 0 where y_i >= 0, i=1,...,n Parameters ---------- y: (n, ) numpy array, all elements positive n-dimensional vector to project weight: (n, ) numpy array, all elements positive n-dimensional weight vector for l1 norm ball radius: int, optional, default: 1, radius of the weighted L1-ball Returns ------- x_opt: (n, ) numpy array, all elements non-negative Euclidean projection of y onto the weighted L1-ball """ assert all(entry >= 0 for entry in y), "y must be positive!" if weight.dot(y) <= radius: return y, 0 else: # use the projection way if method == 'bisection': x_opt, lamb = root_finding.bisection(weight, y, radius, lamb) elif method == 'sortBased': x_opt, lamb = root_finding.sortBased(weight, y, radius) elif method == 'projection': act_ind = range(len(y)) while True: # calculate y_bar_act y_act = y[act_ind] weights_act = weight[act_ind] x_sol_hyper, lamb = proj_hyperplane(y_act, weights_act, radius) y_act = np.maximum(x_sol_hyper, 0.0) y[act_ind] = y_act # update the active set act_ind = y > 0 if sum(x_sol_hyper < 0) == 0: x_opt = y * np.sign(y) break return x_opt, lamb
def main(): root_finding.epsilon = 1e-9 r_init = root_finding.epsilon delta_r = 1e-6 result = root_finding.sequential(problem_to_solve, r_init, delta_r) print "result =", result print "f(result)=", problem_to_solve(result) result_bisection = root_finding.bisection(problem_to_solve, result - delta_r, result) print "result_bisection =", result_bisection print "f(result_bisection) =", problem_to_solve(result_bisection) help(root_finding.sequential)
def main(): # 객체를 만듦 experiment = Experiment() # 객체의 변수 force_N 을 지정하기 위해 사용자 입력을 받아 들임 # force_N 에는 문자열? 실수 중 어떤 것이 저장될 것인가? experiment.force_N = float(raw_input("Enter force (N):")) # 예를 들어 허용 최대 응력이나 안전률을 변경하는 것도 객체를 수정 하는 대신 main() 함수 값을 바꾸면 됨 # 2분법을 이용하여 허용 응력과 안전률을 고려한 최소 직경을 구함 # 더 적은 초기값 x_l_init = root_finding.epsilon_global * 2 # 더 큰 초기값 x_h_init = 1.0 # 2분법 호출 result = root_finding.bisection(experiment.problem_to_solve, x_l_init, x_h_init, 1e-9) # 결과 표시 print "result =", result
import numpy as np import math from root_finding import bisection def h(x: float) -> float: return math.exp(1)**x - 1.5 if isinstance(h(0.0), float): x = bisection(h, 0.0, 1, 1e-3) print(f"exp({x:g}) =", np.exp(x))
# to test repl embedding # press [play] to run import root_finding as rf def f(x): return 10 - x * x print(rf.bisection(f, 0, 5, 1e-5))
def test_bisection(self): result = rf.bisection(f, 0.0001, 100, epsilon=1e-8) self.assertAlmostEqual(0.0, f(result), places=3)