help integral integral Numerically evaluate integral. Q = integral(FUN,A,B) approximates the integral of function FUN from A to B using global adaptive quadrature and default error tolerances. FUN must be a function handle. A and B can be -Inf or Inf. If both are finite, they can be complex. If at least one is complex, integral approximates the path integral from A to B over a straight line path. For scalar-valued problems the function Y = FUN(X) must accept a vector argument X and return a vector result Y, the integrand function evaluated at each element of X. For array-valued problems (see the 'ArrayValued' option below) FUN must accept a scalar and return an array of values. Q = integral(FUN,A,B,PARAM1,VAL1,PARAM2,VAL2,...) performs the integration with specified values of optional parameters. The available parameters are 'AbsTol', absolute error tolerance 'RelTol', relative error tolerance integral attempts to satisfy |Q - I| <= max(AbsTol,RelTol*|Q|), where I denotes the exact value of the integral. Usually RelTol determines the accuracy of the integration. However, if |Q| is sufficiently small, AbsTol determines the accuracy of the integration, instead. The default value of AbsTol is 1.e-10, and the default value of RelTol is 1.e-6. Single precision integrations may require larger tolerances. 'ArrayValued', FUN is an array-valued function when the input is scalar When 'ArrayValued' is true, FUN is only called with scalar X, and if FUN returns an array, integral computes a corresponding array of outputs Q. The default value is false. 'Waypoints', vector of integration waypoints If FUN(X) has discontinuities in the interval of integration, the locations should be supplied as a 'Waypoints' vector. Waypoints should not be used for singularities in FUN(X). Instead, split the interval and add the results from separate integrations with singularities at the endpoints. If A, B, or any entry of the waypoints vector is complex, the integration is performed over a sequence of straight line paths in the complex plane, from A to the first waypoint, from the first waypoint to the second, and so forth, and finally from the last waypoint to B. Examples: % Integrate f(x) = exp(-x^2)*log(x)^2 from 0 to infinity: f = @(x) exp(-x.^2).*log(x).^2 Q = integral(f,0,Inf) % To use a parameter in the integrand: f = @(x,c) 1./(x.^3-2*x-c) Q = integral(@(x)f(x,5),0,2) % Specify tolerances: Q = integral(@(x)log(x),0,1,'AbsTol',1e-6,'RelTol',1e-3) % Integrate f(z) = 1/(2z-1) in the complex plane over the % triangular path from 0 to 1+1i to 1-1i to 0: Q = integral(@(z)1./(2*z-1),0,0,'Waypoints',[1+1i,1-1i]) % Integrate the vector-valued function sin((1:5)*x) from 0 to 1: Q = integral(@(x)sin((1:5)*x),0,1,'ArrayValued',true) Class support for inputs A, B, and the output of FUN: float: double, single See also integral2, integral3, function_handle Reference page for integral help quad quad Numerically evaluate integral, adaptive Simpson quadrature. Q = quad(FUN,A,B) tries to approximate the integral of scalar-valued function FUN from A to B to within an error of 1.e-6 using recursive adaptive Simpson quadrature. FUN is a function handle. The function Y=FUN(X) should accept a vector argument X and return a vector result Y, the integrand evaluated at each element of X. Q = quad(FUN,A,B,TOL) uses an absolute error tolerance of TOL instead of the default, which is 1.e-6. Larger values of TOL result in fewer function evaluations and faster computation, but less accurate results. The quad function in MATLAB 5.3 used a less reliable algorithm and a default tolerance of 1.e-3. Q = quad(FUN,A,B,TOL,TRACE) with non-zero TRACE shows the values of [fcnt a b-a Q] during the recursion. Use [] as a placeholder to obtain the default value of TOL. [Q,FCNT] = quad(...) returns the number of function evaluations. Use array operators .*, ./ and .^ in the definition of FUN so that it can be evaluated with a vector argument. quad will be removed in a future release. Use INTEGRAL instead. Example: Q = quad(@myfun,0,2); where the file myfun.m defines the function: %-------------------% function y = myfun(x) y = 1./(x.^3-2*x-5); %-------------------% or, use a parameter for the constant: Q = quad(@(x)myfun2(x,5),0,2); where the file myfun2.m defines the function: %----------------------% function y = myfun2(x,c) y = 1./(x.^3-2*x-c); %----------------------% Class support for inputs A, B, and the output of FUN: float: double, single See also integral, integral2, integral3, quadgk, quad2d, trapz, function_handle. Reference page for quad help integral integral Numerically evaluate integral. Q = integral(FUN,A,B) approximates the integral of function FUN from A to B using global adaptive quadrature and default error tolerances. FUN must be a function handle. A and B can be -Inf or Inf. If both are finite, they can be complex. If at least one is complex, integral approximates the path integral from A to B over a straight line path. For scalar-valued problems the function Y = FUN(X) must accept a vector argument X and return a vector result Y, the integrand function evaluated at each element of X. For array-valued problems (see the 'ArrayValued' option below) FUN must accept a scalar and return an array of values. Q = integral(FUN,A,B,PARAM1,VAL1,PARAM2,VAL2,...) performs the integration with specified values of optional parameters. The available parameters are 'AbsTol', absolute error tolerance 'RelTol', relative error tolerance integral attempts to satisfy |Q - I| <= max(AbsTol,RelTol*|Q|), where I denotes the exact value of the integral. Usually RelTol determines the accuracy of the integration. However, if |Q| is sufficiently small, AbsTol determines the accuracy of the integration, instead. The default value of AbsTol is 1.e-10, and the default value of RelTol is 1.e-6. Single precision integrations may require larger tolerances. 'ArrayValued', FUN is an array-valued function when the input is scalar When 'ArrayValued' is true, FUN is only called with scalar X, and if FUN returns an array, integral computes a corresponding array of outputs Q. The default value is false. 'Waypoints', vector of integration waypoints If FUN(X) has discontinuities in the interval of integration, the locations should be supplied as a 'Waypoints' vector. Waypoints should not be used for singularities in FUN(X). Instead, split the interval and add the results from separate integrations with singularities at the endpoints. If A, B, or any entry of the waypoints vector is complex, the integration is performed over a sequence of straight line paths in the complex plane, from A to the first waypoint, from the first waypoint to the second, and so forth, and finally from the last waypoint to B. Examples: % Integrate f(x) = exp(-x^2)*log(x)^2 from 0 to infinity: f = @(x) exp(-x.^2).*log(x).^2 Q = integral(f,0,Inf) % To use a parameter in the integrand: f = @(x,c) 1./(x.^3-2*x-c) Q = integral(@(x)f(x,5),0,2) % Specify tolerances: Q = integral(@(x)log(x),0,1,'AbsTol',1e-6,'RelTol',1e-3) % Integrate f(z) = 1/(2z-1) in the complex plane over the % triangular path from 0 to 1+1i to 1-1i to 0: Q = integral(@(z)1./(2*z-1),0,0,'Waypoints',[1+1i,1-1i]) % Integrate the vector-valued function sin((1:5)*x) from 0 to 1: Q = integral(@(x)sin((1:5)*x),0,1,'ArrayValued',true) Class support for inputs A, B, and the output of FUN: float: double, single See also integral2, integral3, function_handle Reference page for integral doc integral doc integral quad(@(x) x.^2.*exp(-x.^2),-Inf,Inf) [Warning: Infinite or Not-a-Number function value encountered.] [> In quad (line 100)] ans = NaN quad(@(x) x.^2.*exp(-x.^2),-10,10) ans = 0.8862 format long e quad(@(x) x.^2.*exp(-x.^2),-10,10) ans = 8.862275289520790e-01 sqrt(pi)/2 ans = 8.862269254527579e-01 quad(@(x) x.^2.*exp(-x.^2),-20,20) ans = 2.341224229802663e-07 quad(@(x) x.^2.*exp(-x.^2),-30,30) ans = 3.697500737084623e-18 quad(@(x) x.^2.*exp(-x.^2),-30,31) ans = 8.862273628583137e-01 2*quad(@(x) x.^2.*exp(-x.^2),0,30) ans = 8.862278125547811e-01 2*quad(@(x) x.^2.*exp(-x.^2),0,50) ans = 8.862118289009481e-01 2*quad(@(x) x.^2.*exp(-x.^2),0,100) ans = 8.505568796545164e-18 2*integral(@(x) x.^2.*exp(-x.^2),0,100) ans = 8.862269254527630e-01 fun = @(x) x+1; quad(fun,0,1) ans = 1.500000000000000e+00 rettangoli(fun,10,0,1,0) ans = 1.450000000000000e+00 rettangoli(fun,10,0,1,1/10) ans = 1.550000000000000e+00 rettangoli(fun,10,0,1,1/20) ans = 1.500000000000000e+00 rettangoli(fun,2,0,1,1/20) ans = 1.300000000000000e+00 rettangoli(fun,2,0,1,1/10) ans = 1.350000000000000e+00 rettangoli(fun,2,0,1,1/4) ans = 1.500000000000000e+00 nrange=(10:20) nrange = 10 11 12 13 14 15 16 17 18 19 20 errori=3*(1./nrange) errori = Columns 1 through 2 3.000000000000000e-01 2.727272727272727e-01 Columns 3 through 4 2.500000000000000e-01 2.307692307692308e-01 Columns 5 through 6 2.142857142857143e-01 2.000000000000000e-01 Columns 7 through 8 1.875000000000000e-01 1.764705882352941e-01 Columns 9 through 10 1.666666666666667e-01 1.578947368421053e-01 Column 11 1.500000000000000e-01 format errori=3*(1./nrange) errori = Columns 1 through 7 0.3000 0.2727 0.2500 0.2308 0.2143 0.2000 0.1875 Columns 8 through 11 0.1765 0.1667 0.1579 0.1500 loglog(nrange,errori,'*') errori2=5*(1./nrange).^2 errori2 = Columns 1 through 7 0.0500 0.0413 0.0347 0.0296 0.0255 0.0222 0.0195 Columns 8 through 11 0.0173 0.0154 0.0139 0.0125 loglog(nrange,errori,'*',nrange,errori2,'o') quad(@(x) x.^2,0,2) ans = 2.6667 8/3 ans = 2.6667 test_rettangoli test_rettangoli test_rettangoli diary off