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:
if d = x^2, and x=[0,0;1,0] then why is eval(d) not equal to x^2

Subject: if d = x^2, and x=[0,0;1,0] then why is eval(d) not equal to x^2

From: Don Robison

Date: 27 Mar, 2010 19:07:26

Message: 1 of 10

Hello my name is Don Robison. I am having a problem with the way matlab evaluates expressions. It seems if I put a polynomial on the command prompt and I have declared the variables to be matrices the evaluation will be fine, but if I declare the polynomial to be a symbol then try to evaluate the symbolic expression instead of a literal they are not the same thing.

Is there something other than eval I should be using to plug the matrices into the symbolic expression? eval seems to not be evaluating the exponent of the matrix.

Subject: if d = x^2, and x=[0,0;1,0] then why is eval(d) not equal to x^2

From: us

Date: 27 Mar, 2010 19:18:04

Message: 2 of 10

Don Robison <joe_blow_281@hotmail.com> wrote in message <1111757053.455267.1269716876794.JavaMail.root@gallium.mathforum.org>...
> Hello my name is Don Robison. I am having a problem with the way matlab evaluates expressions. It seems if I put a polynomial on the command prompt and I have declared the variables to be matrices the evaluation will be fine, but if I declare the polynomial to be a symbol then try to evaluate the symbolic expression instead of a literal they are not the same thing.
>
> Is there something other than eval I should be using to plug the matrices into the symbolic expression? eval seems to not be evaluating the exponent of the matrix.

CSSM needs to see the code...

us

Subject: if d = x^2, and x=[0,0;1,0] then why is eval(d) not equal to

From: Don Robison

Date: 27 Mar, 2010 19:28:58

Message: 3 of 10

>> syms x
>> d = x^2
d =
x^2
>> x=[0,0;1,0]
x =
     0 0
     1 0
>> x^2
ans =
     0 0
     0 0
>> eval(d)
ans =
     0 0
     1 0
>>

Subject: if d = x^2, and x=[0,0;1,0] then why is eval(d) not equal to

From: us

Date: 27 Mar, 2010 19:58:07

Message: 4 of 10

Don Robison <joe_blow_281@hotmail.com> wrote in message <1817186076.455340.1269718178462.JavaMail.root@gallium.mathforum.org>...
> >> syms x
> >> d = x^2
> d =
> x^2
> >> x=[0,0;1,0]

note: X is reassigned to a 2x2 DOUBLE...

one of the solutions

     syms x;
     d=x^2;
     xd=[0,0;1,0];
     r=subs(d,'x',xd)
%{
% r =
     0 0
     1 0
%}

us

Subject: if d = x^2, and x=[0,0;1,0] then why is eval(d) not equal to

From: Don Robison

Date: 27 Mar, 2010 20:50:48

Message: 5 of 10

Hello us. This is Don Robison. Thank you for your help, but the code does not return x^2. I need to evaluate a symbolic expression such as x^i*y^i*z^k, and have the matrix multiplied by itself i,j,k times. The code you gave seems to just return xd without taking it to the second power.

the input must be a trinomial with any power, the output must be that a sum of matrices that have been multiplied by themselves as many times as the exponent in the term.

the output I am looking for in this particular instance would be [0,0;0,0] or xd^2.

Subject: if d = x^2, and x=[0,0;1,0] then why is eval(d) not equal to

From: ImageAnalyst

Date: 27 Mar, 2010 23:43:37

Message: 6 of 10

Do you mean to use .^ (dot caret) instead of ^ (caret) to take element-
by-element squaring? Otherwise without the dot, it's completely
different. Just checking....

Subject: if d = x^2, and x=[0,0;1,0] then why is eval(d) not equal to

From: Matt J

Date: 27 Mar, 2010 23:52:08

Message: 7 of 10

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <23cb4247-0a3e-47af-90c6-7889c83e2524@30g2000yqi.googlegroups.com>...
> Do you mean to use .^ (dot caret) instead of ^ (caret) to take element-
> by-element squaring? Otherwise without the dot, it's completely
> different. Just checking....
=============

That doesn't explain the discrepancy between x^2 and eval(d), though. They should be the same regardless of which kind of exponentiation is used...

Subject: if d = x^2, and x=[0,0;1,0] then why is eval(d) not equal to

From: Roger Stafford

Date: 27 Mar, 2010 23:54:06

Message: 8 of 10

Don Robison <joe_blow_281@hotmail.com> wrote in message <1396842502.455793.1269723085870.JavaMail.root@gallium.mathforum.org>...
> Hello us. This is Don Robison. Thank you for your help, but the code does not return x^2. I need to evaluate a symbolic expression such as x^i*y^i*z^k, and have the matrix multiplied by itself i,j,k times. The code you gave seems to just return xd without taking it to the second power.
>
> the input must be a trinomial with any power, the output must be that a sum of matrices that have been multiplied by themselves as many times as the exponent in the term.
>
> the output I am looking for in this particular instance would be [0,0;0,0] or xd^2.

  Actually [0,0;0,0] is the correct answer! The matrix product of that matrix by itself is all zeros.

Roger Stafford

Subject: if d = x^2, and x=[0,0;1,0] then why is eval(d) not equal to

From: Matt J

Date: 28 Mar, 2010 00:38:05

Message: 9 of 10

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hom5qu$r3l$1@fred.mathworks.com>...

> Actually [0,0;0,0] is the correct answer! The matrix product of that matrix by itself is all zeros.
>


Yes, however, that is not the output that is obtained...

Subject: if d = x^2, and x=[0,0;1,0] then why is eval(d) not equal to

From: Don Robison

Date: 28 Mar, 2010 02:49:44

Message: 10 of 10

I found something that kinda works, if I change the situation a bit. I just create an m-file called x, and return the matrix from the function x. This gets around the problem of the internal subs function being unable to substitute a matrix in place of a symbolic variable. Of course no symbolic operations can be preformed on the expression anymore, because x is a function now instead of a symbolic variable.

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