how to plot a factorial with variable please help!
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
yuqing zhu
am 31 Mai 2018
Kommentiert: Majid Farzaneh
am 31 Mai 2018
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:
0 Kommentare
Akzeptierte Antwort
Majid Farzaneh
am 31 Mai 2018
Bearbeitet: Majid Farzaneh
am 31 Mai 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 Kommentare
Majid Farzaneh
am 31 Mai 2018
Bearbeitet: Majid Farzaneh
am 31 Mai 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);
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Special Functions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!