Matlab - Numerical Integration

In summary, you may be able to integrate your data using the 'trapz' function in Matlab, but you may need to modify the input format first.
  • #1
H_man
145
0
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?

Thanks in advance,

Harry
 
Physics news on Phys.org
  • #2
H_man said:
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?

Thanks in advance,

Harry

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
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/quad.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
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.

Thanks...

:blushing:
 
  • #4
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:
  • #5
Thanks! Thats a very helpful suggestion.

:smile:
 

Related to Matlab - Numerical Integration

1. What is numerical integration in Matlab?

Numerical integration in Matlab is a method used to approximate the integral of a function by dividing the area under the curve into smaller trapezoids. It is a useful tool for solving complex mathematical problems that cannot be solved analytically.

2. How do I perform numerical integration in Matlab?

To perform numerical integration in Matlab, you can use the built-in function 'trapz' or 'quad'. 'trapz' is used for single integration while 'quad' is used for double or triple integration. Both functions require the input of the function to be integrated, the limits of integration, and the number of intervals to be used.

3. What is the difference between 'trapz' and 'quad' in Matlab?

'trapz' is used for single integration while 'quad' is used for double or triple integration. Additionally, 'trapz' uses the trapezoidal rule to approximate the integral, while 'quad' uses adaptive quadrature methods which are more accurate for complex functions.

4. How can I improve the accuracy of my numerical integration in Matlab?

To improve the accuracy of numerical integration in Matlab, you can increase the number of intervals used in the calculation. You can also try using a more accurate integration method, such as 'quad' instead of 'trapz'. Additionally, you can adjust the tolerance level of the function to get a more precise result.

5. Can I perform numerical integration on a vector or matrix in Matlab?

Yes, numerical integration can be performed on a vector or matrix in Matlab. The 'trapz' and 'quad' functions can handle vector inputs and will return the integrated result for each column of the vector or matrix. However, the function must be able to handle vector inputs, so not all functions can be integrated in this way.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
949
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
27
Views
3K
Back
Top