numerical-integration


python: integrating a piecewise function

℡╲_俬逩灬. 提交于 2020-01-24 07:09:47
问题 I want to integrate a piecewise a defined function that is multiplied by the Legendre polynomials. Unfortunately, I can't find how to use the nth Legendre polynomial of x in the documentation. I want to integrate each Legendre polynomial of x when n = 1,..., 50 so I have set n = np.arange(1, 51, 1) . import numpy as np import pylab from scipy import integrate n = np.arange(1, 51, 1) def f(x): if 0 <= x <= 1: return 1 if -1 <= x <= 0: return -1 I suppose I need to define another function let's

python: integrating a piecewise function

ぃ、小莉子 提交于 2020-01-24 07:09:06
问题 I want to integrate a piecewise a defined function that is multiplied by the Legendre polynomials. Unfortunately, I can't find how to use the nth Legendre polynomial of x in the documentation. I want to integrate each Legendre polynomial of x when n = 1,..., 50 so I have set n = np.arange(1, 51, 1) . import numpy as np import pylab from scipy import integrate n = np.arange(1, 51, 1) def f(x): if 0 <= x <= 1: return 1 if -1 <= x <= 0: return -1 I suppose I need to define another function let's

How can you perform this improper integral as Mathematica does?

此生再无相见时 提交于 2020-01-21 09:01:49
问题 Take this Mathematica code: f[x_] := Exp[-x]; c = 0.9; g[x_] := c*x^(c - 1)*Exp[-x^c]; SetPrecision[Integrate[f[x]*Log[f[x]/g[x]], {x, 0.001, \[Infinity]}],20] Mathematica computes this without problem and gives the answer 0.010089328699390866240 . I would like to be able to perform similar integrals but I don't have a copy of Mathematica. Just naively implementing it in scipy, for example, using a standard quadrature library fails sadly because f(x) and g(x) get arbitrarily close to 0. Here

R integrate: returns wrong solution (is using wrong quadrature points?)

断了今生、忘了曾经 提交于 2020-01-17 06:26:12
问题 I have a function in R which I am trying to integrate, but for some (extreme) values of the function parameters, integrate returns the incorrect solution. I believe the issue may be that integrate selects improper quadrature points for some of these extreme values, but first I will provide demonstrate the issue. The function I wish to integrate is the following. integrandFunc_F <- function(x, func_u, func_u_lowerBar, func_u_upperBar, func_mean_v, func_sigma_v, func_sigma_epsilon, func_sigma_y

Quadrature to approximate a transformed beta distribution in R

杀马特。学长 韩版系。学妹 提交于 2020-01-07 05:11:09
问题 I am using R to run a simulation in which I use a likelihood ratio test to compare two nested item response models. One version of the LRT uses the joint likelihood function L(θ,ρ) and the other uses the marginal likelihood function L(ρ). I want to integrate L(θ,ρ) over f(θ) to obtain the marginal likelihood L(ρ). I have two conditions: in one, f(θ) is standard normal (μ=0,σ=1), and my understanding is that I can just pick a number of abscissa points, say 20 or 30, and use Gauss-Hermite

How do I change the value of a parameter inside Matlab's ode45 solver

99封情书 提交于 2020-01-07 03:59:11
问题 I'm trying to solve a differential equation using ode45 in Matlab. I'm running two scripts: function xdot=linearproblem(t,x) global kappa mass F xdot(1)=-(kappa/mass)*x(2)+(F/mass)*(cos(omega1*t)); xdot(2)=x(1); xdot=xdot'; end Then in the second script, I have close all clear clc global kappa mass F kappa=4; F=2; mass=0.5; options=odeset('omega1',[1.4 1.5 1.6]); [t x]=ode45(@linearproblem,0:0.005:100,[0 0],options); a=x(8000,2); omega1=omega1' a=a' I'm trying to solve the equation using

Numerical Integration, using the trapezium rule in C

会有一股神秘感。 提交于 2020-01-06 06:56:10
问题 I am using the trapezium rule to calculate the integral of a function between 0 and infinity. I can calculate the value of the integral for a given value of N, and now I am trying to loop N from two to a given value but it will not work. It keeps calculating the value of the integral for when N is 2 and repeating instead of the new value of N. The problem is in the for loop in main() I think. #include <stdio.h> #include <math.h> #include <stdlib.h> #include <float.h> double f(double x) {

How does scipy.integrate.quad know when to stop?

元气小坏坏 提交于 2020-01-04 19:07:31
问题 I have a piece of code that I am using scipy.integrate.quad. The limits of integration are minus infinity to infinity. It runs OK, but I would like it faster. The nature of the problem is that the function being integrated is the product of three functions: (1) one that is narrow (between zero and (2) one that is wide (between, say, 200,000 and 500,000), and (3) one that falls off as 1/abs(x). I only need accuracy to .1%, if that. I could do a lot of work and actually determine integration

How does scipy.integrate.quad know when to stop?

别等时光非礼了梦想. 提交于 2020-01-04 19:06:02
问题 I have a piece of code that I am using scipy.integrate.quad. The limits of integration are minus infinity to infinity. It runs OK, but I would like it faster. The nature of the problem is that the function being integrated is the product of three functions: (1) one that is narrow (between zero and (2) one that is wide (between, say, 200,000 and 500,000), and (3) one that falls off as 1/abs(x). I only need accuracy to .1%, if that. I could do a lot of work and actually determine integration

Numerical Triple Integration in R

牧云@^-^@ 提交于 2019-12-31 02:41:29
问题 Is it possible to do triple integration in R without using the cubature package? based on the answer in this post InnerFunc = function(x) { x + 0.805 } InnerIntegral = function(y) { sapply(y, function(z) { integrate(InnerFunc, 15, z)$value }) } integrate(InnerIntegral , 15, 50) 16826.4 with absolute error < 1.9e-10 For example, to code this triple integral: I tried InnerMostFunc = function(v) { v + y^2 } InnerMostIntegral = function(w) { sapply(w, function(x) { integrate(InnerMostFunc, 1, 2)

工具导航Map