How to combine numbers and symbols?

2 ビュー (過去 30 日間)
Ivan
Ivan 2019 年 10 月 28 日
回答済み: Steven Lord 2019 年 10 月 28 日
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"

採用された回答

Steven Lord
Steven Lord 2019 年 10 月 28 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by