lettura simple

Matlab's quad() Function

Let me tell you about the quad() function in Matlab. This function is quite useful when it comes to solving definite integrals of functions.

quad(f,a,b)

  • The first parameter (f) is the integrand function written as an anonymous function. It's the function to integrate.
  • The second parameter (a) is the lower bound of the integration interval.
  • The third parameter (b) is the upper bound of the integration interval.

The quad() function then calculates the definite integral, which is the area between the x-axis and the graph of the integrand function f(x) in the integration interval (a,b).

$$ \int_a^b f(x) \ dx $$

Now, it's worth noting that in Matlab, there are two functions you can use to calculate the definite integral of a function: quad() and int(). However, these two functions are based on different numerical methods. The quad() function uses the adaptive quadrature method based on the Gauss-Kronrod algorithm, while the int() function uses other numerical methods.

Let me show you an example of how to use the quad() function in Matlab.

Suppose we want to calculate the definite integral of the function 2x in the interval [1, 2].

$$ \int_1^2 2x \ dx $$

To do this, we first define the function as an anonymous function in Matlab.

>> f = @(x) 2*x

In this case, we don't need to define the dependent variable x as a symbol.

Then, we call the quad() function and pass in the function f, as well as the lower and upper bounds of the integration interval.

>> quad(f,1,2)

The quad() function then calculates the definite integral of the function 2x in the interval [1, 2] and returns the value 3, which is the area between the x-axis and the graph of the function in the integration interval.

ans=3

This way, we have solved the definite integral.

$$ \int_1^2 2x \ dx = 3 $$

Verification. Just to double-check, we can verify that the result we obtained using the quad() function is correct by calculating the primitive function of 2x, which is x2, and then evaluating the primitive function in the integration interval [1, 2] $$ \int_1^2 2x \ dx = [ x^2]_1^2 = 2^2 - 1^2 = 4 - 1 = 3 $$ It is the area between the graph of the function f(x) = 2x and the x-axis within the interval (1,2).

So, there you have it. The quad() function in Matlab is a very useful tool when it comes to calculating definite integrals of functions.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin