numerical-integration

Calculate the Area under a Curve

自古美人都是妖i 提交于 2019-12-17 02:27:04
问题 I would like to calculate the area under a curve to do integration without defining a function such as in integrate() . My data looks as this: Date Strike Volatility 2003-01-01 20 0.2 2003-01-01 30 0.3 2003-01-01 40 0.4 etc. I plotted plot(strike, volatility) to look at the volatility smile. Is there a way to integrate this plotted "curve"? 回答1: The AUC is approximated pretty easily by looking at a lot of trapezium figures, each time bound between x_i , x_{i+1} , y{i+1} and y_i . Using the

Numerical integration error

倾然丶 夕夏残阳落幕 提交于 2019-12-14 03:18:36
问题 When I run the following script below, I get the error Too many input arguments (I have figured out that this specifically happens when calculating intNH(5,2) in the second for loop). Basically what is happening is that fun2 for i=5 equals 0 (because the relevant G element is 0 ) and MATLAB cannot numerically integrate a function equal to zero (or that's how I've interpreted this). Does anyone have any pointers about how I can overcome this? I have tried some if statements saying that if the

Solving an ODE with ode45 after interpolating

依然范特西╮ 提交于 2019-12-14 03:18:29
问题 I am working with the following code and can't find what is wrong: xx = 0:1/50:1; v = 3.*exp(-xx)-0.4*xx; xq = xx; vq = @(xq) interp1(xx,v,xq); tspan = 0:1/50:1; x0 = 3; [~, y2] = ode45(@(t,x)vq(x), tspan, x0); I get that y2 = [3;NAN;NAN;NAN,.....] . Yet, when I plot both equations before calling ode45 , I get that they are equal, which is not a surprise. When I compute: f = @(t,r) 3.*exp(-r)-0.4*r; [~, y] = ode45(f,tspan,x0); it works fine. But I need to show that I can get the same results

2-fold (repeated) integral in R

左心房为你撑大大i 提交于 2019-12-13 16:39:24
问题 I want to compute a 2-fold repeated integral (not double integral) in R, for example, where In practice, both f(x) and g(x) are quite complicated, but to run an experiment, let's simplify g(x)=1 and f(x)=cos(x), in R I use integrate to compute: > phi = function(x){integrate(function(x){cos(x)},lower=x,upper=3)[["value"]]^2} > foldintegral = integrate(phi,lower=0,upper=3) And I got this error message: Error in integrate(phi, lower = 0, upper = 3) : evaluation of function gave a result of wrong

Numerical integration Legendre Polynomials MATLAB

只愿长相守 提交于 2019-12-13 04:16:09
问题 The Legendre polynomials are implemented in MATLAB as vectors, where you also get all the associated Legendre polynomials evaluated at a particular point x. Thus, I don't know how I can use these functions inside an integral. My question is: How can I evaluate the (NUMERICALLY CALCUALTED(!)) integral from -1 to 1 over the n-th Legendre polynomial in Matlab? EDIT: As I received an answer that is really not what I want: I want to use the implementation of the Legendre polynomials in MATLAB

Nested integral in Python

元气小坏坏 提交于 2019-12-13 03:50:59
问题 I have an M -dimensional integral where the outer limits over x_M are [0, y] , the next limits over x_{M-1} are [0, max(x_M, y-x_M)] , .... and the inner integral is over x_1 with limits [0, max(x_2, y-x_M-...-x_2)] . The function/integrand is (K!/(K-M)!)*(1/(x_1+1)^2)*....*(1/(x_{M-1}+1)^2)*(1/(x_M+1)^{K-M+2}) where K and M are integers such that K >= M >= 1 , and K!=K*(K-1)*...*2*1 is K factorial. How can I do this in Python using scipy.integrate.nquad? I had a similar problem here, but don

Integral of Intensity function in python

安稳与你 提交于 2019-12-12 11:35:43
问题 There is a function which determine the intensity of the Fraunhofer diffraction pattern of a circular aperture... (more information) Integral of the function in distance x= [-3.8317 , 3.8317] must be about 83.8% ( If assume that I0 is 100) and when you increase the distance to [-13.33 , 13.33] it should be about 95%. But when I use integral in python, the answer is wrong.. I don't know what's going wrong in my code :( from scipy.integrate import quad from scipy import special as sp I0=100.0

Matlab numerical integration

拜拜、爱过 提交于 2019-12-12 03:04:22
问题 I'm trying to integrate x using matlab, and that task is simple by using the following commands: syms x a=int(x) The problem is I'm not sure how to implement numerical integration. I want to integrate x using a set amount of intervals using different techniques. Can anyone help me with the syntax call for numerical integration? The MathWorks site isn't very helpful. I also do know there is a method called traps, but I'm looking for other methods within matlab, like Riemann sum approximation.

Implementing numerical integration using scipy.integrate.nquad

懵懂的女人 提交于 2019-12-11 17:48:25
问题 I have this 2-dimensional integral with dependent limits. The function can be defined in Python as def func(gamma, u2, u3): return (1-1/(1+gamma-u3-u2))*(1/(1+u2)**2)*(1/(1+u3)**2) where the limits of u3 is from 0 to gamma (a positive real number), and the limits of u2 is from 0 to gamma-u3 . How can I implement this using scipy.integrate.nquad? I tried to read the documentation, but it was not easy to follow, especially I am relatively new to Python. Extension: I would like to implement a

Double integral in RcppNumerical

烂漫一生 提交于 2019-12-11 17:45:24
问题 I have to calculate the double integral of the function: > DIntegral <- function(x,y){res <- pnorm(x,1,0.1) * dexp(y-2,1.2) return(res) } The upper limit for x and y are: 10 and Infinity respectively. The lower limit for x and y are: 1 and 2 respectively. How can I do this double integration in RcppNumerical ? For one dimensional integration my C++ file looks like: // [[Rcpp::depends(RcppEigen)]] // [[Rcpp::depends(RcppNumerical)]] #include <RcppNumerical.h> using namespace Numer; class PDF: