can you help me to programming algorithm of x^n and n! ,and plotting in same figure

1 回表示 (過去 30 日間)
ime sem
ime sem 2013 年 11 月 7 日
コメント済み: ime sem 2013 年 11 月 8 日
x^n=x*x.......... n fois

回答 (2 件)

Yatin
Yatin 2013 年 11 月 8 日
Hi,
Below are the implementations for the factorial and the power functions. You can store the values from both the functions in an array at each step and then plot these on the same plot using the "plot" function twice. To plot on the same figure, you need to use the "hold" command.
function result = numFactorial(number)
if(number == 0 || number == 1)
result = 1;
else
result = 1;
for index = 1: number
result = result* index;
end
end
end
function [result] = pow(number, numPower)
if(numPower == 1)
result = number;
elseif(rem(numPower, 2) ~= 0)
%a = getpowby2(number, (numPower-1)/2);
a = pow(number,(numPower-1)/2);
result = a*a*number;
else
%a = getpowby2(number, numPower/2);
a = pow(number, numPower/2);
result = a*a;
end
end

Vivek Selvam
Vivek Selvam 2013 年 11 月 8 日
This may help you get started
doc hold
doc factorial
doc power
n = 5;
x = 2;
vector = 1:n;
plot(factorial(vector));
hold on
plot(power(x,vector));
% or the 2 plot commands can be integrated into 1 like below
% plot(vector,factorial(vector),vector,power(x,vector));

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by