18.1.2: 18.1.2 The integral() function
- Page ID
- 92083
The integral() function is preferred over the traz() function.
The function integral() determines a good set of x-values and uses a more sophisticated algorithm than trapz()
You need to specify a "handle" to a Matlab function, like is done for the fzero() function. The function and its handle can be specified in 2 methods:
Method A: Use a function .m file, such as this:
function y = y_cubic(x)
y = x.^3 -4*x + 4
end
In the main script, the integral function refers to this file like this:
x1 = -2.0;
x2 = 1.5;
intg1 = integral(@y_cubic, x1, x2)
% =14.7656, which = the analytic answer
Method B: Use an "in-line" function, like this:
y_handle = @(x) x.^3 -4*x + 4
% This is also known as an "anonymous function"
intg1 = integral(y_handle, x1, x2)
% 14.7656, which = the analytic answer
You may use either a function .m file or an anonymous function definition within their script.
My opinion is that a function .m file is the clearer method.