MATLAB: How to combine numbers and symbols

MATLABsymbolic

While calculation some complex expressions with symbols and numbers, Matlab appears to be a weak tool to extract later Real and Imag part…
I have an easy question..
Let's assume we have an expression
syms a real
and I want to calculate – 4*a
how "4" will be treated here? and "4*a"

Best Answer

If you state when you define the symbolic variable a that it is real, Symbolic Math Toolbox can take advantage of that information.
>> syms a real
>> real(4*a)
ans =
4*a
>> imag(4*a)
ans =
0
Note that even though you've said a is real, the result of operating on a may have a non-zero imaginary part under certain circumstances.
>> real(sqrt(a))
ans =
real(a^(1/2))
>> imag(sqrt(a))
ans =
imag(a^(1/2))
Consider substituting a = 4 into sqrt(a). The result of that calculation has zero as its imaginary part. But if you substitute a = -1, that has a non-zero imaginary part.
>> imag(subs(sqrt(a), a, 4))
ans =
0
>> imag(subs(sqrt(a), a, -1))
ans =
1
If you were to tell Symbolic Math Toolbox that not only is a real but it is also positive, the toolbox can take into account that information.
>> syms b positive
>> imag(sqrt(b))
ans =
0
Related Question