Results are Fractional and Imaginary

2 ビュー (過去 30 日間)
Matthew
Matthew 2014 年 11 月 12 日
回答済み: Walter Roberson 2017 年 12 月 16 日
Two part question...I have the following simple code.
1) When y=f(-2) is calculated and shown in the Command Window, it's shown as a fractional equation instead of a numeric result. Why is this? Is it standard Matlab convention? Can a preference be changed to instead display a numeric result? It's often easier to see the number instead.
2) Piggybacking first question...I read somewhere that using double() will force the software to display a numeric result. Why does the Command Window show an imaginary component (i) in the result when I calculate the derivate at -2? By hand, the derivate and its value at -2 are very straightforward and has no imaginary component; I get -0.7391, which doesn't correlate with Matlab's answer at all. Why does the software display i? Can a preference be changed to show the actual real number? How does this complex display translate to the actual numeric value?
syms x
f(x)=(x^(2/3)+x^(1/3))
y=f(-2)
%Take derivate of f(x)
df=diff(f)
%Find value of derivate at -2
double(df(-2))

回答 (3 件)

Roger Stafford
Roger Stafford 2014 年 11 月 12 日
編集済み: Roger Stafford 2014 年 11 月 12 日
Be aware that taking cube root is actually equivalent to solving a cubic equation and thus always has three possible roots - that is, y = x^(1/3) is equivalent to x^3-y = 0 which has three roots for x. Hence (-2)^(1/3) which is equivalent to solving x^3+2 = 0 has the three roots of which two are complex-valued:
-1.2599
0.6300 + 1.0911*i
0.6300 - 1.0911*i
and if you write (-2)^(1/3), matlab will in fact give the second of these as its answer rather than the first.

Matthew
Matthew 2014 年 11 月 18 日
I found my answer to #2. The nthroot command forces the software to retrieve the value of the real root, instead of the imaginary roots.
I'm still unsure about my first question though. Anyone have any more insight to my first question?

Walter Roberson
Walter Roberson 2017 年 12 月 16 日
"1) When y=f(-2) is calculated and shown in the Command Window, it's shown as a fractional equation instead of a numeric result. Why is this? Is it standard Matlab convention? Can a preference be changed to instead display a numeric result? It's often easier to see the number instead."
You used syms x which is the Symbolic Toolbox, which is designed to attempt to find exact closed-form solutions rather than approximations. The decimal values you were expecting are approximations rather than exact solutions, and so are not calculated when it can find an exact solution.
To see the decimal approximation instead, then use vpa(y) or double(y) .
There is no setting to have the output automatically converted to decimal.

Community Treasure Hunt

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

Start Hunting!

Translated by