Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Difference between i and pi?

Subject: Difference between i and pi?

From: Joerg Buchholz

Date: 22 Jan, 2009 18:24:01

Message: 1 of 10

Matlab knows certain constants like the imaginary unit 'i' and circular constant 'pi' that you can use out of the box:

>> i^2

ans =

    -1

>> cos (pi)

ans =

    -1

If on the other hand you declare your own symbolic variables:

>> syms pi i

the former imaginary unit loses its mathematical properties (which is expected behavior):

>> i^2
 
ans =
 
i^2
 
But the circular constant seems to keep its mathematical properties (which seems to be inconsistent):

>> cos (pi)
 
ans =
 
-1

Any idea?

Subject: Difference between i and pi?

From: someone

Date: 22 Jan, 2009 19:15:07

Message: 2 of 10

"Joerg Buchholz" <buchholz@hs-bremen.de> wrote in message <gladk1$otr$1@fred.mathworks.com>...
> Matlab knows certain constants like the imaginary unit 'i' and circular constant 'pi' that you can use out of the box:
>
> >> i^2
>
> ans =
>
> -1
>
> >> cos (pi)
>
> ans =
>
> -1
>
> If on the other hand you declare your own symbolic variables:
>
> >> syms pi i
>
> the former imaginary unit loses its mathematical properties (which is expected behavior):
>
> >> i^2
>
> ans =
>
> i^2
>
> But the circular constant seems to keep its mathematical properties (which seems to be inconsistent):
>
> >> cos (pi)
>
> ans =
>
> -1
>
> Any idea?

% Interesting, must have something to do
% with the symbolic toolbox. I get:

>> clear all
>> cos(pi)

ans =

    -1

>> pi = 1

pi =

     1

>> cos(pi)

ans =

    0.5403

>

Subject: Difference between i and pi?

From: David

Date: 22 Jan, 2009 19:22:02

Message: 3 of 10

"Joerg Buchholz" <buchholz@hs-bremen.de> wrote in message <gladk1$otr$1@fred.mathworks.com>...
> Matlab knows certain constants like the imaginary unit 'i' and circular constant 'pi' that you can use out of the box:
>
> >> i^2
>
> ans =
>
> -1
>
> >> cos (pi)
>
> ans =
>
> -1
>
> If on the other hand you declare your own symbolic variables:
>
> >> syms pi i
>
> the former imaginary unit loses its mathematical properties (which is expected behavior):
>
> >> i^2
>
> ans =
>
> i^2
>
> But the circular constant seems to keep its mathematical properties (which seems to be inconsistent):
>
> >> cos (pi)
>
> ans =
>
> -1
>
> Any idea?

perhaps it is more an inconsistency in how it is handled by various functions... for instance after doing syms i pi the results of i^2 and pi^2 both give symbolic answers. but cos(i) gives a symbolic where cos(pi) gives a number... but exp(pi) and exp(i) both give symbolic responses.

Subject: Difference between i and pi?

From: Joerg Buchholz

Date: 22 Jan, 2009 19:53:01

Message: 4 of 10

"David" <dave@bigcompany.com> wrote in message
:
> perhaps it is more an inconsistency in how it is handled by various functions... for instance after doing syms i pi the results of i^2 and pi^2 both give symbolic answers. but cos(i) gives a symbolic where cos(pi) gives a number... but exp(pi) and exp(i) both give symbolic responses.

I do not agree. cos(pi) is numeric in R2007b, R2008b, and R2009a:

>> syms pi i
>> cos (pi)
 
ans =
 
-1
 
 
>> whos
  Name Size Bytes Class Attributes

  ans 1x1 128 sym
  i 1x1 126 sym
  pi 1x1 128 sym

Subject: Difference between i and pi?

From: Joerg Buchholz

Date: 22 Jan, 2009 20:12:01

Message: 5 of 10

