Derivative of a real function becomes complex

3 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2018-5-2
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.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-5-2
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 个评论
Roberto Cipollone
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

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by