Algorithm for Fractional power calculation
이전 댓글 표시
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
답변 (1개)
Alan Stevens
2022년 2월 28일
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
Life is Wonderful
2022년 3월 4일
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
2022년 3월 4일
Walter Roberson
2022년 3월 4일
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
2022년 3월 9일
편집: Life is Wonderful
2022년 3월 9일
Walter Roberson
2022년 3월 9일
편집: Walter Roberson
2022년 3월 10일
Life is Wonderful
2022년 3월 10일
Walter Roberson
2022년 3월 10일
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
2022년 3월 10일
Walter Roberson
2022년 3월 10일
https://cradpdf.drdc-rddc.gc.ca/PDFS/unc82/p531366.pdf talks about a reduced-complexity lookup table method.
Life is Wonderful
2022년 3월 12일
편집: Life is Wonderful
2022년 3월 13일
Walter Roberson
2022년 3월 12일
Sorry, I do not know.
카테고리
도움말 센터 및 File Exchange에서 Math Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!