realpow
Element-wise power that errors on complex results.
Category: Mathematics > Elementary Math
Syntax
Z = realpow(X, Y)
Returns X.^Y, or an error if any result would be complex.
Description
A negative base with a non-integer exponent, such as (-8)^(1/3), has no real value, and the .^ operator answers with a complex principal root. realpow raises the error Realpow produced complex result. in that case, which is useful as a guard in code that assumes real numbers.
Otherwise it behaves exactly like .^.
Input arguments
X, Y- Real arrays of compatible sizes: base and exponent.
Output arguments
Z- Real array.
Example
Real powers
realpow([2 3], 2)
realpow(2, [1 2 3])
(-8)^(1/3)
Output
ans = 1×2
4 9
ans = 1×3
2 4 8
ans = 1.0000 + 1.7321i