from root_bisection import * from root_regula_falsi import * from root_regula_falsi_mod import * from root_ridder import * def f(x): return x**4 - 8*x**3 - 35*x**2 + 450*x - 1001 xl = 5.5 #4.5 xu = 10.0 #6.0 xr = root_bisection(f, xl, xu) xr = root_regula_falsi(f, xl, xu) xr = root_regula_falsi_mod(f, xl, xu) xr = root_ridder(f, xl, xu)
from math import sqrt, tanh from root_regula_falsi import * from root_bisection import * from root_ridder import * g = 9.81 v = 5.0 t = 2.5 L = 4.0 def f(H): x = sqrt(2 * g * H) return v - x * tanh(x / (2 * L) * t) def calc_v(H): x = sqrt(2 * g * H) return x * tanh(x / (2 * L) * t) Hl = 0.0 Hu = 2.0 Hr = root_bisection(f, Hl, Hu) print("v = ", calc_v(Hr), " (should be close to ", v, ")") Hr = root_regula_falsi(f, Hl, Hu) print("v = ", calc_v(Hr), " (should be close to ", v, ")") Hr = root_ridder(f, Hl, Hu) print("v = ", calc_v(Hr), " (should be close to ", v, ")")
from math import exp from root_regula_falsi import * from root_bisection import * from root_ridder import * g = 9.81 t = 4.0 v = 36.0 m = 82.0 def f(c): return v - g*m/c*(1 - exp(-c*t/m)) def calc_v(c): return g*m/c*(1 - exp(-c*t/m)) cl = 3.0 cu = 5.0 cr = root_bisection(f, cl, cu) print("v = ", calc_v(cr), " (should be close to ", v, ")") cl = 3.0 cu = 5.0 cr = root_regula_falsi(f, cl, cu) print("v = ", calc_v(cr), " (should be close to ", v, ")") cl = 3.0 cu = 5.0 cr = root_ridder(f, cl, cu) print("v = ", calc_v(cr), " (should be close to ", v, ")")
from root_regula_falsi import * from root_bisection import * from root_regula_falsi_mod import * from root_ridder import * r1 = 0.5 r2 = 1.0 h = 1.0 ρf = 200.0 ρw = 1000.0 def calc_V_frustum(r1, r2, h): return pi * h / 3 * (r1**2 + r2**2 + r1 * r2) # h1/h = (r2-r1)/r # -> r = (r2-r1)*h/h1 def f(h1): r = (r2 - r1) * h / h1 Vw = calc_V_frustum(r1 + r, r2, h - h1) # displaced volume of water Vf = calc_V_frustum(r1, r2, h) # cone volume return ρw * Vw - ρf * Vf xr = root_regula_falsi(f, 0.1, 1.0, NiterMax=1000) xr = root_regula_falsi_mod(f, 0.1, 1.0, NiterMax=1000) xr = root_bisection(f, 0.01, 1.0, NiterMax=1000) xr = root_ridder(f, 0.01, 1.0, NiterMax=1000)
from math import exp from root_regula_falsi import * from root_bisection import * from root_ridder import * g = 9.81 c = 15 t = 10.0 v = 36.0 def f(m): return v - g*m/c*(1 - exp(-c*t/m)) def calc_v(m): return g*m/c*(1 - exp(-c*t/m)) ml = 1.0 mu = 100.0 mr = root_bisection(f, ml, mu) print("v = ", calc_v(mr), " (should be close to ", v, ")") ml = 1.0 mu = 100.0 mr = root_regula_falsi(f, ml, mu) print("v = ", calc_v(mr), " (should be close to ", v, ")") ml = 1.0 mu = 100.0 mr = root_ridder(f, ml, mu) print("v = ", calc_v(mr), " (should be close to ", v, ")")
from root_bisection import * from root_regula_falsi import * from root_ridder import * Q = 20.0 g = 9.81 def f(y): B = 3 + y Ac = 3 * y + y**2 / 2 return 1 - Q * B / (g * Ac**3) yl = 0.5 yu = 2.5 yr = root_bisection(f, 0.5, 2.5) yr = root_regula_falsi(f, 0.5, 2.5) yr = root_ridder(f, 0.5, 2.5)