how to plot a factorial with variable please help!

2 views (last 30 days)
I want to plot a function that contains factorial in it. But for some reason it produced many errors. The graph can be plotted if I open up the factorial and write it. But I am wondering what's wrong with with the code with factorial function.
Here's the code I am struggling with:
syms n;
f(n) = 1-factorial(3000-n)./factorial(2990-n)./(3000^10);
range = linspace(1,3000,3000);
plot(range, f(range));
This is the error message:
Error using symengine
Nonnegative integer or a symbol expected.
Error in sym/subs>mupadsubs (line 157)
G = mupadmex('symobj::fullsubs',F.s,X2,Y2);
Error in sym/subs (line 142)
G = mupadsubs(F,X,Y);
Error in symfun/feval>evalScalarFun (line 42)
y = subs(formula(Ffun), Fvars, inds);
Error in symfun/feval (line 28)
varargout{1} = evalScalarFun(F, Fvars, varargin);
Error in symfun/subsref (line 178)
B = feval(A,inds{:});
Error in untitled (line 4)
plot(range, f(range));
If I open up the factorial (write out each term and run the code, it produced the desired image:

Accepted Answer

Majid Farzaneh
Majid Farzaneh on 31 May 2018
Edited: Majid Farzaneh on 31 May 2018
Hi, This will work:
syms n;
f(n) = 1-factorial(3000-n)./factorial(2990-n)./(3000^10);
range = linspace(1,2990,2990);
plot(range, f(range));
The input argument of factorial must be positive or zero (Test factorial(-1)). You have factorial(2990-n) and n=2991:3000 in your code that makes factorial undefined.
  3 Comments
Majid Farzaneh
Majid Farzaneh on 31 May 2018
Edited: Majid Farzaneh on 31 May 2018
I have an another suggestion for you. This code has better performance if you can forget the syms
n = linspace(1,2990,2990);
f = 1-factorial(3000-n)./factorial(2990-n)./(3000^10);
plot(n, f);

Sign in to comment.

More Answers (0)

Categories

Find more on Special Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!