how drop near zero imaginary terms from symbolic result?

7 Ansichten (letzte 30 Tage)
Russell
Russell am 30 Aug. 2022
Kommentiert: Dyuman Joshi am 21 Sep. 2023
I'm doing some symbolic matrix manipulations with complex valued matrices. Some of my results are as below.
Zaa*(1 + 3i/36028797018963968) + Zab*(1 + 3i/36028797018963968)
Is there a way to tell Matlab to ignore those near zero imaginary results so the answer will be reported simply as,
Zaa + Zab
  4 Kommentare
Jamie Steel
Jamie Steel am 30 Aug. 2022
Bearbeitet: Jamie Steel am 30 Aug. 2022
@John D'Errico Ah I see, perhaps I misinterpreted the expression - I assumed the symbols were only Zaa and Zab, and that the following complex number z was some numerical result multiplying them. That is horribly messy though! My experience with symbolic manipulation is certainly limited enough such that I've never run into trouble like that thankfully
Russell
Russell am 30 Aug. 2022
Thank you. That is better than nothing, but is ugly.
I tried the following and it sorta works too. Wish better way to do it directly.
vpa(term,2)
ans =
Zaa*(1.0 + 8.3e-17i) + Zab*(1.0 + 8.3e-17i)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Moksh
Moksh am 21 Sep. 2023
Hi Russell,
I understand that you are attempting to eliminate the imaginary part in the intermediate term if it is close to zero, as shown in the example provided.
You can utilize the "abs" and "imag" functions in MATLAB to obtain the magnitude of the imaginary part. By comparing this magnitude with a threshold value, which can be adjusted as needed, you can determine if the imaginary part is sufficiently close to zero.
Here is an example code:
syms Zaa Zab
% Get the imaginary part in z
z = 1 + 3i/36028797018963968;
imag_z = imag(z);
tol = 1e-5;
% Ignore the imaginary part in z if it is near zero
if abs(imag_z) < tol
z = real(z);
end
% Define the main result
Z = Zaa * z + Zab * z;
disp(Z)
For more understanding about the above used functions, please refer to the following documentation:
Hope this information helps!
Best Regards,
Moksh Aggarwal
  1 Kommentar
Dyuman Joshi
Dyuman Joshi am 21 Sep. 2023
You are treating z as a numeric variable, which is not the case here as OP is dealing with symbolic variables.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by