Algorithm for Fractional power calculation
Show older comments
Hi
1. fractional base is no problem with current implementation.
2. To support fractional exponents, get the n-th root for any given number b. How to implement algorithm to get a numerical approximation ?
3. Current approach is inefficient, because it loops e times
b = [-32:32]; % example input values
e = [-3:3]; % example input values but doesn't support fraction's
power_function(b,e)
p = 1;
if e < 0
e = abs(e);
multiplier = 1/b;
else
multiplier = b;
end
for k = 1:e
p(:) = p * multiplier; % n-th root for any given number
end
Answers (1)
Alan Stevens
on 28 Feb 2022
How about using the Newton-Raphson algorithm. Here's the basic idea:
% x^n = b
% Let f(x) = x^n - b
% dfdx(x) = n*x^(n-1)
%
% Use Newton-Raphson iteration
% x1 = initial guess
% err = 1;
% while err > tol
% x = x1 - f(x1)/dfdx(x1);
% err = abs(x - x1);
% x1 = x
% end
13 Comments
Life is Wonderful
on 4 Mar 2022
Here's a simple example
% x = b^(1/n) The n'th root of b
% This can be written as x^n = b or x^n - b = 0
%
% Let f(x) = x^n - b and dfdx(x) = n*x^(n-1)
%
% Then Newton-Raphson algorithm is
% x(n) = x(n-1) - f(x(n-1))/dfdx(x(n-1))
% Simple example:
b = 5;
n = 3;
% i.e. we want the cube root of 5
f = @(x) x^n - b;
dfdx = @(x) n*x^(n-1);
tol = 10^-4;
err = 1;
x = 1; % initial guess
while err > tol
xn = x - f(x)/dfdx(x);
err = abs(xn - x);
x = xn;
end
disp(x)
% Check
disp([x b^(1/n)])
Your original values of b include negative values, for which the roots will, in general, involve complex roots. You will need to modify the routine to account for the real and imaginary parts for these cases.
Life is Wonderful
on 4 Mar 2022
Edited: Life is Wonderful
on 4 Mar 2022
Life is Wonderful
on 4 Mar 2022
Walter Roberson
on 4 Mar 2022
For fixed point work with non-integer exponents, you will find that it is typically easiest to take the fixed-point log, multiply by the exponent, and then take the fixed-point exp() .
There are particular integer exponents and particular roots where those particular powers can be expressed in better ways, but CORDIC approaches are the way to go in most other cases.
Life is Wonderful
on 9 Mar 2022
Edited: Life is Wonderful
on 9 Mar 2022
Walter Roberson
on 9 Mar 2022
Edited: Walter Roberson
on 10 Mar 2022
Life is Wonderful
on 10 Mar 2022
Walter Roberson
on 10 Mar 2022
You asked for Fixed Point implementations, but you say that you do not need CORDIC, and you also say that you do not need lookup tables.
When you say that you do not need lookup tables or CORDIC, do you mean that you only want a loose approximation of the value? Do you mean that you have a lot of memory restrictions so you cannot afford to store the range lookup tables? Do you mean that you are dealing with patent / trade secret issues and cannot use CORDIC techniques for legal reasons?
It would help if I were to understand why you do not want to use well-established CORDIC methods ???
Life is Wonderful
on 10 Mar 2022
Walter Roberson
on 10 Mar 2022
https://cradpdf.drdc-rddc.gc.ca/PDFS/unc82/p531366.pdf talks about a reduced-complexity lookup table method.
Life is Wonderful
on 12 Mar 2022
Edited: Life is Wonderful
on 13 Mar 2022
Walter Roberson
on 12 Mar 2022
Sorry, I do not know.
Categories
Find more on Math Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!