Need help in evaluating integral with quad

조회 수: 9 (최근 30일)
Raghav
Raghav 2011년 3월 10일
Hi,I need to numerically evaluate an integral,for different values of a parameter,and store these in a vector.The integral is:
f(theta)=integral of(exp(-2x)*sin(2xsin(theta/2))dx, x running from 0 to 2 I am trying to attempt this with quad using the anonymous function approach.I coded it thus,
theta=0.01:0.1:2*pi-0.01;
f=@(x,th)(exp(-0.5*x)*sin(x*2*sin((th)/2)));
for i=1:length(theta)
th=theta(i);
born(i)=quad(@(x)f(x,th),0,2);
end;
I cannot understand why I get the error
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==> @(x,th)(exp(-0.5*x)*sin(x*2*sin((th)/2)))
Error in ==> @(x)f(x,th)
Error in ==> quad at 77
y = f(x, varargin{:});
Error in ==> born_integral at 8
born(i)=quad(@(x)f(x,th),0,2);
Is there a way to make it work with quad or a similiar function ? (I'll try numerical integration algorithms as a last resort) Thanks in advance.
  댓글 수: 1
John D'Errico
John D'Errico 2011년 3월 10일
Quad IS a numerical integration algorithm!!!!!

댓글을 달려면 로그인하십시오.

채택된 답변

Paulo Silva
Paulo Silva 2011년 3월 10일
Just a dot missing from the second line, before *sin
theta=0.01:0.1:2*pi-0.01;
f=@(x,th)(exp(-0.5*x).*sin(x*2*sin((th)/2)));
for i=1:length(theta)
th=theta(i);
born(i)=quad(@(x)f(x,th),0,2);
end

추가 답변 (1개)

Matt Fig
Matt Fig 2011년 3월 10일
Or, you can do it in one shot with QUADV:
th = 0.01:0.1:2*pi-0.01;
f = @(x)(exp(-0.5*x).*sin(x*2*sin((th)/2)));
born = quadv(f,0,2);

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by