Getting factorials to work in my equation

16 vues (au cours des 30 derniers jours)
imarquez
imarquez le 14 Juin 2016
Commenté : imarquez le 14 Juin 2016
Hello everyone,
I am having a problem with my code where I need to use a factorial due to the equation needing it in the denominator as I'm solving for a Fourier coefficient. I keep getting: Error using factorial (line 20) N must be an array of real non-negative integers.
Here is the code I'm using:
if true
clear all, close all, clc
format long
fc = 2E9 %center frequency
W0 = 1 %omega 0
Tao = 30E-3 %Tao
T = 0.1 %T
k = linspace(0, 10E12, 1000) %k
y = ((((-1).^k).*(k.*pi.*(fc+(k.*10)).*Tao).^2.*k)./factorial(2.*k+1))
x = sum(y)
Ck = W0*(Tao/T)*abs(x) %coefficient
figure(1)
plot(Ck,y)
% code
end
First of all I'm confused by the line 20 since there are not that many lines in my code, but I get this error right after "Error in PlottingFourierCoeff (line 8) y = ((((-1).^k).*(k.*pi.*(fc+(k.*10)).*Tao).^2.*k)./factorial(2.*k+1))" and that makes sense since that's where the factorial comes into play. My code may be a little rough because I started at at the end of the day yesterday and didn't get all the way through it since I knew I needed to figure out how to fix the factorial problem, but thank you for any help on this matter in advance!

Réponse acceptée

Steven Lord
Steven Lord le 14 Juin 2016
There's no guarantee that all the values of 2*k+1 are integer values and the factorial function is only defined for integer value inputs. What, for example, should factorial(1.2345) return? [I know someone will suggest gamma, but there are other issues at work here.]
But even ignoring that, you have a much larger issue. Given the magnitude of the elements in k, only the first term in your expression for y matters. All of the rest are large enough that factorial overflows to Inf.
Do one or more of the following:
  1. only operate with the first element of k
  2. reduce the magnitude of the values in k. Anything over k = 18 results in factorial(k) being greater than flintmax; anything over k = 170 results in factorial(k) overflowing to Inf. [The factorial function grows VERY quickly.]
  3. change the units of your problem. For example if you're working with quantities whose units are meters, consider representing your quantities in terms of kilometers instead.
  3 commentaires
Steven Lord
Steven Lord le 14 Juin 2016
  1. You don't have an infinite amount of time to wait for MATLAB to perform your summation for k = 1 to k = Inf. Nor do you have an infinite amount of memory to store the vector k.
  2. You're trying to operate on extremely, almost ridiculously large numbers. For example, let's compute the factorial of 200 using Symbolic Math Toolbox:
>> vpa(factorial(sym(200)), 20)
ans =
7.8865786736479050355e374
To put that into perspective, a lower bound on the volume of the observable universe in cubic meters is about 7e83, or just a little more than factorial(61).
Choose a reasonable upper bound for the variable k, one beyond which each term of your summation is negligible. That upper bound most certainly will not be 10e12; it's probably going to be somewhere between 10 and 100. Like I said before, the factorial function grows VERY quickly.
imarquez
imarquez le 14 Juin 2016
I ended up using a string to represent k from 1:16 and then turned T into my vector with the sampling. I've gotten close to what I needed now so thank you for helping me figure out that I could only do 16 iterations of this. Now I just need to find the frequency portion of the equation I'm using so that I can plot this verses frequency.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by