numerical-integration

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)

Python numerical integration with Simpson's rule

泄露秘密 提交于 2019-12-31 02:01:13
问题 I have started to work through this book (Computational Physics Exercise 5.4) and its exercises and I got stuck with the following question: Write a Python function J(m,x) that calculates the value of Jm(x) using Simpson’s rule with N = 1000 points. Use your function in a program to make a plot, on a single graph, of the Bessel functions J0, J1, and J2 as a function of x from x = 0 to x = 20. I have created the following code to evaluate the first part of the question but not sure if even

How do I do numerical integration of a vector in MATLAB?

旧时模样 提交于 2019-12-29 06:25:09
问题 I have a vector of 358 numbers. I'd like to make a numerical integration of this vector, but I don't know the function of this one. I found that we can use trapz or quad, but i don't really understand how to integrate without the function. 回答1: If you know the horizontal spacing of your vector, you can use trapz in order to integrate it without the function. For example, to integrate y=sin(x) from 0 to pi with 358 sections, x=0:pi/357:pi; y=sin(x); area=trapz(x,y); If you just use trapz(y) ,