Reputation: 43
I'm a beginning programmer who's currently working on translating a Julia script to Python. It concerns the following piece of Julia code:
function p(temp, irr)
p_max = P_1 *
exp(T_AP / T_P1 - T_AP / (temp + 273.15)) /
(1 + exp(T_APL / (temp + 273.15) - T_APL / T_PL) + exp(T_APH / T_PH - T_APH / (temp + 273.15)))
β_func(x) = p_max -
(α * I_sat / log(1 + α / x)) * (α / (α + x)) * (x / (α + x))^(x / α)
β = find_zero(β_func, (0, 0.1), Bisection())
p_s = α * I_sat / log(1 + α / β)
return p_s * (1 - exp(-α * irr / p_s)) * exp(-β * irr / p_s)
end
As far as I understand, the code finds an x value for which the difference between p_max and β_func is zero, and uses this value in the equation for p_s.
I've been trying to translate this code to Python for a while now, without any success. There are several examples of applying Newton's method in Python (e.g., https://personal.math.ubc.ca/~pwalls/math-python/roots-optimization/newton/), but these examples use 1 equation instead of 2.
Any help or insights are very welcome. Thanks!
Upvotes: 2
Views: 463
Reputation: 648
If the problem is simply the syntax here you are : (I imported a scipy function, so you have to install scipy module to run this) And btw there's a lot of unknown variables.
import math
from scipy import optimize
def p(temp, irr):
p_max = P_1 * math.exp(T_AP / T_P1 - T_AP / (temp + 273.15)) / (1 + math.exp(T_APL / (temp + 273.15) - T_APL / T_PL) + math.exp(T_APH / T_PH - T_APH / (temp + 273.15)))
B_func = lambda x: p_max - (a * I_sat / math.log(1 + a / x)) * (a / (a + x)) * (x / (a + x))^(x / a)
B = optimize.newton(B_func, 0)
p_s = a * I_sat / math.log(1 + a / B)
return p_s * (1 - math.exp(-a * irr / p_s)) * math.exp(-B * irr / p_s)
Upvotes: 1