quad NaN, encountered infinite or not a number value???

5 views (last 30 days)
im using this function to calculate energy and bandwidth of x. my MATLAB version is more up to date than my uni textbooks so quad functions do not output numbers but NaN. Is there another way to implement the qaud lines highlighted (at the bottom) correctly?
function [W,E_W, Energy] = MS7P2 (tau, beta, tol)
%MS7P2.M: matlab seswsh 7, program2
%fucntion m file ocputes the essential bandwidth w for square pulse
%inputs: tau = pulse width, beta= fractin of signal energy desired in W
%tol = tolerance of relative energy error
%outputs: w= essential bandwidth [rad/s] , E_W = ENERGY CONTAINED IN
%BANDWIDTH W
W = 0; step = 2*pi/tau; %initial guess and step balues
X_squared = @(omega,tau) (tau*MS7P1(omega*tau/2).^2);
E = beta*tau; %desired energyt in w
relerr = (E-0)/E; %initial relative error is 100 percent
while(abs(relerr) > tol),
if (relerr>0),%W too small
W=W + step; %increase w by step
elseif(relerr<0),%w too large
step = step/2; W = W-step; %decrease step size and then w.
end
Energy = quad (X_squared, -1e6,1e6 [], [], tau); ********PROBLEM HERE**************
E_W = 1/(2*pi)*quad (X_squared, -W,W,[], [], tau); ********PROBLEM HERE**************
relerr = (E - E_W)/E;
%[W,E_W, Energy] = MS7P2 (3,0.96,0.00001)
end
  3 Comments
David Anderson
David Anderson on 11 Mar 2015
Edited: David Anderson on 11 Mar 2015
sorry, yeah done it now.The program runs fine, just instead of a value i receive NaN
Adam
Adam on 11 Mar 2015
Putting ASSIGNMENT DUE or IMPORTANT in all your question titles is more likely to get people to not answer them than to do so. Everyone's questions are important to them and people give help in their own time so asking people to hurry up to answer your question is generally frowned upon.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 11 Mar 2015
A problem:
X_squared = @(omega,tau) (tau*MS7P1(omega*tau/2).^2);
Please don’t call your function from within that function. It creates recursion problems.
The problem is likely not with the quad function, since it still works in R2014b. It just suggests the integral function instead.
In your other Question about this, you defined the problem as:
  • Considering x(t) as a squared pulse with duration 3 seconds and X(ω) its Fourier Transform, verify in Matlab, that the energy of X(ω) is consistent with the value of 2 · π · 3. Moreover find the essential bandwidth (in radians per second) that contains the 96% of the energy of the square pulse with a tolerance of relative energy error of 0.00001
I’m not quite certain what functions you’re allowed to use, but I would:
  1. Define the pulse as a function of time, either as a function or as a vector (depending on what you’re instructed to do), with respect to a time vector;
  2. Take the Fourier transform of the pulse (however you’re instructed to do so), remembering to create a frequency vector as well;
  3. Plot the Fourier transform of the pulse as a function of frequency (primarily for your information and benefit);
  4. Find the energy and essential bandwidth, according to the procedure outlined in the book.
  7 Comments
David Anderson
David Anderson on 11 Mar 2015
implemented your suggestion but used quadv instead of quad, now i get NaN for rows and columns of omega??
Star Strider
Star Strider on 11 Mar 2015
I would imagine ‘omega’ is the radian frequency vector output from the Fourier transform. (The other output would be the amplitude at each value of ‘omega’.)
It is not difficult to manually calculate the Fourier transform of a square wave. (It’s easier with the Symbolic Math Toolbox.) Calculating the integral representation and then evaluating it in MATLAB would be one way. The other way would be to define a time scale of appropriate resolution (use the linspace function) and a matching square wave (amplitude), then take the Fourier transform of it using the fft function. Calculate a matching frequency vector (that is likely your ‘omega’) at the same time, following the example code in the documentation for fft. You might use the cumtrapz or trapz functions to integrate the Fourier transform to get the energy and essential bandwidth, as your textbook instructs you to do.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!