matlab怎么将编好的函数,通过自定义一个函数来实现50!的运算,怎么用matlab编这个程序...

满意答案

function n = factorial(n)

%FACTORIAL Factorial function.

% FACTORIAL(N) for scalar N, is the product of all the integers from 1 to N,

% i.e. prod(1:N). When N is an N-D matrix, FACTORIAL(N) is the factorial for

% each element of N. Since double precision numbers only have about

% 15 digits, the answer is only accurate for N <= 21. For larger N,

% the answer will have the correct order of magnitude, and is accurate for

% the first 15 digits.

%

% See also PROD.

% Copyright 1998-2004 The MathWorks, Inc.

% $Revision: 1.7.4.6 $ $Date: 2004/06/25 18:52:29 $

N = n(:);

if any(fix(N) ~= N) || any(N < 0) || ~isa(N,'double') || ~isreal(N)

error('MATLAB:factorial:NNegativeInt', ...

'N must be a matrix of non-negative integers.')

end

n(N>170) = 171;

m = max([1; n(:)]);

N = [1 1 cumprod(2:m)];

n(:) = N(n+1);

以上是求阶乘的MATLAB内置函数

接下来是自定义实现50!的运算

function result = jiecheng(N)

result = 1;

for ii = 1:N

result = result*ii;

end

调用函数jiecheng(50)

00分享举报

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值