How do we find the value of y=cos(x.^(2.5)) ?

3 views (last 30 days)
I have a matrix x having elements from -1 to 1 with increment of 0.5 I want to find the power of each element to the power 2.5
My Code:
x=[-1:0.5:1]
y=cos(x.^(2.5))
Command Window Display y =
0.0000 + 1.0000i 0.0000 + 0.1768i 0.0000 + 0.0000i 0.1768 + 0.0000i 1.0000 + 0.0000i
I'm getting complex values for some reason
  4 Comments
KSSV
KSSV on 28 Dec 2017
I suspect, you are not using inbuilt cos function......What does which cos gives?

Sign in to comment.

Answers (2)

Birdman
Birdman on 28 Dec 2017
Edited: Birdman on 28 Dec 2017
syms x
y=cos((x).^(2.5));
y=subs(y,x,[-1:0.5:1]);
y=vpa(y,4)
  5 Comments
Birdman
Birdman on 28 Dec 2017
Edited: Birdman on 28 Dec 2017
When you check
(-1).^2.245
it results a complex number. Therefore first two terms are complex.
John D'Errico
John D'Errico on 28 Dec 2017
There is nothing fishy about what you got!
When you raise a negative number to a non-integer power, the result will be complex. That is fact.
If the power is an integer multiple of 0.5, 2.5 for example, then the result will be purely imaginary. The cosine of a number that lies purely on the imaginary axis will be real.
When the power is NOT an integer multiple of 0.5, then the result of raising a negative number to that power will have a real part. The cosine function of a complex number that has a non-zero real and imaginary part will be complex.
So there is nothing fishy. I explained all of this in some depth in my answer.

Sign in to comment.


John D'Errico
John D'Errico on 28 Dec 2017
Edited: John D'Errico on 28 Dec 2017
Why are you surprised at getting complex results?
What is (-1)^2.5, of for that matter, (-1)^2.85? (Be careful, as it is not the same as -1^2.5.)
When you raise a negative number to a non-integer power, there will be no real solutions. Then you try to take the cos of a complex input. Again, it is usually complex, but NOT always so.
You should see that the results for the non-negative values raised to the 2.5 power had a zero imaginary part. So they were indeed real.
Ok, so how about the results for negative x? This depends on the power you raised them to.
The cosine function maps values with a ZERO real part and non-zero imaginary part into real numbers. This is easy to show. One identity for the cosine function is:
cos(x) = (exp(i*x) + exp(-i*x))/2
This is valid for any x, real or complex. So when x is purely imaginary, thus can be written as x = i*y, our identity reduces to
cos(i*y) = (exp(-y) + exp(y))/2 = cosh(y)
So the cosine of a purely imaginary input is purely real, and since cosh(y) is greater than or equal to 1 for all inputs, we will expect to see a result that is >= 1.
If we have
x = -1:.5:1;
and then raise x to the 2.5 power, we get:
x.^2.5
ans =
0 + 1i 0 + 0.17678i 0 + 0i 0.17678 + 0i 1 + 0i
I would point out that in your question, you show the command window display NOT for the cosine, thus cos(x.^2.5), but that is the display for simply x.^2.5.
cos(x.^2.5)
ans =
1.5431 1.0157 1 0.98442 0.5403
So the first two elements, even though they had imaginary parts, produced real results. Again, the cosine function maps complex inputs on the imaginary axis to real numbers, although they will be outside the range [-1,1].
Now see what happens when you raise x to a different fractional power?
x.^2.85
ans =
-0.89101 + 0.45399i -0.12358 + 0.062967i 0 + 0i 0.1387 + 0i 1 + 0i
As expected, the first two elements had a no-zero real part. So now when we form the cos, we get complex results.
cos(x.^2.85)
ans =
0.69453 + 0.36532i 0.99434 + 0.0077667i 1 + 0i 0.9904 + 0i 0.5403 + 0i
So all of this is exactly as expected.

Products

Community Treasure Hunt

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

Start Hunting!