Calculate Exponentials WITHOUT built-in exponent function?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dillan Masellas
el 8 de Abr. de 2016
Comentada: Walter Roberson
el 6 de En. de 2022
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!
0 comentarios
Respuesta aceptada
Roger Stafford
el 9 de Abr. de 2016
Editada: Roger Stafford
el 9 de Abr. de 2016
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 comentarios
Life is Wonderful
el 6 de En. de 2022
This doesn't work for fractional case
n = 10.2
x = 2.5;
x ^ n
ExpValue = 1;
for k = 1:n % (Corrected)
ExpValue = ExpValue*x;
end
ExpValue
Walter Roberson
el 6 de En. de 2022
True, but the original poster only needed integral powers as they were developing a series approximation to exp()
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!