MATLAB How to Integrate Field Measurements Multiplied by COS(Z) in Matlab?

AI Thread Summary
The discussion focuses on integrating a set of over 1000 field measurements multiplied by COS(Z) in Matlab. The user initially attempted to use the quad function but encountered errors due to incorrect input formatting, as it requires a function handle rather than discrete data points. Suggestions were made to use the trapz function for numerical integration, particularly by taking a subset of the data for specific intervals. The user found this advice helpful, indicating a potential solution to their integration challenge. Overall, the thread emphasizes the importance of using appropriate functions and data formats in Matlab for numerical integration tasks.
H_man
Messages
143
Reaction score
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
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?
 
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:
 
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:
Thanks! Thats a very helpful suggestion.

:smile:
 

Similar threads

Replies
2
Views
3K
Replies
7
Views
2K
Replies
13
Views
2K
Replies
3
Views
3K
Replies
5
Views
3K
Replies
7
Views
2K
Back
Top