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

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 Commenti

Please use the [ {} Code ] button to format your code. It’s usually impossible to read otherwise.
(I did it for you here.)
David Anderson
David Anderson il 11 Mar 2015
Modificato: David Anderson il 11 Mar 2015
sorry, yeah done it now.The program runs fine, just instead of a value i receive NaN
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.

Accedi per commentare.

Risposte (1)

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 Commenti

I know the code i have works though as it ran fine on university PC today, all I had to change was inline line to suit new version and i now need to change quad lines, was wondering if there is any other method to implement those 2 quad lines specifically ?
You may not be calling quad correctly, according to the online documentation. (If you’re using an earlier version of MATLAB, that may be close to being correct. I can’t tell because I don’t have access to that documentation.)
If you want to integrate only with respect to ‘tau’ and you have a function that takes two input arguments, one of which is ‘tau’, you have to specify that you want only ‘tau’ as the variable of integration in an anonymous function construction.
For example (and calling quad according to the current online documentation):
Energy = quad(@(tau) X_squared(omega,tau), -1e6, 1e6);
and so on for the other one.
tried that but produces error undefined variable 'omega' ?
You have to define ‘omega’, either as a value your function calculates somewhere or as an argument to your ‘MS7P2’ function.
David Anderson
David Anderson il 11 Mar 2015
Modificato: David Anderson il 11 Mar 2015
Maybe a stupid question but how would you recommend defining omega in function ? Had a few attempts and causes more errors than it solves. Or is there an integral implementation possible ?
implemented your suggestion but used quadv instead of quad, now i get NaN for rows and columns of omega??
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.

Accedi per commentare.

Richiesto:

il 11 Mar 2015

Commentato:

il 11 Mar 2015

Community Treasure Hunt

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

Start Hunting!

Translated by