Calculation of abs() and angle() of a symbolic expression - strange behaviour

조회 수: 1 (최근 30일)
Hi.
I have come across some strange behaviour of the abs() and angle() function in symbolic calculations:
The first result is as expected. The second should be calculated with equal ease as
but matlab just rewrites the expression using the | | sign.
Similar behaviour applies to the angle function
Why is this happening and how to force matlab to do the calculation also in the second case?
Marek
  댓글 수: 2
Govind KM
Govind KM 2024년 12월 10일
Hi Marek,
I am able to reproduce this in MATLAB R2024b for abs but not for angle. Do you have a similiar example for the same behaviour using angle?
Marek
Marek 2024년 12월 10일
Hi,
thanks for your interest in the topic.
Here is an example:
The correct result should be -atan2(LR,R+1), yet matlab again rewrites the expression
best
Marek

댓글을 달려면 로그인하십시오.

채택된 답변

Govind KM
Govind KM 2024년 12월 10일
편집: Walter Roberson 2024년 12월 10일
Hi Marek,
When a symbolic expression is defined, it undergoes some internal simplification before being displayed. For some cases, additional simplification steps can be required to get a simpler result. The simplify method can be used to perform further simplication on a symbolic expression. Here is sample code for the provided example:
syms R real positive
syms L real positive
%Specify the number of simplification steps using the 'Steps' option
%Use the 'All' option to return all equivalent results
exp1=simplify(abs(R+1i*L*R+1),'All',true,'Steps',10), disp(char(exp1))
[((R + 1)^2 + L^2*R^2)^(1/2); (2*R + R^2 + L^2*R^2 + 1)^(1/2)]
exp2=simplify(abs(R+1i*L*R+L),'All',true,'Steps',10), disp(char(exp2))
[abs(L + R + L*R*1i); ((L + R)^2 + L^2*R^2)^(1/2); (L^2 + R^2 + L^2*R^2 + 2*L*R)^(1/2)]
The desired result can be selected as needed:
desiredExp=exp2(2), disp(char(desiredExp))
((L + R)^2 + L^2*R^2)^(1/2)
The PreferReal option can be used to favor the selection of expressions containing real values over the ones containing complex values:
simplify(abs(R+1i*L*R+L),'Steps',10,'Criterion','PreferReal'), disp(char(ans))
((L + R)^2 + L^2*R^2)^(1/2)
A similar approach can be followed for the angle function.
More details on the simplify method and its options can be found in the following documentation:
Hope this is helpful!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by