Solving implicit function and passing in three arguments - python

In the equation above I want to solve for f and pass in Re, D, and epsilon. Here is my code below:
import math
from scipy.optimize import fsolve
# Colebrook Turbulent Friction Factor Correlation
def colebrook(Re, D, eps):
return fsolve(-(1 / math.sqrt(f)) - 2 * math.log10(((eps / D) / 3.7) + (2.51 / Re * math.sqrt(f))), f)
Would I use fsolve() or solve()? I read up on fsolve() on Python's main site, however I don't understand some of the inputs it wants. Thank you in advance!
Also, I am using Spyder (Python 3.6)

The wikipedia page on "Darcy friction factor formulae" has a section on the Colebrook equation, and shows how f can be expressed in terms of the other parameters using the Lambert W function.
SciPy has an implementation of the Lambert W function, so you can use that to compute f without using a numerical solver:
import math
from scipy.special import lambertw
def colebrook(Re, D, eps):
"""
Solve the Colebrook equation for f, given Re, D and eps.
See
https://en.wikipedia.org/wiki/Darcy_friction_factor_formulae#Colebrook%E2%80%93White_equation
for more information.
"""
a = 2.51 / Re
b = eps / (3.7*D)
p = 1/math.sqrt(10)
lnp = math.log(p)
x = -lambertw(-lnp/a * math.pow(p, -b/a))/lnp - b/a
if x.imag != 0:
raise ValueError('x is complex')
f = 1/x.real**2
return f
For example,
In [84]: colebrook(125000, 0.315, 0.00015)
Out[84]: 0.019664137795383934
For comparison, the calculator at https://www.engineeringtoolbox.com/colebrook-equation-d_1031.html gives 0.0197.

Related

How can write this formula to Python code? [duplicate]

I am looking for a function in Numpy or Scipy (or any rigorous Python library) that will give me the cumulative normal distribution function in Python.
Here's an example:
>>> from scipy.stats import norm
>>> norm.cdf(1.96)
0.9750021048517795
>>> norm.cdf(-1.96)
0.024997895148220435
In other words, approximately 95% of the standard normal interval lies within two standard deviations, centered on a standard mean of zero.
If you need the inverse CDF:
>>> norm.ppf(norm.cdf(1.96))
array(1.9599999999999991)
It may be too late to answer the question but since Google still leads people here, I decide to write my solution here.
That is, since Python 2.7, the math library has integrated the error function math.erf(x)
The erf() function can be used to compute traditional statistical functions such as the cumulative standard normal distribution:
from math import *
def phi(x):
#'Cumulative distribution function for the standard normal distribution'
return (1.0 + erf(x / sqrt(2.0))) / 2.0
Ref:
https://docs.python.org/2/library/math.html
https://docs.python.org/3/library/math.html
How are the Error Function and Standard Normal distribution function related?
Starting Python 3.8, the standard library provides the NormalDist object as part of the statistics module.
It can be used to get the cumulative distribution function (cdf - probability that a random sample X will be less than or equal to x) for a given mean (mu) and standard deviation (sigma):
from statistics import NormalDist
NormalDist(mu=0, sigma=1).cdf(1.96)
# 0.9750021048517796
Which can be simplified for the standard normal distribution (mu = 0 and sigma = 1):
NormalDist().cdf(1.96)
# 0.9750021048517796
NormalDist().cdf(-1.96)
# 0.024997895148220428
Adapted from here http://mail.python.org/pipermail/python-list/2000-June/039873.html
from math import *
def erfcc(x):
"""Complementary error function."""
z = abs(x)
t = 1. / (1. + 0.5*z)
r = t * exp(-z*z-1.26551223+t*(1.00002368+t*(.37409196+
t*(.09678418+t*(-.18628806+t*(.27886807+
t*(-1.13520398+t*(1.48851587+t*(-.82215223+
t*.17087277)))))))))
if (x >= 0.):
return r
else:
return 2. - r
def ncdf(x):
return 1. - 0.5*erfcc(x/(2**0.5))
To build upon Unknown's example, the Python equivalent of the function normdist() implemented in a lot of libraries would be:
def normcdf(x, mu, sigma):
t = x-mu;
y = 0.5*erfcc(-t/(sigma*sqrt(2.0)));
if y>1.0:
y = 1.0;
return y
def normpdf(x, mu, sigma):
u = (x-mu)/abs(sigma)
y = (1/(sqrt(2*pi)*abs(sigma)))*exp(-u*u/2)
return y
def normdist(x, mu, sigma, f):
if f:
y = normcdf(x,mu,sigma)
else:
y = normpdf(x,mu,sigma)
return y
Alex's answer shows you a solution for standard normal distribution (mean = 0, standard deviation = 1). If you have normal distribution with mean and std (which is sqr(var)) and you want to calculate:
from scipy.stats import norm
# cdf(x < val)
print norm.cdf(val, m, s)
# cdf(x > val)
print 1 - norm.cdf(val, m, s)
# cdf(v1 < x < v2)
print norm.cdf(v2, m, s) - norm.cdf(v1, m, s)
Read more about cdf here and scipy implementation of normal distribution with many formulas here.
Taken from above:
from scipy.stats import norm
>>> norm.cdf(1.96)
0.9750021048517795
>>> norm.cdf(-1.96)
0.024997895148220435
For a two-tailed test:
Import numpy as np
z = 1.96
p_value = 2 * norm.cdf(-np.abs(z))
0.04999579029644087
Simple like this:
import math
def my_cdf(x):
return 0.5*(1+math.erf(x/math.sqrt(2)))
I found the formula in this page https://www.danielsoper.com/statcalc/formulas.aspx?id=55

