Derivative of a real function becomes complex

5 ビュー (過去 30 日間)
Roberto Cipollone
Roberto Cipollone 2018 年 5 月 2 日
コメント済み: Roberto Cipollone 2018 年 5 月 3 日
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
Roberto Cipollone 2018 年 5 月 3 日
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