Why do different solutions arise from these seemingly identical approaches to a typing these numbers in? If I exponentiate a number as a variable, it becomes imaginary, if I type in the number directly, it stays rational?
3 views (last 30 days)
Show older comments
Adam Goldsmith
on 19 Jun 2017
Answered: Steven Lord
on 19 Jun 2017
I'm probably wording this poorly, but this isn't making any sense to me.
I have a set of variables that I've defined as:
K = 1.04 - r
rB2 = ((C0 + C1 * ((log(dt) - C2) / (log(1/T(i)) - C3))^(1/C4)) + 1)^-1
rc = ((rB2 - r)/(1 - r0))^K
r, C0 - C4 are are all real floating constants with no imaginary portion, T is simulated temperature at step i and dt is timestep. rB2 returns a number with no imaginary constant.
For my numbers, as an example, rB2 = 1.3e-33, r = 0.79, and K = 0.25.
((1.3e-33 - 0.79)/(1-0.79)) = -3.7619, and -3.7619^0.25 = -1.3927, right?
....except when it doesn't! Sometimes, MATLAB returns an imaginary number, 0.9848 + 0.9848i, depending on how I type the equation in. For example, here's some command line output:
>> ((rB2 - r)/(1-r))
ans =
-3.7619
>> ans^K
ans =
0.9848 + 0.9848i
>> -3.7619^K
ans =
-1.3927
what the heck?? if I state the number -3.7619 as a variable (any variable, not just 'ans'), it comes out imaginary when exponentiated by K... but not if I type the number in by hand. Also,
>> ((1.3e-33 - 0.79)/(1-0.79))
ans =
-3.7619
>> ((1.3e-33 - 0.79)/(1-0.79))^0.25
ans =
0.9848 + 0.9848i
So, what is the difference between these two seemingly identical approaches of typing the numbers in?? And how do I get a script to generate a value rc that isn't imaginary without typing it in myself every iteration of the script?
Thanks in advance.
0 Comments
Accepted Answer
Steven Lord
on 19 Jun 2017
By operator precedence, the power operator ^ is computed first then the unary minus operator - is applied. So these two expressions are the same:
x1 = -3.7619^K
x2 = -(3.7619^K)
In both those cases, you're raising a positive number to a fractional power which results in a real result then that real result is being negated. If you want to raise the negative number to a power:
x3 = (-3.7619)^K
Raising a negative number to a fractional power can result in a complex number, even when there is a real number that is also a root of the negative number. See the first example in the documentation for the nthroot function for more information.
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!