How to tell Matlab to give real cube roots instead of complex ones?
이전 댓글 표시
I am using symbolic math toolbox (Matlab2011b) to do some geometric computations to derive an expression for a geometric length. At some point of my calculations some cube roots are introduced and unfortunately Matlab seems to automatically simplify terms like (-1)^(1/3) to yield 0.5000 + 0.8660i.
I realize this is a correct solution, however i want my result to be real - is there a way to tell Matlab to always return the real cube root?
These imaginary ones are messing up my expressions...
(Also i wonder why matlab chooses to consider one of two complex roots instead of giving all three solutions or the real one...)
채택된 답변
추가 답변 (4개)
Hi,
seems more like MATLAB is converting your symbolic expression into a complex double value. So what are you doing with the symbolic expression? Or better, what is your actual code?
댓글 수: 6
Friedrich
2013년 3월 27일
which MATLAB version are you using? For me in 13a MATLAB doesnt add imaginary terms:
>> syms ax ay az nx ny nz alpha dx dy ho real;
>> (1/(az*dx*dy*(2*ax*nx + dx*nx + 2*ay*ny + dy*ny + 2*az*nz)*sqrt(nx^2 + ny^2 + nz^2)))*(-2*alpha*ax^2*az*dx*dy*nx^2 - alpha*ax*az*dx^2*dy*nx^2 - 4*alpha*ax*ay*az*dx*dy*nx*ny - alpha*ay*az*dx^2*dy*nx*ny - alpha*ax*az*dx*dy^2*nx*ny - 2*alpha*ay^2*az*dx*dy*ny^2 - alpha*ay*az*dx*dy^2*ny^2 - 4*alpha*ax*az^2*dx*dy*nx*nz - alpha*az^2*dx^2*dy*nx*nz - 4*alpha*ay*az^2*dx*dy*ny*nz - alpha*az^2*dx*dy^2*ny*nz - 2*alpha*az^3*dx*dy*nz^2 - (-1)*az^(2/3)*dx^(2/3)*dy^(2/3)*(ax*nx + ay*ny + az*nz)^(1/3)*(2*ax*nx + dx*nx + 2*ay*ny + dy*ny + 2*az*nz)^(2/3)*(nx^2 + ny^2 + nz^2)^(1/3)* ((2*dx*dy*ho*(3*az^2 - 3*az*ho + ho^2)*(ax*nx + dx*nx + ay*ny + az*nz)*(ax*nx + ay*ny + dy*ny + az*nz)*(ax*nx + dx*nx + ay*ny + dy*ny + az*nz))/az^2 + alpha^3*az*dx*dy*(ax*nx + ay*ny + az*nz)^2*(2*ax*nx + dx*nx + 2*ay*ny + dy*ny + 2*az*nz))^(1/3))
ans =
-(2*alpha*az^3*dx*dy*nz^2 + alpha*ax*az*dx^2*dy*nx^2 + 2*alpha*ax^2*az*dx*dy*nx^2 + alpha*ay*az*dx*dy^2*ny^2 + 2*alpha*ay^2*az*dx*dy*ny^2 + alpha*az^2*dx^2*dy*nx*nz + alpha*az^2*dx*dy^2*ny*nz + alpha*ax*az*dx*dy^2*nx*ny + 4*alpha*ax*az^2*dx*dy*nx*nz + alpha*ay*az*dx^2*dy*nx*ny + 4*alpha*ay*az^2*dx*dy*ny*nz - az^(2/3)*dx^(2/3)*dy^(2/3)*(alpha^3*az*dx*dy*(ax*nx + ay*ny + az*nz)^2*(2*ax*nx + 2*ay*ny + 2*az*nz + dx*nx + dy*ny) + (2*dx*dy*ho*(3*az^2 - 3*az*ho + ho^2)*(ax*nx + ay*ny + az*nz + dx*nx)*(ax*nx + ay*ny + az*nz + dy*ny)*(ax*nx + ay*ny + az*nz + dx*nx + dy*ny))/az^2)^(1/3)*(nx^2 + ny^2 + nz^2)^(1/3)*(ax*nx + ay*ny + az*nz)^(1/3)*(2*ax*nx + 2*ay*ny + 2*az*nz + dx*nx + dy*ny)^(2/3) + 4*alpha*ax*ay*az*dx*dy*nx*ny)/(az*dx*dy*(nx^2 + ny^2 + nz^2)^(1/2)*(2*ax*nx + 2*ay*ny + 2*az*nz + dx*nx + dy*ny))
Thomas
2013년 3월 27일
Friedrich
2013년 3월 27일
When using the corrected code I get an "i" part in the result:
-(2*alpha*az^3*dx*dy*nz^2 + alpha*ax*az*dx^2*dy*nx^2 + 2*alpha*ax^2*az*dx*dy*nx^2 + alpha*ay*az*dx*dy^2*ny^2 + 2*alpha*ay^2*az*dx*dy*ny^2 + alpha*az^2*dx^2*dy*nx*nz + alpha*az^2*dx*dy^2*ny*nz + alpha*ax*az*dx*dy^2*nx*ny + 4*alpha*ax*az^2*dx*dy*nx*nz + alpha*ay*az*dx^2*dy*nx*ny + 4*alpha*ay*az^2*dx*dy*ny*nz + 4*alpha*ax*ay*az*dx*dy*nx*ny + az^(2/3)*dx^(2/3)*dy^(2/3)*((3^(1/2)*i)/2 + 1/2)*(alpha^3*az*dx*dy*(ax*nx + ay*ny + az*nz)^2*(2*ax*nx + 2*ay*ny + 2*az*nz + dx*nx + dy*ny) + (2*dx*dy*ho*(3*az^2 - 3*az*ho + ho^2)*(ax*nx + ay*ny + az*nz + dx*nx)*(ax*nx + ay*ny + az*nz + dy*ny)*(ax*nx + ay*ny + az*nz + dx*nx + dy*ny))/az^2)^(1/3)*(nx^2 + ny^2 + nz^2)^(1/3)*(ax*nx + ay*ny + az*nz)^(1/3)*(2*ax*nx + 2*ay*ny + 2*az*nz + dx*nx + dy*ny)^(2/3))/(az*dx*dy*(nx^2 + ny^2 + nz^2)^(1/2)*(2*ax*nx + 2*ay*ny + 2*az*nz + dx*nx + dy*ny))
Thomas
2013년 3월 27일
Sriman
2023년 11월 1일
How to find in cubic root
Ruye Wang
2014년 4월 16일
0 개 추천
I have the same question. I need to evaluate a function in symbolic form, essentially like the following:
syms x;
f=x^(1/3);
subs(f,-1)
The output is one of the three roots: 0.5000 + 0.8660i
But what I need is the real cube root of x=-1 (which is -1).
I know I could get the real cube root if I explicitly hard code it like this:
nthroot(-1,3)
However, how do I do this in the form of symbolic function, which is needed in the program?
Steven Lord
2017년 9월 7일
0 개 추천
See this Answer for an explanation of why something like (-8)^(1/3) returns a complex result and an alternative function you can use.
댓글 수: 2
Jason Duvall
2022년 3월 13일
This link is no lonber available, but the issue persists. Does anyone have an update?
Walter Roberson
2022년 3월 13일
MATLAB defines the .^ operator on numeric values in terms of log and exp, and that will give you complex results for negative base numbers except for integer powers.
카테고리
도움말 센터 및 File Exchange에서 Common Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!