|
In article <g4kl0p$k1c$1@fred.mathworks.com>,
Emeka Obe <obeway@gmail.com> wrote:
>I experience a problem getting the real and imaginary parts
>of a complex expression in symbolic form.
>For example I have this code and will like to know how to
>get the real and imaginary parts:
>clear; clc;
>syms x y n
>z=n*cos(x)+i*y*sin(x);
>r = real(z);
>The expressions I expected to get were:
>r = n*cos(x) and
>But instead I get:
>r =
>1/2*n*cos(x)+1/2*i*y*sin(x)+1/2*conj(n*cos(x)+i*y*sin(x))
The output you received is correct, and your expected output is not.
Your variable y is symbolic, so the program is to produce the general
output for all possible values the symbol y could assume. That
output is for real() is *not* n*cos(x) . Simple proof: suppose y was 5*i .
real(n*cos(x) + i*i*5*sin(x)) is real(n*cos(x) - 5*sin(x))
which is different than your proposed answer of n*cos(x) .
The hidden assumption in your proposed expression is that none of
n, x, or y are complex: if any of them are complex then your
proposed output is incorrect. If that assumption is in fact valid
for your problem domain, then in order to have your proposed output
appear automatically, tell matlab about those assumptions.
I do not have the symbolic toolbox, so I do not know what the
syntax is for providing assumptions. In the base Maple engine
which underlies the symbolic toolbox, the way I would code would be
(in Maple code!):
z := n*cos(x) + I*y*sin(x);
r := Re(z) assuming real;
m := Im(z) assuming real;
--
"There is no greater calling than to serve your fellow men.
There is no greater contribution than to help the weak.
There is no greater satisfaction than to have done it well."
-- Walter Reuther
|