"Joerg Buchholz" <buchholz@hs-bremen.de> wrote in message <glaiqt$bcm$1@fred.mathworks.com>...
> "David" <dave@bigcompany.com> wrote in message
> :
> > perhaps it is more an inconsistency in how it is handled by various functions... for instance after doing syms i pi the results of i^2 and pi^2 both give symbolic answers. but cos(i) gives a symbolic where cos(pi) gives a number... but exp(pi) and exp(i) both give symbolic responses.
>
> I do not agree. cos(pi) is numeric in R2007b, R2008b, and R2009a:
>
> >> syms pi i
> >> cos (pi)
>
> ans =
>
> -1
>
>
> >> whos
> Name Size Bytes Class Attributes
>
> ans 1x1 128 sym
> i 1x1 126 sym
> pi 1x1 128 sym
>

Sorry! It should read: "cos(pi) is symbolic in R2007b, R2008b, and R2009a:"

Subject: Difference between i and pi?

From: tristram.scott@ntlworld.com (Tristram Scott)

Date: 23 Jan, 2009 10:38:02

Message: 6 of 10

Joerg Buchholz <buchholz@hs-bremen.de> wrote:
> "Joerg Buchholz" <buchholz@hs-bremen.de> wrote in message <glaiqt$bcm$1@fred.mathworks.com>...
>> "David" <dave@bigcompany.com> wrote in message
>> :
>> > perhaps it is more an inconsistency in how it is handled by various functions... for instance after doing syms i pi the results of i^2 and pi^2 both give symbolic answers. but cos(i) gives a symbolic where cos(pi) gives a number... but exp(pi) and exp(i) both give symbolic responses.
>>
>> I do not agree. cos(pi) is numeric in R2007b, R2008b, and R2009a:
>>
>> >> syms pi i
>> >> cos (pi)
>>
>> ans =
>>
>> -1
>>
>>
>> >> whos
>> Name Size Bytes Class Attributes
>>
>> ans 1x1 128 sym
>> i 1x1 126 sym
>> pi 1x1 128 sym
>>
>
> Sorry! It should read: "cos(pi) is symbolic in R2007b, R2008b, and R2009a:"

I am not sure what the problem is with this behaviour. If you went to the
underlying symbolic math engine (Maple or whatever is used now), and
presented it with an expression involving cos(pi), it would happily
simplify that to -1, just as you would do on a piece of paper.

If you had an expression involving cos(x), then that would be left as
symbolic, again, just as you would do on a piece of paper.

I don't have the symbolic toolbox, so can't verify this, but from what you
have said I think MATLAB is doing the correct things with the classes here.
The result in ans is reported to be symbolic, with a value of -1. If you
want it as a numeric value, ask for the numeric value of the symbolic
answer. If you want to contnue to use it as a symbolic value, then do so.


--
Dr Tristram J. Scott
Energy Consultant

Subject: Difference between i and pi?

From: Joerg Buchholz

Date: 23 Jan, 2009 11:50:05

Message: 7 of 10

tristram.scott@ntlworld.com (Tristram Scott) wrote in message
:
> I am not sure what the problem is with this behaviour. If you went to the
> underlying symbolic math engine (Maple or whatever is used now), and
> presented it with an expression involving cos(pi), it would happily
> simplify that to -1, just as you would do on a piece of paper.
>
> If you had an expression involving cos(x), then that would be left as
> symbolic, again, just as you would do on a piece of paper.
>
> I don't have the symbolic toolbox, so can't verify this, but from what you
> have said I think MATLAB is doing the correct things with the classes here.
> The result in ans is reported to be symbolic, with a value of -1. If you
> want it as a numeric value, ask for the numeric value of the symbolic
> answer. If you want to contnue to use it as a symbolic value, then do so.
>
>
> --
> Dr Tristram J. Scott
> Energy Consultant


Tristram,
my last post was just a response to David who said that

syms i pi
cos(pi)

gives a numeric response. I showed that cos(pi) gives a symbolic response if pi has been declared symbolic.

The original problem was:
If you declare 'i' and 'pi' symbolic in Matlab (syms i pi), 'i' becomes a new variable that loses the properties of the imaginary unit (i^2, ans=i^2), but 'pi' keeps the properties of the circular constant (cos(pi), ans=-1). I feel like this different behavior of 'i' and 'pi' is unexpected and inconsistent.

Using your own argumentation: If you asked any CAS for the square of the imaginary unit it would happily respond with '-1'. But if you declared a new symbolic variable 'i' in Matlab and square it, Matlab/Maple/MuPAD would not simplify the expression. This behavior seems to be inconsistent compared to the behavior of cos(pi) described above.

