Function that shouldn't be able to generate complex numbers giving me complex results

2 views (last 30 days)
Basically the original question. I've got a function that shouldnt physically be able to give me complex numbers (the only thing that is taken to a root is a sum of two squares, which has to be positive). I've included the code that I have here. If someone could give me some info on why this is returning complex outputs I would love you forever.
f = @(x1,x2)((1/5)*((x1)^2 + (x2)^2)^(3/2) - (x1^2 + x2^2) + x1/2 + x2/2 + 7);
lim=[-5,0,-5,0];
X1 = linspace(lim(1),lim(2),10);
X2 = linspace(lim(3),lim(4),10);
[x1,x2] = meshgrid(X1,X2);
f(x1,x2)
The only reason I really care is that i'm trying to take this function and put it into the surf function, which threw the error
Error using surf (line 71)
X, Y, Z, and C cannot be complex.
I did a little hunting on my own, and discovered that for some reason, when I put just straight values (like if I were to call f(-4, -3)) it returns a positive decimal number, but when I pass the two matrices, it returns a bunch of complex numbers.

Accepted Answer

Ryan Berry
Ryan Berry on 19 Apr 2022
Never mind I figured it out.
For anyone else who may run into this issue - because I was inputting a pair of matrices, raising stuff to powers becomes weird. I wanted to raise each individual number to the 3/2, and so I had to do
((x1.^2) + (x2.^2)).^(3/2)
Without the dot modifier on the to the 3/2, it returns complex numbers.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!