How to vectorize integral?

7 vues (au cours des 30 derniers jours)
Sameer Chikane
Sameer Chikane le 6 Août 2017
Commenté : TheStranger le 1 Sep 2017
h=0.1;
t1=0:h:2*pi;
alpha=0.3;
f=@(x) sin(x);
for i=2:length(t1)
t=t1(i);
k=1/gamma(1-alpha);
w=@(s) (t-s).^(-alpha).*cos(s);
p(i)=integral(w,0,t);
end
  1 commentaire
TheStranger
TheStranger le 1 Sep 2017
Hello Sameer. Sorry for the offtop, but have you tested, whether a vectorized version is faster than just looping? I tried "integral" function with "ArrayValued=1" and it turned out that it is 20% slower than just looping over the parameter, for example.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 6 Août 2017
alpha=0.3;
syms s t
w = int((t-s).^(-alpha).*cos(s), s, 0, t);
h=0.1;
t1=0:h:2*pi;
p = double(subs(w, t, t1));
  1 commentaire
Sameer Chikane
Sameer Chikane le 6 Août 2017
Thanks a lot. It worked!!!

Connectez-vous pour commenter.

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 6 Août 2017
Modifié(e) : Andrei Bobrov le 6 Août 2017
h=0.1;
t1=0:h:2*pi;
p = arrayfun(@(t)integral(@(s)(t-s).^-alpha.*cos(s),0,t),t1)
  1 commentaire
Sameer Chikane
Sameer Chikane le 8 Août 2017
Thank you!!!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by