Clear Filters
Clear Filters

trapz Vs. quad - 1 integral 2 different results

4 views (last 30 days)
Hi every one,
I'm trying to find a numeric solve for an integral, and got some weird answer.
so i tried with another function and got a different answer.
i suppose one of them is the right answer, my bet is the trapz answer is the right one. but I'm not sure.
can somebody explain where i am wrong? next thing i need to solve an 2d-integral using quad2d, and i have to know how to do it right.
10x
here is a running code:
clear
Fi1=@(ro,z,theta1,k)exp(k.*z.*cos(theta1).*1i).*sqrt(cos(theta1)).* sin(theta1).^2.*besselj(1.0,k.*ro.*sin(theta1));
NA=0.95;
ni=1;
alpha=asind(NA/ni)
alpha=asin(NA/ni);
lambda=600e-9;
r=0.5e-6;
z=0;
V=linspace(0,alpha,300);
k=2*pi/lambda;
I1_1=trapz( Fi1(r,z,V,k), V)
I1_2=quad( @(theta1) Fi1(r,z,theta1,k), 0,alpha)

Accepted Answer

Marlies
Marlies on 27 Mar 2012
  • The practical answer to why you get two different answers is that you have switched the X- and Y input for the trapz command. If you replace the line that computes I1_1 with the line below, you will get the same answer for the two.
I1_1=trapz(V, Fi1(r,z,V,k))
  • The more generic question is which integration-command you should be using to solve a 2D integral. Of the two methods you mentioned, only quad2d is capable of performing 2D quadratic integration.
  • If you also have access to release R2012a, there is also the command 'integral' available. That command uses a global adaptive quadrature while quad uses the adaptive Simpson quadrature (1D) or integration over the planar region (2D). See also the documentation for those two methods: href=""<http://www.mathworks.com/help/techdoc/ref/f16-5872.html#f16-6472</a>>
Hope this helps.
Marlies

More Answers (0)

Community Treasure Hunt

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

Start Hunting!