How do i replicate power function? in MATLAB

Click For Summary

Discussion Overview

The discussion revolves around replicating the power function in MATLAB without using the power operator. Participants explore how to compute a number raised to a power using loops and multiplication, specifically focusing on positive integer exponents.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster (OP) seeks a method to compute a power function using only loops and multiplication, specifically for positive integers.
  • One participant suggests a corrected approach using a separate loop counter variable instead of reusing the input variable, providing a code example that successfully calculates the power.
  • Another participant introduces the mathematical expression a^b = exp(b*log(a)), but questions its relevance to the OP's request.
  • There is a clarification that in MATLAB, the function log refers to the natural logarithm, while log10 is used for base 10 logarithm.
  • One participant notes a potential inconsistency in the OP's request regarding the types of exponents they wish to calculate.

Areas of Agreement / Disagreement

Participants generally agree on the method for calculating powers using loops for positive integers, but there is disagreement regarding the applicability of logarithmic functions to the OP's problem and uncertainty about the OP's requirements.

Contextual Notes

There are limitations in the discussion regarding the types of exponents that can be handled, as well as the OP's conflicting statements about the problem scope.

physizl
Messages
9
Reaction score
0

Homework Statement



need to do a power function of any number to any power WITHOUT using the power key

aka i need to replicate the power function using only loops and multiplication

for example, compute 5 to the third power by doing 5*5*5 not 5^3

but i don't know how to tell MATLAB to multiply a number by itself n times

Homework Equations



for loops


The Attempt at a Solution



Code:
num = input(' value? ')
n = input( ' power? ')
for num = 1:n
    num = num*num;
end
disp(num)
 
Physics news on Phys.org
Try this. Note that it is a bad idea to use the same variable for both input and as a loop counter. This is why I changed the loop counter variable to i.
Code:
num = input(' value? ')
n = input( ' power? ')
product = 1
for i = 1:n
    product = product*num;
end
disp(product)
Using your example numbers of num = 5 and n = 3 (to get 53), you get these values for product:
product = 1 (before start of loop)
product = 5 (when i = 1)
product = 25 (when i = 2)
product = 125 (when i = 3)
 
a^b = exp(b*log(a))
 
CEL said:
a^b = exp(b*log(a))
True (sort of), but not relevant to this problem. log on the right side above should be ln, the natural log.

Emphasis added.
physizl said:
aka i need to replicate the power function using only loops and multiplication
 
Mark44 said:
True (sort of), but not relevant to this problem. log on the right side above should be ln, the natural log.

Emphasis added.

In Matlab log is the natural logarithm. The base 10 logarithm is log10.
Using loops and multiplication you can only calculate powers of natural integer exponents.
 
The OP seems to be giving conflicting information about the problem - using loops and multiplication (which is fine for pos. integer exponents) vs. raising a number to any power.
 

Similar threads

Replies
3
Views
3K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
13
Views
3K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K