How to separate real and imaginary components from a given expression in Matlab?

37 views (last 30 days)
Hello everyone I have a expression and I need to separate the real and imaginary components of the expression. I have written a simple code using symbolic math toolbox. The code is:
clc
syms X1 X2 XC RL
Zin=((X1*XC)-1i*(X2*RL))/(RL+1i*(X1*X2*XC));
A=real(Zin)
B=imag(Zin)
Can anyone please point out whats wrong with it or maybe another way to do this right.
Thank You.

Accepted Answer

Steven Lord
Steven Lord on 23 Jan 2023
syms X1 X2 XC RL
Zin=((X1*XC)-1i*(X2*RL))/(RL+1i*(X1*X2*XC));
A=real(Zin)
A = 
B=imag(Zin)
B = 
What leads you to believe something is wrong? For general X1, X2, XC, and RL (which could be complex) that's the correct result. If you were to assume that they were real you get something a bit different.
syms X1 X2 XC RL real
A = real(Zin)
A = 
B = imag(Zin)
B = 
  5 Comments
Walter Roberson
Walter Roberson on 23 Jan 2023
We are not told that X1 X2 XC RL are real valued.
Suppose we had the expression 1i * X1 * X2 and we do not know that X1 and X2 are real. If we take real(1i * X1 * X2) is it correct that would be the same as X1*X2 ? Obviously not: real(1i * 3 * 5) = real(15i) = 0, which is not the same as real(3*5) = real(15) = 15. Should we guess then that real(1i * X1 * X2) = real(conj(X1 * X2)) ?
format long g
A = (3+4i) * (5+6i)
A =
-9 + 38i
real(1i * A)
ans =
-38
conj(A)
ans =
-9 - 38i
real(conj(A))
ans =
-9
real(conj(3+4i) * conj(5+6i))
ans =
-9
imag(3+4i) * imag(5+6i)
ans =
24
No. However it is correct that real(1i*A) == imag(A). Whether you write imag(A) or real(1i*A) is a matter of taste.
Steven Lord
Steven Lord on 23 Jan 2023
What's the real part of x+1i*y? If you said x you'd be incorrect in the general case, as shown by this example that substitutes numbers for x and y:
x = 1+2i;
y = 3+4i;
z = x + 1i*y
z = -3.0000 + 5.0000i
RE = real(z)
RE = -3
IM = imag(z)
IM = 5
RE == x % false
ans = logical
0

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!