Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab - Numerical Integration

  1. Sep 18, 2008 #1

    I have a set of over 1000 field measurements as a function of Z. I want to multiply them by COS(Z) and integrate with respect to Z.

    I have tried using the quadl function but to no avail.

    Is what I want to do possible in Matlab (ie. is there a built in function for this) and if so would some kind individual let me know how?

    Thanks in advance,

  2. jcsd
  3. Sep 18, 2008 #2


    User Avatar
    Science Advisor

    I assume that you've got numerical data here, and not symbolic stuff, and I'll assume that "to no avail" means you get a red error message...

    Can you do with Trapezoid rule integration (trapz), or Simpson's Rule (quad)?


    Not to be patronizing (not knowing your background) but typing 'help quad' would bring up the internal MATLAB documentation for the 'quad' function (though the online documentation, as linked above, is more thorough). Perhaps you're not formatting your inputs properly?
  4. Sep 18, 2008 #3
    Hi, the trapz function works just fine, but I need to integrate over a specific range and it just integrates all the data.

    The quad function I simply can't get working. I have cut and pasted below the relevant lines of code, and the error message.

    load data_2_6_08_2.txt

    z_2_6 = data_2_6_08_2(:,1);
    By_2_6 = data_2_6_08_2(:,2);

    z = -650:650;
    nn = spline (z_2_6, By_2_6, z);

    q = trapz(nn);
    q1 = quad(nn,1,50); %Its this line that gives me the problem%

    I get the error....

    ??? Error using ==> fcnchk at 105
    FUN must be a function, a valid string expression,
    or an inline function object.

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

    Error in ==> TryingTheAviFilter at 12
    q1 = quad(nn,1,50);

    I did look at the help that comes with Matlab but did not find it helpful in this case. Any idea what I am doing wrong... I'm sure its something daft.


  5. Sep 18, 2008 #4


    User Avatar
    Science Advisor

    Ah... I think I see your problem!

    'quad' and 'quadl' both expect actual symbolic (or constant) functions (or function handles). Not a collection of discrete points (see the example under the 'quad' or 'quadl' documentation where they actually go about defining a quadratic).

    If you only need to (numerically) integrate over an interval, why not just take a subset of your data points, and then use 'trapz'? For instance, trapz(nn(1:50)). As per the support page, you can also use this if you have non-uniform spacing.
  6. Sep 18, 2008 #5
    Thanks!! Thats a very helpful suggestion.

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Matlab - Numerical Integration
  1. Integration in Matlab (Replies: 3)

  2. Integration in Matlab (Replies: 1)

  3. Matlab integral (Replies: 0)

  4. Integration in matlab (Replies: 0)

  5. Integration in matlab (Replies: 3)