Thread Subject: Real and imaginary parts of symbolic expression

Subject: Real and imaginary parts of symbolic expression

From: Emeka Obe

Date: 4 Jul, 2008 07:55:05

Message: 1 of 3

Dear All,

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);
m = imag(z);

The expressions I expected to get were:
r = n*cos(x) and
m = y*sin(x).

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))
 
 and

m =
 
-1/2*i*(n*cos(x)+i*y*sin(x)-conj(n*cos(x)+i*y*sin(x)))

Though these are correct, I cannot they are more
complicated and I cannot simplify them to obtain the
answers I listed above. Can anyone help me out of this?
I need to have a way of obtaining the real and imaginary
parts of any arbitrary complex expression is the actual
form without having longer expressions.

Thanks

ES




Subject: Real and imaginary parts of symbolic expression

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 4 Jul, 2008 17:21:42

Message: 2 of 3

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

Subject: Real and imaginary parts of symbolic expression

From: Steven Lord

Date: 8 Jul, 2008 14:49:31

Message: 3 of 3


"Walter Roberson" <roberson@ibd.nrc-cnrc.gc.ca> wrote in message
news:g4lm76$q9h$1@canopus.cc.umanitoba.ca...
> In article <g4kl0p$k1c$1@fred.mathworks.com>,
> Emeka Obe <obeway@gmail.com> wrote:

*snip*

> 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!):

The syntax for assuming that x, y, and n are real symbolic variables is:

syms x y n real

--
Steve Lord
slord@mathworks.com


Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
symbolic math Emeka Obe 4 Jul, 2008 04:00:08
real Emeka Obe 4 Jul, 2008 04:00:08
imag Emeka Obe 4 Jul, 2008 04:00:08
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com