Solving function containing gamma in Python

I'm quite new to programming with python.
I was wondering, if there is a smart way to solve a function, which includes a gamma function with a certain shape and scale.
I already created a function G(x), which is the cdf of a gamma function up to a variable x. Now I want to solve another function including G(x). It should look like: 0=x+2*G(x)-b. Where b is a constant.
My code looks like that:
b= 10
def G(x):
return gamma.cdf(x,a=4,scale=25)
f = solve(x+2*G(x)-b,x,dict=True)
How is it possible to get a real value for G(x) in my solve function?
Thanks in advance!
To get roots from a function there are several tools in the scipy module.
Here is a solution with the method fsolve()
from scipy.stats import gamma
from scipy.optimize import fsolve
def G(x):
return gamma.cdf(x,a=4,scale=25)
# we define the function to solve
def f(x,b):
return x+2*G(x)-b
b = 10
init = 0. # The starting estimate for the roots of f(x) = 0.
roots = fsolve(f,init,args=(b))
print roots
Gives output :
[9.99844838]
Given that G(10) is close to zero this solution seems likely
Sorry, I didn't take into account your dict=True option but I guess you are able to put the result in whatever structure you want without my help.
rom sympy import *
# from scipy.stats import gamma
# from sympy.stats import Arcsin, density, cdf
x, y, z, t, gamma, cdf = symbols('x y z t gamma cdf')
#sol = solve([x - 3, y - 1], dict=True)
from sympy.stats import Cauchy, density
from sympy import Symbol
x0 = Symbol("x0")
gamma = Symbol("gamma", positive=True)
z = Symbol("z")
X = Cauchy("x", x0, gamma)
density(X)(z)
print(density(X)(z))
sol = solve([x+2*density(X)(z)-10, y ], dict=True)
print(sol)
Or:
from scipy.stats import gamma
from sympy import solve, Poly, Eq, Function, exp
from sympy.abc import x, y, z, a, b
def G(x):
return gamma.cdf(x,a=4,scale=25)
b= 10
f = solve(x+2*G(x)-b,x,dict=True)
stats cdf gamma solve sympy

Using a ODE in another ODE with args make the code very very slow