Subject: Difference between i and pi?

From: tristram.scott@ntlworld.com (Tristram Scott)

Date: 26 Jan, 2009 10:38:48

Message: 8 of 10

Joerg Buchholz <buchholz@hs-bremen.de> wrote:
> tristram.scott@ntlworld.com (Tristram Scott) wrote in message
>
> Using your own argumentation: If you asked any CAS for the square of the
> imaginary unit it would happily respond with '-1'. But if you declared a
> new symbolic variable 'i' in Matlab and square it, Matlab/Maple/MuPAD would
> not simplify the expression. This behavior seems to be inconsistent
> compared to the behavior of cos(pi) described above.
>
>

[Thinking outloud...]

Is this perhaps the subtle idea that we seldom use pi as anything other
than the circular constant (although I have often found it representing
price), but we often use i for things other than sqrt(-1), and indeed
engineers in particular will usually use j = sqrt(-1) instead.

Do you just need to tell the CAS that as well as being symbolic, i is
defined to be sqrt(-1)?

syms i
i = sqrt(-1)

Or whatever the equivalent syntax is with the symbolic toolbox.

--
Dr Tristram J. Scott
Energy Consultant

Subject: Difference between i and pi?

From: us

Date: 26 Jan, 2009 11:37:21

Message: 9 of 10

"Joerg Buchholz"
> If you declare 'i' and 'pi' symbolic in Matlab (syms i pi), 'i' becomes a new variable that loses the properties of the imaginary unit (i^2, ans=i^2), but 'pi' keeps the properties of the circular constant (cos(pi), ans=-1). I feel like this different behavior of 'i' and 'pi' is unexpected and inconsistent...

i agree...
moreover, this adds to the confusion...

     syms pi PI Pi;

     which pi; % pi is a variable
     which PI; % PI is a variable
     which Pi; % Pi is a variable

     r=cos(pi) % r = -1
     r=cos(PI) % r = -1
     r=cos(Pi) % r = cos(Pi) !

     r=cos(pi*pi) % r = cos(pi^2)
     r=cos(PI*pi) % r = cos(pi^2)
     r=cos(PI*PI) % r = cos(pi^2) !
     r=cos(Pi*Pi) % r = cos(Pi^2) !

     r=cos(pi+pi) % r = 1
     r=cos(PI+pi) % r = 1
     r=cos(PI+PI) % r = 1
     r=cos(pi+Pi) % r = -cos(Pi)
     r=cos(Pi+Pi) % r = cos(2*Pi)

us

Subject: Difference between i and pi?

From: Joerg Buchholz

Date: 26 Jan, 2009 14:21:01

Message: 10 of 10

"us " <us@neurol.unizh.ch> wrote in message <glk79h$9l2$1@fred.mathworks.com>...
> "Joerg Buchholz"
> > If you declare 'i' and 'pi' symbolic in Matlab (syms i pi), 'i' becomes a new variable that loses the properties of the imaginary unit (i^2, ans=i^2), but 'pi' keeps the properties of the circular constant (cos(pi), ans=-1). I feel like this different behavior of 'i' and 'pi' is unexpected and inconsistent...
>
> i agree...
> moreover, this adds to the confusion...
>
> syms pi PI Pi;
>
> which pi; % pi is a variable
> which PI; % PI is a variable
> which Pi; % Pi is a variable
>
> r=cos(pi) % r = -1
> r=cos(PI) % r = -1
> r=cos(Pi) % r = cos(Pi) !
>
> r=cos(pi*pi) % r = cos(pi^2)
> r=cos(PI*pi) % r = cos(pi^2)
> r=cos(PI*PI) % r = cos(pi^2) !
> r=cos(Pi*Pi) % r = cos(Pi^2) !
>
> r=cos(pi+pi) % r = 1
> r=cos(PI+pi) % r = 1
> r=cos(PI+PI) % r = 1
> r=cos(pi+Pi) % r = -cos(Pi)
> r=cos(Pi+Pi) % r = cos(2*Pi)
>
> us

Seems like pi (Matlab's circular constant) and PI (MuPAD's circular constant) cannot be used as propertyless symbolic variables. :-(

Tags for this Thread

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.

Contact us