0

I am using below Matlab code to calculate power function i.e. without using built-in function. My requirement is - What improvement/ suggestion make the below function support fractional base and exponential values to calculate power? Thank you

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;
    end

Coder
  • 1
  • Welcome to MSE. That question is off-topic here. – José Carlos Santos Feb 28 '22 at 12:38
  • OK, where shall I ask ? – Coder Feb 28 '22 at 12:47
  • While I agree with @JoséCarlosSantos here are some remarks that you might find helpful: 1. fractional base is no problem with your current implementation. 2. If you want to support fractional exponents, you have to be able to get the $n$-th root for any given number $b.$ Either you use built-in functions or you have to implement your own algorithm to get a numerical approximation for that. 3. Your approach is inefficient, because it loops $e$ times. You might want to take a look at exponentiation by squaring – Reinhard Meier Feb 28 '22 at 13:06

0 Answers0