I wrote the following code. It is a ODE which has a parameter in it as another ODE.
As we can see M(m0,z,b,c) in used in another ODE which itself is a ODE function. the code is very slow, Could anyone give me a suggestion how to improve it?
import numpy as np
from scipy.integrate import odeint
def model(m,z,c,b):
dmdt = ((c**2-m)/(1+z))*(6-9*(m/c**2)+3*b*(m+(m**2)))
return dmdt
def M(m0,z,c,b):
m = odeint(model,m0,[0,z], args= (c, b))
mm=m[-1,0]
return mm
def model1(H ,z,m0,c,b):
c = 0.6
b=0.035
dHdt = (H/(1+z))*(6-9*(M(m0,z,c,b)/c**2)+3*b*(M(m0,z,c,b)+(M(m0,z,c,b)**2)))
return dHdt
def model2(H0,z,m0,c,b):
H = odeint(model1,H0,[0,z], args=(m0,c,b))
HH=H[-1,0]
return HH
print(model2(70,1,0.75,0.69,0.035))
You can solve a coupled system as a coupled system.
def model(U,z,c,b):
M, H = U
dMdt = ((c**2-M)/(1+z))*(6-9*(M/c**2)+3*b*(M+M**2))
dHdt = (H /(1+z))*(6-9*(M/c**2)+3*b*(M+M**2))
return [dMdt, dHdt]
def solution(H0,z,m0,c,b):
U = odeint(model,[m0,H0],[0,z], args=(c,b))[-1]
M, H = U
return H
print(solution(70,1,0.75,0.69,0.035))
which rapidly returns 0.107569653042 while your code with the modifications
def model1(H, z, m0, c, b):
mm = M(m0,z,c,b)
dHdt = (H/(1+z))*(6-9*(mm/c**2)+3*b*(mm+(mm)**2)))
return dHdt
returns the similar 0.107569746892 somewhat slower. These 6 digits of coincidence are consistent with the default error tolerances of 1e-6.
To get results with higher accuracy set the control parameters for the error tolerances atol, rtol.
For further reduction in operations do
def model(U,z,c,b):
M, H = U
factor = (6-9*M/c**2+3*b*(M+M**2))/(1+z)
return [(c**2-M)*factor, H*factor]
If your task is really massive, use a compiled programming language for fast mass number crunching.

Manipulating functions in python

I want to be able to use one function in another function, in this case the function nu in the expression for integrand. At the moment this is my code:
from trapezium import trap
import scipy as sp
# mass_enc returns the mass enclosed within a radius R
# nu is the density function
def mass_enc(q, R):
integrand = lambda r: 4 * sp.pi * r**2 * q(r)
return trap(integrand, 0, R, 100)
def nu(a):
return a
print(mass_enc(nu, 10))
However this seems messy - is there a better way to do it?

Utility function not evaluating correctly numpy python

The answer should be 117.4, I'm getting 9982.3... Not sure what the problem is but here's my code:
def util(c,p,alpha):
mu = 0
for i in range(0,len(c)):
m = p[i]*(c[i]**(1-alpha))
mu += m
return mu**(1/(1-alpha))
omega = 0.495
c = np.array([100,200,1000])
p = np.array([omega, omega, 1-2*omega])
alpha = 5
EDIT: I'm not sure if there is an error with my math or with the function I wrote, I am asking if my math fits the code that I wrote.
I'm solving this equation for mu: U(mu) = E[U(c)] with payoffs c, and probability distribution p, as above. U(c) has the form c^(1-alpha)/(1-alpha).
U(mu) = mu^(1-alpha)/(1-alpha) = E[U(c)] = (omega*c1^(1-alpha)+omega*c2^(1-alpha)+(1-2*omega)*c3^(1-alpha))/(1-alpha)
=> mu = (omega*c1^(1-alpha)+omega*c2^(1-alpha)+(1-2*omega)*c3^(1-alpha))^(1/(1-alpha))
Your main problem is that Python is doing integer division. Python 2 does integer division with /, unless you do from __future__ import division (see PEP 238). So you need to change at least one of your operands to a floating-point value. You can do this by setting alpha = 5.0 instead of alpha = 5. Or you can write 1.0 - alpha instead of 1 - alpha.
Also, you can make your code a bit more compact by using numpy's vectorized operations. Your util function can be shortened to:
def util(c, p, alpha):
mu = np.sum(p * (c ** (1.0 - alpha)))
return mu ** (1 / (1.0 - alpha))

Categories