Effacer les filtres
Effacer les filtres

Optimize the construction of a function for quadrature purposes

3 vues (au cours des 30 derniers jours)
pluton schmidt
pluton schmidt le 15 Fév 2023
Modifié(e) : Torsten le 15 Fév 2023
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

Réponse acceptée

Torsten
Torsten le 15 Fév 2023
Modifié(e) : Torsten le 15 Fév 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

Plus de réponses (0)

Catégories

En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by