Main Content

real

Real part of complex number

Syntax

Description

real(z) returns the real part of z. If z is a matrix, real acts elementwise on z.

example

Examples

Compute Real Part of Numeric Inputs

Find the real parts of these numbers. Because these numbers are not symbolic objects, you get floating-point results.

[real(2 + 3/2*i), real(sin(5*i)), real(2*exp(1 + i))]
ans =
    2.0000         0    2.9374

Compute Real Part of Symbolic Inputs

Compute the real parts of the numbers converted to symbolic objects:

[real(sym(2) + 3/2*i), real(4/(sym(1) + 3*i)),  real(sin(sym(5)*i))]
ans =
[ 2, 2/5, 0]

Compute the real part of this symbolic expression:

real(2*exp(1 + sym(i)))
ans =
2*cos(1)*exp(1)

Compute Real Part of Symbolic Expressions

In general, real cannot extract the entire real parts from symbolic expressions containing variables. However, real can rewrite and sometimes simplify the input expression:

syms a x y
real(a + 2)
real(x + y*i)
ans =
real(a) + 2
 
ans =
real(x) - imag(y)

If you assign numeric values to these variables or specify that these variables are real, real can extract the real part of the expression:

syms a
a = 5 + 3*i;
real(a + 2)
ans =
     7
syms x y real
real(x + y*i)
ans =
x

Clear the assumption that x and y are real by recreating them using syms:

syms x y

Compute Real Part for Matrix Input

Find the real parts of the elements of matrix A:

syms x
A = [-1 + sym(i), sinh(x); exp(10 + sym(7)*i), exp(sym(pi)*i)];
real(A)
ans =
[             -1, real(sinh(x))]
[ cos(7)*exp(10),            -1]

Input Arguments

collapse all

Input, specified as a number, vector, matrix, or array, or a symbolic number, variable, array, function, or expression.

Tips

  • Calling real for a number that is not a symbolic object invokes the MATLAB® real function.

Alternatives

You can compute the real part of z via the conjugate: real(z)= (z + conj(z))/2.

Version History

Introduced before R2006a

See Also

| | | |