How to integrate functions inside a loop

1 vue (au cours des 30 derniers jours)
Abdurrahman M
Abdurrahman M le 19 Oct 2016
Hello, I am a beginner in matlab and I am trying to integrate a function where the independent variable is x, from 0 to infinity, for a range of values of en1. Here is my attempt:
a = dlmread('density.txt');// this is a two column matrix with 40 points
en1 = a(:,1);
int1 = a(:,2);
El = 0;
Tl = 0.41;
Em = -918.85;
Tm = 0.17;
Ea = -13.95;
Q = zeros(size(en1));
for k = 1:length(en1)
f = @(x,k) (int1(k))/(((en1(k)-x-El).^2+((Tl)^2)./4)*((en1(k)-x-Ea-Em)^2+((Tm)^2)/4));;
Q = quadgk(f,0,Inf);
end
I am quite lost here and would greatly appreciate it if someone can help.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 19 Oct 2016
Modifié(e) : Andrei Bobrov le 19 Oct 2016
a = dlmread('density.txt');
en1 = a(:,1);
int1 = a(:,2);
El = 0;
Tl = 0.41;
Em = -918.85;
Tm = 0.17;
Ea = -13.95;
f = @(x) int1./(((en1-x-El).^2+Tl^2./4).*((en1-x-Ea-Em).^2+Tm^2/4));
Q = integral(f,0,inf,'ArrayValued',true);
or
en1 = a(:,1);
int1 = a(:,2);
El = 0;
Tl = 0.41;
Em = -918.85;
Tm = 0.17;
Ea = -13.95;
f = @(x,k) int1(k)./(((en1(k)-x-El).^2+Tl^2./4).*((en1(k)-x-Ea-Em).^2+Tm^2/4));
Q = zeros(numel(int1),1);
for k = 1:length(en1)
Q(k) = quadgk(@(x)f(x,k),0,Inf);
end
  1 commentaire
Abdurrahman M
Abdurrahman M le 19 Oct 2016
It worked great! Thank you so much Andrei!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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