Calculate Exponentials WITHOUT built-in exponent function?

조회 수: 5 (최근 30일)
Dillan Masellas
Dillan Masellas 2016년 4월 8일
댓글: Walter Roberson 2022년 1월 6일
Greetings!
I am new to MATLAB and going in circles trying to logic my way what should be a simple function:
How do I calculate, using a "for" loop, x^k?
My function inputs are ( x, n ); I start with:
% code
ExpValue = 0;
k = 1;
for i = 1:n;
ExpValue = ExpValue + ?
end
Recall that I must use a Sum for the exponential function... x*1, x*x, x*x*x all the way to squaring x up to n times.
Thanks!

채택된 답변

Roger Stafford
Roger Stafford 2016년 4월 9일
편집: Roger Stafford 2016년 4월 9일
Trying to compute x^n using only summation is a terrible method. Surely you would be allowed to use multiplication!
ExpValue = 1;
for k = 1:n % (Corrected)
ExpValue = ExpValue*x;
end
  댓글 수: 4
Life is Wonderful
Life is Wonderful 2022년 1월 6일
This doesn't work for fractional case
n = 10.2
n = 10.2000
x = 2.5;
x ^ n
ans = 1.1455e+04
ExpValue = 1;
for k = 1:n % (Corrected)
ExpValue = ExpValue*x;
end
ExpValue
ExpValue = 9.5367e+03
Walter Roberson
Walter Roberson 2022년 1월 6일
True, but the original poster only needed integral powers as they were developing a series approximation to exp()

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by