Need help in evaluating integral with quad

13 views (last 30 days)
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.

Accepted Answer

Paulo Silva
Paulo Silva on 10 Mar 2011
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

More Answers (1)

Matt Fig
Matt Fig on 10 Mar 2011
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);

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!