How can i plot this function with factorial?

2 ビュー (過去 30 日間)
James Daron
James Daron 2021 年 2 月 2 日
回答済み: Steven Lord 2021 年 2 月 2 日
I would like to have the following function plotting:
lap = (3*factorial(x)-1)./(120*10^(12));
xValues = linspace(0,1,20);
plot(xValues, semilap(xValues));
I get the following error message:
"Array indices must be positive integers or logical values."
I would also be very grateful if someone could tell me how to plot the Y-axis logarithmically in this case.
  1 件のコメント
VBBV
VBBV 2021 年 2 月 2 日
編集済み: VBBV 2021 年 2 月 2 日
x = 4; % input vector same size as xValues
lap = (3*factorial(x)-1)./(120*10^(12));
xValues = linspace(0,1,20);
semilogy(xValues, (lap)); % semilogy
Value of x here is shown as e,g,

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

回答 (2 件)

Rik
Rik 2021 年 2 月 2 日
If you want to plot a function, you should do so explicitly:
lap = @(x)(3*factorial(x)-1)./(120*10^(12));
xValues = round(logspace(0,1,20));%round to make sure the input to factorial is interger-only
semilogy(xValues, lap(xValues))

Steven Lord
Steven Lord 2021 年 2 月 2 日
You haven't shown us what semilap is. If you intended for that to be the expression in lap evaluated at the points in xValues, you need to make lap a function or a function handle (which could be anonymous.) However:
lap = @(x) (3*factorial(x)-1)./(120*10^(12));
xValues = linspace(0,1,20);
plot(xValues, lap(xValues));
Error using factorial (line 20)
N must be an array of real non-negative integers.

Error in solution (line 1)
lap = @(x) (3*factorial(x)-1)./(120*10^(12));
the factorial function is only defined for real non-negative integer values and xValues contains non-integer values. You could use the gamma function instead.
As for plotting logarithmically, see the semilogy function (or semilogx if you want just the X axis to be in logarithmic scale or loglog if you want both logarithmic.)

カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by