Replacing the "quad" function with "integral" doesn't work in Matlab

1 reply · 4K views · last reply
PainterGuy
Messages
938
Reaction score
73
TL;DR
Why using numerical integration "integral" function is not working for me?
Hi,

I was trying to numerically integrate the following inverse Fourier transform integral,
Equation: f(x) = (1/π) ∫ from 0 to ∞ of [2 sin(ωπ) / ω] cos(ωx) dω
, using the code below. The plot is also shown below.

The plot looks good which means the result is good as well. By the way, I was getting a warning which I quote below the code.

Matlab:
% file name "fourier_transform_unit_pulse_numerical_integration.m"
% using quad function

clear all; close all; clc;

x=linspace(-8*pi,8*pi,300);
Rs_x=0.*[1:300];for i1=1:300

    x1=x(i1);

    f=@(w)(1/pi)*(((2*sin(pi*w)/w)*cos(x1*w)));

    Rs_x(i1)=quad(f,0,60);

end

plot(x,Rs_x);
hold;

Code:
Warning: Maximum function count exceeded; singularity likely.
> In quad (line 98)
  In fourier_transform_unit_pulse_numerical_integration (line 18)

Plot #1
MATLAB plot of a rectangular pulse: value 1 between roughly x = -3 and x = 3, near 0 elsewhere, with small ripples on the baseline, over x from -30 to 30.
In the code below, I'm using "quad" function to do the numerical integration and I understand that it's a deprecated function and "integral" function should be used instead. I replaced "quad" with "integral" in the code. The "integral" function doesn't work for me as the plot below shows. Where am I going wrong? Could you please help me?

Matlab:
Rs_x(i1)=quad(f,0,60);
changed to
Matlab:
Rs_x(i1)=integral(f,0,60);

Plot #2
MATLAB plot of an oscillating function against x from -30 to 30, with a tall central peak near 0 reaching about 2.5 times 10 to the minus 3, and decaying ripples around it.
 
Hi again,

I think that I understand it now. The following code works. Please note that in this corrected code I'm using the operators .^, .*, ./, and .\ which are called array operators and are used when multiplying or dividing vectors or matrices of the same size term by term. It means that the function "integral" accepts vector or array values by default. When the array operators are used, it signals that element by element operations are to be carried out; in other words, all the variables should be treated as scalars or as a single element of an array.

In MATLAB functions such as sin and sign can have either scalars or arrays (vectors or matrices) passed to them. There are a number of functions that are written specifically to operate on vectors or on columns of matrices; these include the functions min, max, sum, prod, cumsum, and cumprod.

It's mostly safe to use array operators because this increases the chances that the expression would work with most functions.

Matlab:
% file name "fourier_transform_unit_pulse_numerical_integration.m"
% using integral function

clear all; close all; clc;

x=linspace(-8*pi,8*pi,300);
Rs_x=0.*[1:300];for i1=1:300

    x1=x(i1);

    f=@(w)(1/pi).*(((2.*sin(pi.*w)./w).*cos(x1.*w)));

    Rs_x(i1)=integral(f,0,60);

end

plot(x,Rs_x);
hold;

Line plot of a rectangular pulse with a flat top at 1 between about x = -3 and x = 3, zero elsewhere, with small ripples near the edges, on axes from -30 to 30 and -0.2 to 1.2.


We could have used instead the following lines without using array operators and the code would work fine. In MATLAB command window type: help integral.

Matlab:
f=@(w)(1/pi)*(((2*sin(pi*w)/w)*cos(x1*w)));
Rs_x(i1)=integral(f,0,60, 'ArrayValued', true);

Using "ArrayValued', true" signals that all the variables are arrays and term by terms operations are to be carried out.

Thank you!

Helpful link(s):
1: https://www.mathworks.com/help/matlab/ref/integral.html#btw3ipp-6
 
Last edited:
  • Informative
Likes   Reactions: mfb