how to plot a factorial with variable please help!

조회 수: 11 (최근 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 CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by