how do i integrate one vector by using "intergral function" and not trapz ?

7 views (last 30 days)
code:
i have a complexe double vector f (1x25) and i want to integrate f over -pi and pi, i used it:
phase=1i*integral(f,-pi,pi)
my question is in phase, i am getting "First input argument must be a function handle."
how can i solve it
thank you

Answers (1)

John D'Errico
John D'Errico on 7 Dec 2021
Edited: John D'Errico on 7 Dec 2021
You cannot use the intergral function, as it does not exist. You could use integral, almost. :) Except that it does not apply to vectors of data. :(
The problem is, when you look at a sequence of values, for example
x = 1:5;
y = x.^2
y = 1×5
1 4 9 16 25
Is y a function? Yes, you may think of what you did as executing a function to obtain y. But if all we see is y, it is just a list of numbers. There is no connection between them. There is no knowledge that they represent values of something that WAS a function. MATLAB cannot know how the values in that list of numbers were obtained, or how to infer what should lie between the values, if you wanted it to do so.
So you have a simple solution. You can use trapz. You CANNOT use integral. Integral does not apply to vector of data.
trapz(x,y)
ans = 42
Is that correct?
syms X
int(X.^2,[1,5])
ans = 
double(ans)
ans = 41.3333
Sadly, no, because trapz is not exact for quadratic functions. And we know the origin of the vector y. It was pretty close though.
And a problem is, how could you integrate between two points that are not explicitly given in x?
One simple solution is to convert that list of points into a function, one that can then be integrated. So we might do this:
spl = spline(x,y);
integral(@(x) ppval(spl,x),1,5)
ans = 41.3333
Now integral is exactly correct, to within floating point trash, at least. And now we can integrate between other limits.
integral(@(x) ppval(spl,x),1.3,4.85)
ans = 37.2957
Be careful about trying to go outside of the support of the spline though.
If you have the curve fitting toolbox, then you can use fnint to perform the integration.
splint = fnint(spl);
diff(fnval(splint,[1,5]))
ans = 41.3333
In the end, you need to remember that vectors in MATLAB are just lists of numbers. You need to provide the intelligence in how to use them, what they mean to you, and what functions apply to them in context of their meaning to you.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!