Results are Fractional and Imaginary
6 次查看(过去 30 天)
显示 更早的评论
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))
0 个评论
回答(3 个)
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.
0 个评论
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!