# Matlab - Numerical Integration

1. Sep 18, 2008

### H_man

Hi,

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?

Harry

2. Sep 18, 2008

### MATLABdude

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)?

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/trapz.html

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?

3. Sep 18, 2008

### H_man

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.

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

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.

Thanks....

4. Sep 18, 2008

### MATLABdude

Ah... I think I see your problem!

'http://www.mathworks.com/access/helpdesk/help/techdoc/ref/quad.html"' 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 'http://www.mathworks.com/access/helpdesk/help/techdoc/ref/trapz.html"'? For instance, trapz(nn(1:50)). As per the support page, you can also use this if you have non-uniform spacing.

Last edited by a moderator: Apr 23, 2017
5. Sep 18, 2008

### H_man

Thanks!! Thats a very helpful suggestion.