how to plot a factorial with variable please help!

8 次查看(过去 30 天)
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:

采纳的回答

Majid Farzaneh
Majid Farzaneh 2018-5-31
编辑:Majid Farzaneh 2018-5-31
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 个评论
Majid Farzaneh
Majid Farzaneh 2018-5-31
编辑:Majid Farzaneh 2018-5-31
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);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Special Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by