Integrating a Discrete Function in MATLAB

In summary: You seem to be familiar with indexing, so I'm not sure why you're resistant to just extracting the portion you want, especially since this is what is required for the Simpson's rule implementation as well. In summary, the user is looking to integrate a data curve with discrete time points using Simpson's rule in MATLAB. However, MATLAB does not allow for this as it requires a function handle. The user can instead use the trapezoid rule and extract subsets of the data to integrate over a specific interval.
  • #1
tramar
53
0
I have a data curve with discrete time points that I imported into MATLAB. The x-axis is an array named t:

Code:
t =

  1.0e+003 *

    0.0319
    0.0505
    0.0851
    0.1037
    0.1356
    0.1648
    0.2021
    0.2313
    0.3616
    0.5823
    0.8880
    1.1778
    1.4996
    1.7814

The y-axis named Cp:

Code:
Cp =

  1.0e+004 *

    0.0077
    0.7846
    3.7077
    3.2923
    1.8769
    1.3769
    1.0539
    0.7769
    0.5462
    0.3923
    0.2692
    0.1846
    0.1462
    0.1385

The plot of the curve looks like this:

[PLAIN]http://dl.dropbox.com/u/11932911/Cp.jpg

Essentially I want to integrate the function from zero to one of the time points. I tried using the functions quad and trapz but they seem to return red error messages. The function trapz works when I integrate over the entire curve (trapz(Cp)) but it won't let me specify an integration interval.
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Trapz implements the trapezoid rule--if this is what you're looking for, that's great (but since you're not putting in the t-values, you're just getting the sum of x, which may or may not be what you're looking for):
http://www.mathworks.com/help/techdoc/ref/trapz.html

If you want to integrate only a portion of the data, put in only a portion of the vectors!

For instance, to extract the 1st through 5th values of x, you'd type in:
>> extract=x(1:5)

More on array indexing (including the colon operator used above):
http://www.mathworks.com/help/techdoc/learn_matlab/f2-12841.html#f2-428

As per the examples under trapz above, as long as the two input vectors are the same size, trapz should produce something--not sure what the error message is, but if you'd post it, we may be able to help you.
 
Last edited by a moderator:
  • #3
I should have posed the question in a better way... Based on the research I've done I think Simpson's rule is more efficient. I was initially trying to use the quad function:

Code:
quad(Cp, 0, t(1))
? Error using ==> fcnchk at 108
FUN must be a function, a valid string expression, or an inline function object.

Error in ==> quad at 66
f = fcnchk(funfcn);

I'm guessing that the error is because Cp is a discrete function and not a definite function of x. Essentially I need to know how to integrate this curve with Simpson's rule for a certain interval. Is there a way to do it directly or would I need to use the extract=x(..) function?
 
  • #4
The Simpson's rule implementation in MATLAB requires a function handle. Since you have a collection of data points, this won't work. And as you don't have a huge number of datapoints, efficiency isn't terribly relevant.
http://www.mathworks.com/help/techdoc/ref/function_handle.html

I'd suggest you stick with the trapezoid rule and use the subset extraction method I linked to earlier.
 
  • #5


I would suggest using the function "trapz" to integrate your data curve. However, in order to specify an integration interval, you will need to provide both the x-axis values and the corresponding y-axis values to the function. In this case, you can use the following code:

trapz(t(1:5),Cp(1:5))

This will integrate from the first data point to the fifth data point on your curve. You can adjust the indices to specify any interval you desire.

Additionally, you can also use the "cumtrapz" function to perform a cumulative integration on your data, which will give you the area under the curve up to each data point. This can be useful for visualizing the changing integral over time.

I would also recommend checking the documentation for both "trapz" and "cumtrapz" to ensure that your data is in the correct format and that you are using the functions correctly. If you continue to encounter error messages, it may be helpful to consult with a more experienced MATLAB user or seek assistance from the MATLAB community forums.
 

FAQ: Integrating a Discrete Function in MATLAB

1. How do I integrate a discrete function in MATLAB?

To integrate a discrete function in MATLAB, you can use the trapz function. This function takes in the discrete values of the function and returns the numerical approximation of the integral using the trapezoidal rule.

2. What is the difference between integrating a continuous and a discrete function in MATLAB?

The main difference is that a discrete function involves a finite set of values, while a continuous function has an infinite number of values. In MATLAB, you need to use different functions for integrating these two types of functions. For continuous functions, you can use integral or quad functions, while for discrete functions, you can use trapz or quadgk functions.

3. Can I integrate a function with multiple variables in MATLAB?

Yes, you can integrate a function with multiple variables in MATLAB using the integral2 or dblquad functions. These functions allow you to specify the range of integration for each variable and provide the necessary inputs for the function.

4. How can I plot the integrated function in MATLAB?

After integrating the function using one of the integration functions mentioned above, you can use the plot function to plot the integrated function. Make sure to specify the range of integration and the number of points for the x-axis to get a smooth curve.

5. How can I check the accuracy of the integrated function in MATLAB?

To check the accuracy of the integrated function, you can use the integral function with a higher tolerance value. This will give you a more accurate result but might take longer to compute. You can also compare the results of different integration functions to see if they are consistent.

Back
Top