Why am I getting complex values from the following anonymous function?
Show older comments
I'm trying to build a function to be used with lsqnonlin(), it should pull data from 3 arrays of doubles and output a value. When I call it though, I get a 10x10 complex double.
Here's my function:
fun = @(consumption1,leisure1,pollution1)
(((leisure1.^-2.5).*((.5*consumption1.^-1.5)+(.5*imag(pollution1.^1.5))))/
((consumption1.^-2.5).*((.5*leisure1.^-1.5)+(.5*imag(pollution1.^1.5)))))
and here's the result of the first 10 values:
Columns 1 through 4
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
Columns 5 through 8
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
Columns 9 through 10
-0.0000 + 0.2777i 0.0000 + 0.0000i
-0.0000 - 0.0000i 0.0000 + 0.0000i
0.4205 + 0.0000i 0.0000 + 0.0000i
-0.0000 - 0.0000i 0.0000 + 0.0000i
-0.0000 + 0.0198i 0.0000 + 0.0000i
-0.0000 - 0.0000i 0.0000 + 0.0000i
0.0000 - 0.0000i 0.0000 + 0.0000i
0.0000 - 0.0000i 0.0000 + 0.0000i
1.2943 + 0.0001i 0.0000 + 0.0000i
0.0000 - 0.0000i 0.0000 + 0.0000i
Any ideas or insight would be greatly appreciated, I was previously running into the same problem when I would do pollution1.^1.5, which is why I added(imag(pollution1.^1.5) which gave the correct value.
Answers (1)
Star Strider
on 12 Aug 2016
My guess is that one of your variables has at least one negative number. Raising them to a non-integer power produces a complex value. Note that not all of them are complex, but MATLAB produces complex representation for them all.
Example:
x = [1 2 3 -4 5]';
y = x.^-1.5
y =
1 + 0i
0.35355 + 0i
0.19245 + 0i
0 + 0.125i
0.089443 + 0i
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!