Why does quad work when integral does not?

7 Ansichten (letzte 30 Tage)
Stephen Ogier
Stephen Ogier am 4 Okt. 2016
Kommentiert: Walter Roberson am 5 Okt. 2016
I'm working on an antenna array problem, and I've run into a quirk in the MATLAB integration routines. When I integrate using the newer integrate function, I get an incorrect result for certain values. When I use the older quad function, my results are correct. Why is this happening? Is there some way to make integrate more accurate? Decreasing the absolute and relative error tolerances didn't help. This is concerning because quad is being deprecated in favor of integrate.
The function was called with the following parameters Z21(0.5, 0.5, 0.35, 0) quad gives Z21 = 17.4274 -37.3897i integrate gives Z21 = 5.4256 -32.7371i
This just gets worse as d gets smaller.
function [ Z ] = Z21( L1, L2, d, h )
%Z21 Computes mutual impedancee of two parallel dipoles with sinusoidal
%current
% Inputs are normalized to wavelength
eta = 120*pi;
I = 1;
%Inline helper functions for clarity
r = @(z) sqrt(d.^2 + (h+L2./2+z-L1./2).^2);
R1 = @(z) sqrt(d.^2 + (h+L2./2+z-L1).^2);
R2 = @(z) sqrt(d.^2 + (h+L2./2+z).^2);
G_norm = @(R) exp(-1i*2*pi*R)/(4*pi*R);
Integrand = @(z) sin(pi.*L2 - 2.*pi.*abs(z)).*(G_norm(R1(z)) + G_norm(R2(z)) - 2.*cos(pi.*L1).*G_norm(r(z)));
%ezplot(@(z) real(Integrand(z)),[-L2/2,L2/2])
%Z = 1i*eta *I* quad(Integrand,-L2/2,L2/2);
Z = 1i*eta *I* integral(Integrand,-L2/2,L2/2, 'RelTol', 1e-13, 'AbsTol', 1e-17);
end
  1 Kommentar
Walter Roberson
Walter Roberson am 4 Okt. 2016
My testing in Maple suggests approximately 17.503850498058055284389205052946 -37.416687039385925796084425375631*i which is slightly different than the quad value, possibly just numeric roundoff.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

David Goodmanson
David Goodmanson am 5 Okt. 2016
Bearbeitet: David Goodmanson am 5 Okt. 2016
Hi Stephen, You are missing a very important dot in your definition of G_norm. If you replace
/ with ./
then both methods agree,without any special tolerance requests for the 'integral' function.
It's interesting that if a,b and c are equal-length row vectors, then
(a/b).*c and (a./b).*c
both produce row vectors, with no error message due to mismatched dimensions, but those vectors are different. However the expressions agree if a,b,c are scalars. So I will speculate that quad works because it accesses its function one point at a time, but integral is more vectorized and so gets it 'wrong'.
  1 Kommentar
Walter Roberson
Walter Roberson am 5 Okt. 2016
Good catch, David. And yes, integral() does vectorize.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by