Derivative of a real function becomes complex

13 views (last 30 days)
Look at these expressions:
Even if every function and variable is real, the result is a complex number. Each step seems valid, but the sequence gives unexpected results. It seems to arise from the particular way it expresses the denominator of cos(atan2(y,x)).
Is the result valid? If not, how can I avoid this transformation?
sqrt(x^2 + y^2) = abs(x + i*y)
All this appears inside a symbolic expression (x and y are expressions too), so I can't manually rewrite them.
  1 Comment
Walter Roberson
Walter Roberson on 2 May 2018
When I test in Maple, I do not see the same difficulty by default, provided that I have put the assumption of 'real' in place like you did. However, in Maple if I hold off the assumption of real until after the diff() then I am able to replicate the problem.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 2 May 2018
I think the error is in the cos(atan2(y,x)) step generating the abs.
Even though for real values the expression abs(x+1i*y) is equivalent to sqrt(x^2+y^2), when you diff() the abs() version under the assumption that you might be working with complex values, you violate the branch-cut assumptions and get a complex branch cut, and when you substitute real values into that afterwards you are still on the complex branch cut.
The work-around is to
rewrite(ans, 'sqrt')
after the cos() and before the diff()
  1 Comment
Roberto Cipollone
Roberto Cipollone on 3 May 2018
Or maybe it is related to abs(.) being not an holonomic function. So the two expressions are not equivalent under differentiation. Strange behaviour.
Anyway, the workaround does perfectly the job. Thanks

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!