how to plot a factorial with variable please help!

8 ビュー (過去 30 日間)
yuqing zhu
yuqing zhu 2018 年 5 月 31 日
コメント済み: Majid Farzaneh 2018 年 5 月 31 日
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);
Majid Farzaneh
Majid Farzaneh 2018 年 5 月 31 日
You're welcome.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpecial Functions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by