numerical-integration

Conversion of symbolic expression to numeric one for use in quad - use lambdify?

拟墨画扇 提交于 2019-12-11 15:29:45
问题 I want to convert an expression containing symbolic variables to a numeric one so that the expression may be subsequently used in an integration method 'quad'. import numpy import math as m import scipy import sympy #define constants gammaee = 5.55e-6 MJpsi = 3.096916 alphaem = 1/137 lambdasq = 0.09 Ca = 3 qOsq = 2 def qbarsq(qsq): return (qsq+MJpsi**2)/4 def xx(qbarsq, w): return 4*qbarsq/(4*qbarsq-MJpsi**2+w**2) from sympy import * x,NN,a,b,ktsq,qbarsq,w = symbols('x NN a b ktsq qbarsq w')

Kernel CDF estimate: integral drops to zero

北城以北 提交于 2019-12-11 07:35:29
问题 I want to integrate a kernel density estimate in order to get a kernel estimate of the cdf. This is my code: set.seed(1) z <- rnorm(250) pdf <- approxfun(density(z, bw = "SJ"), yleft = 0, yright = 0) cdf <- function(b) { integrate(pdf, -Inf, b)$value } x <- seq(-20, 20, 0.1) plot(x, sapply(x, cdf), type = "l", xlab = "x", ylab = "density", ylim= c(0, 1)) Which produces the following plot As you can see, the cdf drops to zero at ~18, which clearly should not happen. Why does this happen and

Can scipy.integrate.fixed_quad compute integral with functional boundaries?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 05:57:46
问题 I would like to numerically integrate function over a triangle similarly as import scipy.integrate as integrate inside = lambda x: integrate.quad(lambda x,y: 1, 0, x, args=(x))[0] outside = integrate.quad(inside, 0, 1)[0] print(outside) 0.5 but using scipy.integrate.fixed_quad function (which has integration order n as parameter). However, when I write inside = lambda x: integrate.fixed_quad(lambda x,y: 1, 0, x, args=(x), n=5) print(inside(5)) Traceback (most recent call last): File "", line

Python integration using both scipy and sympy

我的未来我决定 提交于 2019-12-11 05:48:59
问题 I want to have a custom python function which: Takes a mathematical expression f(x) , bounds of the integral x1,x2 and desired tolerance tol Uses sympy.integrate(y, x) to check if it has analytical/symbolic solution, if it does then returns the result using sympy.integrate(y, (x,x1,x2).evalf()) If it doesn't have an analytical solution out of the sympy then it uses scipy.integrate.quad or other numerical functions to calculate the integral. The reason is that with this method it will be

How to use Rcpp to do numerical integration in C++ within R

≡放荡痞女 提交于 2019-12-11 03:56:03
问题 I was wondering how Rcpp could be used to perform numerical integration by calling C++ in R. My current setup takes a really long time and is highly error prone. I think I need something better than default R numerical integration package. Would doing numerical integration in C++ within R solve these problems? funk <- function(x,b) { 10^b * exp(-x/10) } lambda <- function(y,k) { exp(-k*y) } funk1 <- function(y,x,xb,b,k) { funk(x-xb-y,b) *exp(- integrate(lambda, lower=0, upper = y, k=k)$value)

Wrong result when doing simple Monte Carlo integration in R

萝らか妹 提交于 2019-12-11 03:54:07
问题 I'm giving part of a presentation on numerical integration. While the talk itself will go into better forms of numerical integration (mainly importance sampling and stratified sampling), I'm mentioning during part of my section Monte Carlo integration sampling from the uniform distribution. I've found that: mean(sin(runif(1e8, 0, pi))) is giving an answer of 0.636597 , rather than 1 that is expected. This answer seems pretty consistent with increasing sample size, and I'm unsure why there's

Python: Approximating ln(x) using Taylor Series

我的未来我决定 提交于 2019-12-11 03:35:13
问题 I'm trying to build an approximation for ln(1.9) within ten digits of accuracy (so .641853861). I'm using a simple function I've built from ln[(1 + x)/(1 - x)] Here is my code so far: # function for ln[(1 + x)/(1 - x)] def taylor_two(r, n): x = 0.9 / 2.9 i = 1 taySum = 0 while i <= n: taySum += (pow(x,i))/(i) i += 2 return 2 * taySum print taylor_two(x, 12) print taylor_two(x, 17) What I need to do now is reformat this so that it tells me the number of terms needed to approximate ln(1.9) to

Scipy ode integrate to unknown limit of t, based on the size of solution

自古美人都是妖i 提交于 2019-12-11 00:33:13
问题 I am modeling charged particles moving through an electromagnetic field and am using scipy ode. The code here is simplified, obviously, but works as an example. The problem I have is that I want to end the integration after a limit on r, not on t. So, integrate dx/dt up to the point where norm(x) > r. I don't want to just change the function to integrate over r, however, because the position is a function of t. Can I do a definite integral over an unrelated variable or something? import numpy

How to divide time interval into parts of varying length?

五迷三道 提交于 2019-12-10 10:07:49
问题 I have a time interval from 0 to t . I want to divide this interval into a cumulative sequence in a cycle of 2.25, 2.25 and 1.5, in the following manner: input: start = 0 stop = 19 output: sequence = [0, 2.25, 4.5, 6, 8.25, 10.5, 12, 14.25, 16.5, 18, 19] How can I do this in Python? The idea is to divide a time period into cycles of 6 hours, each cycle consisting of three sequential operations that last 2.25 h, 2.25 h and 1.5 h respectively. Or is there an alternative to using 'milestones'

Looking for Python package for numerical integration over a tessellated domain

南笙酒味 提交于 2019-12-10 03:52:31
问题 I was wondering if anyone knew of a numpy/scipy based python package to numerically integrate a complicated numerical function over a tessellated domain (in my specific case, a 2D domain bounded by a voronoi cell)? In the past I used a couple of packages off of the matlab file exchange, but would like to stay within my current python workflow if possible. The matlab routines were http://www.mathworks.com/matlabcentral/fileexchange/9435-n-dimensional-simplex-quadrature for the quadrature and