how do i integrate one vector by using "intergral function" and not trapz ?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuestas (1)
John D'Errico
el 7 de Dic. de 2021
Editada: John D'Errico
el 7 de Dic. de 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
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)
Is that correct?
syms X
int(X.^2,[1,5])
double(ans)
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)
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)
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]))
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.
0 comentarios
Ver también
Categorías
Más información sobre Numerical Integration and Differentiation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!