How do i replicate power function? in MATLAB

In summary, the conversation discusses the task of creating a power function without using the power key in MATLAB. The solution involves using a for loop and multiplication to calculate powers of natural integer exponents. However, the possibility of raising a number to any power is also mentioned, which may require a different approach.
  • #1
physizl
9
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
  • #2
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)
 
  • #3
a^b = exp(b*log(a))
 
  • #4
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
 
  • #5
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.
 
  • #6
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.
 

1. How do I write a power function in MATLAB?

To write a power function in MATLAB, you can use the built-in function "power". The syntax is "power(base, exponent)" where "base" is the number you want to raise to a power, and "exponent" is the power you want to raise the base to. For example, to calculate 2 to the power of 3, you would write "power(2,3)", which would give you the result of 8.

2. How do I use the power function in a loop?

To use the power function in a loop, you can define a variable for the base and exponent, and then use the "power" function within the loop. For example, if you want to calculate the powers of 2 from 1 to 10, you could use the following code:

for i = 1:10
    base = 2;
    exponent = i;
    result = power(base,exponent);
    display(result);
end

3. How do I create a custom power function in MATLAB?

To create a custom power function in MATLAB, you can use the "function" keyword to define your own function. The syntax would be "function result = functionName(base,exponent)", and then you can use the "power" function within your custom function. For example, you could create a function called "customPower" that calculates the power of a given base and exponent:

function result = customPower(base,exponent)
    result = power(base,exponent);
end

4. How do I handle negative exponents in a power function?

To handle negative exponents in a power function, you can use the "reciprocal" function in MATLAB. The reciprocal of a number is 1 divided by that number. So, for example, if you want to calculate 2 to the power of -3, you could write "power(2,reciprocal(3))", which would give you the result of 0.125.

5. How do I use the power function with matrices in MATLAB?

To use the power function with matrices in MATLAB, the matrix and the exponent must have the same dimensions. You can use the "power" function element-wise by adding a dot before the function name, like this: ".power". For example, if you have a 2x2 matrix A and you want to raise each element to the power of 2, you could write "A = [.5 .25; .1 .2];" and then "A = A.power(2);" which would give you the result of [0.25 0.0625; 0.01 0.04].

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
786
  • Programming and Computer Science
Replies
16
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
810
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
Back
Top