Algorithm for Fractional power calculation

7 Ansichten (letzte 30 Tage)
Life is Wonderful
Life is Wonderful am 28 Feb. 2022
Bearbeitet: Life is Wonderful am 13 Mär. 2022
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

Antworten (1)

Alan Stevens
Alan Stevens am 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 Kommentare
Life is Wonderful
Life is Wonderful am 12 Mär. 2022
Bearbeitet: Life is Wonderful am 13 Mär. 2022
Hi Walter,
Please see below code which is computing exp() and taylor series. At the 1 iteration final exp() value is calculated/converge. Please find the attachment
I could see with one multiplication and one addition, I could get the exp() THDN = -108dBc with x = +23 & -145 with x = -23 dBc which is very much efficient. The same works for fixed point as well . One need to be careful with division operator ( this takes lots time ).
I have added Excel spread sheet ( to replicate my below implementation ) for offline calculation. It has upto 10 fraction place of accuracy ( which pretty good for any standard as acceptance criteria). Please find the attachment.
Next step - With below code, do you have a proposal where number iteration is reduced [ I think one can optimize 90 iteration ] and If one do a loop unroll [smaller than 5 iteration ] , it can be done one max 5 steps. Any proposal/ suggestion ?
Thanks !!
n = 1e2;
x = 23.0; % for taylor series [ -22:22] for 32 bit DSP
inpval = 23.0; % for built-in exp()
% create a function and pass the args
expval = 1.0; % initialize as double
fprintf('%20s|%30s|%26s|%25s|%25s|\n--------------------+------------------------------+--------------------------+-------------------------+-------------------------+\n',"indx","expval","exp(x)",'diff','Nbits');
for i = n-1:-1:1
expval = 1 + x * expval / i;
fprintf('%20d|%30.10f|%26.15f|%25.10f|%25.10f|\n',i,expval,exp(inpval),abs(exp(inpval) - expval), log2(exp(inpval)));
end
Walter Roberson
Walter Roberson am 12 Mär. 2022
Sorry, I do not know.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Linear Programming and Mixed-Integer Linear Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by