integral

Using tryCatch in the integral and mapply

橙三吉。 提交于 2019-12-12 03:53:44
问题 I have the date (test): test<-structure(list(X = c(630L, 631L, 977L, 978L), si = c(1.063166618, 1.063166618, 1.063166618, 1.063166618), ho = c(-0.071587466, -0.071587466, -0.071587466, -0.071587466), bd = c(0.998351574, 0.999361098, 0.999978304, 0.944579005), i1 = c(0.997434318, 0.997434318, 0.997434318, 0.997434318), x1b1m = c(2.938615084, 3.220915079, 4.088527679, 1.818297207), x1b1e = c(3.561241746, 3.843541741, 4.711154342, 1.594420075), x1b0m = c(2.714737952, 2.997037947, 3.864650548, 1

Triple integral in R (how to specifying the domain)

痴心易碎 提交于 2019-12-11 12:38:32
问题 I would like to compute the triple integral of a function of three variables f(x,y,z) in R. I'm using the package cubature and the function adaptIntegrate() . The integrand is equal to 1 only in a certain domain ( x<y<z , 0 otherwise) which I don't know how to specify. I'm trying 2 different implementations of the function, but none of them work: #First implementation fxyz <- function(w) { x <- w[1] y <- w[2] z <- w[3] x*y*z*(x < y)&(y < z) } #Second implementation fxyz <- function(w) { x <-

Integration with Riemann Sum Python

醉酒当歌 提交于 2019-12-11 09:29:59
问题 I have been trying to solve integration with riemann sum. My function has 3 arguments a,b,d so a is lower limit b is higher limit and d is the part where a +(n-1)*d < b . This is my code so far but. My output is 28.652667999999572 what I should get is 28.666650000000388 . Also if the input b is lower than a it has to calculate but I have solved that problem already. def integral(a, b, d): if a > b: a,b = b,a delta_x = float((b-a)/1000) j = abs((b-a)/delta_x) i = int(j) n = s = 0 x = a while n

How to check if a SymPy expression has analytical integral

谁都会走 提交于 2019-12-11 09:08:46
问题 I want to solve my other question here so I need sympy to return an error whenever there is no analytical/symbolic solution for and integral. For example if I try : from sympy import * init_printing(use_unicode=False, wrap_line=False, no_global=True) x = Symbol('x') integrate(1/cos(x**2), x) It just [pretty] prints the integral itself without solving and/or giving an error about not being able to solve it! P.S. I have also asked this question here on Reddit. 回答1: A "symbolic" solution always

Solving a double integral equation for a third variable

Deadly 提交于 2019-12-11 07:19:23
问题 I've been trying to solve a double integral equation for a third variable in matlab. An example: At first, I tried to solve it symbolically (when k=1) as below: syms x y h F = @(x,y,h) 2*x*y+4*h; f = @(x) x/2; solve(int(int(F(x,y,h)*f(x),x,0,3)*f(y),y,0,1)-3, h) The code gives the right answer, i.e. h=2/3 when k=1. For my real problem, however, the functions F and f are so much more complex. And when I applied the same code above with the complex F and f, the matlab does not solve it in an

Curve Fitting with Known Integrals Python

眉间皱痕 提交于 2019-12-11 04:54:39
问题 I have some data that are the integrals of an unknown curve within bins. For your interest, the data is ocean wave energy and the bins are for directions, e.g. 0-15 degrees. If possible, I would like to fit a curve on to the data that conserves the integrals within the bins. I've tried sketching it on a notepad with a pencil and it seems like it could be possible. Does anyone know of any curve-fitting tool in Python to do this, for example in the scipy interpolation sub-package? Thanks in

Integration and false convergence of optimization in R

落花浮王杯 提交于 2019-12-11 04:36:31
问题 I am trying to find MLEs of three positive parameters a , mu and theta , and then the value of a function, saying f1 . f1<-function(para) { a<-para[1] mu<-para[2] the<-para[3] return(a*mu/the) } Step 1 Suppose we have the following (negative) log likelihood function. where x_ij and t_ij are known loglik<-function(para, data) { n.quad<-64 a<-para[1] mu<-para[2] the<-para[3] k<-length(table(data$group)) rule<-glaguerre.quadrature.rules(n.quad, alpha = 0)[[n.quad]] int.ing.gl<-function(y, x, t)

Speeding up a double loop over integrals with changing parameter function values in matlab [duplicate]

不问归期 提交于 2019-12-11 04:33:19
问题 This question already has an answer here : Nested integral within integral2 in matlab (1 answer) Closed 5 years ago . I asked a previous question about having an integral within a loop. I received an answer but it is unbearably slow. I was wondering if anyone could make it faster, I imagine removing loops and removing function definition from the loop. l=3; t=linspace(0,1,365); fun3= @(v) integral(@(v)exp(.071*v),0,v,'ArrayValued',true); for i=2:length(t) for j=i:length(t) xx=t(i); yy=t(j);

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

Sympy fails to integrate the product of a piecewise continuous function and a complex function across the discontinuity

别说谁变了你拦得住时间么 提交于 2019-12-11 01:58:51
问题 If I do: from sympy import * x, L = symbols('x L', real=True) f = Piecewise((1, x<=0), (-1, x<1), (0, True)) g = exp(-x * 1j) integrate(f * g, (x, 0, L)) I get: Piecewise((1.0*I*exp(-1.0*I*L) - 1.0*I, L <= 0), (-1.0*I*exp(-1.0*I*L) + 1.0*I, L < 1), (-1.0*I*exp(-1.0*I) + 1.0*I, True)) But if I change the last line to: integrate(f*g, (x, L/2, L)) I get: Integral(Piecewise((exp(-1.0*I*x), x <= 0), (-exp(-1.0*I*x), x < 1), (0, True)), (x, L/2, L)) Any insight would be appreciated! 回答1: I guess it