LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

プログラミング問題集解答例(問6)

Posted at

問6

def question6(f, a, b, e = 0.00001):
    if f(a) * f(b) >0:
        return False

    while abs(f(a) - f(b)) > e:
        c = (a + b) / 2
        if f(c) == 0:
            return c
        elif f(a) * f(c) >0:
            a = c 
        elif f(a) * f(c)<0:
            b = c

    return (a + b)/2
import math
def f(x):
    return math.exp(x)-3*x
question6(f,0,1)
0.6190605163574219
def g(x):
    return math.tanh(x) + x + 2
question6(g,0,1)
False
question6(g,-10,1)
-1.1743409633636475
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up