Borrar filtros
Borrar filtros

Optimize the construction of a function for quadrature purposes

3 visualizaciones (últimos 30 días)
Hello,
I want to compute the integral of a function which is the linear combination of some known function family (here, Fourier functions):
MyInt = integral(@(t) myfun(t), 0, 2*pi);
My function myfun(t) is built as follows:
function Output = myfun(t)
Output = 0;
for p = 1:20
Output = Output + 1/p.*cos(p*t);
Output = Output + 1/(2*p).*sin(p*t);
end
I can't think of a way to vectorize what's above without the for loop? Can we do better?
Thank you

Respuesta aceptada

Torsten
Torsten el 15 de Feb. de 2023
Editada: Torsten el 15 de Feb. de 2023
MyInt = integral(@myfun, 0, 2*pi)
MyInt = -5.5511e-16
function Output = myfun(t)
p = (1:20).';
Output = sum(1./p.*cos(p.*t) + 1./(2*p).*sin(p.*t),1);
end

Más respuestas (0)

Categorías

Más información sobre Numerical Integration and Differentiation en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by