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:
Entering a symbolic vector and sum

Subject: Entering a symbolic vector and sum

From: Dean

Date: 29 Oct, 2010 01:46:03

Message: 1 of 3

Hi,

I am new to Matlab so please bear with me. I have two questions that you may be able to help with:

1. Firstly, I would like to enter a 5-element vector, X, consisting of symbolic elements. The symbolic elements will eventually be substituted with positive real values, later on in the code. I know that I can enter such a symbolic vector as follows:

>> sym('x1','positive');

For x1 to x5. Then

>> X = [x1 x2 x3 x4 x5]'
 
X =
 
 x1
 x2
 x3
 x4
 x5

This works fine, but I am wondering why I cannot use the method given in Matlab help, which states "To create a symbolic vector or a symbolic matrix A, use A = sym('A', [m n]) or A = sym('A', n).". When I do this, I get the following error:

>> X=sym('X',[5 1])
??? Error using ==> error
Formatted arguments cannot be non-scalar numeric matrices.

Error in ==> sym.sym>assumptions at 2180
    error('symbolic:sym:sym:errmsg1','Second argument %s not
    recognized.',a);

Error in ==> sym.sym>tomupad at 2148
        assumptions(S,x,a);

Error in ==> sym.sym>sym.sym at 114
            S.s = tomupad(x,a);

I am not sure where I am going wrong.

2. Secondly, I need to create symbolic sums. As a simple example, I would like Matlab to assign to variable S the sum from i=1 to 5, of X(i)*log(X(i)), where X(i) refers to element i of vector X. I know I can explicitly enter the whole formula. However, the sums get considerably more complicated (including sums of sums) and thus I would prefer not to have to enter every term. I don't know why the following doesn't work:

>> syms i
>> S = symsum(X(i)*log(X(i)),i,1,5)
??? Error using ==> mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a
double array.

If the input expression contains a symbolic variable, use the VPA function
instead.

Error in ==> sym.sym>sym.double at 927
            Xstr = mupadmex('mllib::double', S.s, 0);

Error in ==> sym.sym>privformatscalar at 2539
    x = double(x);

Error in ==> sym.sym>privformat at 2524
    s = privformatscalar(x);

Error in ==> sym.sym>sym.subsref at 1364
                [inds{k},refs{k}] = privformat(inds{k});

Your help in solving these two problems would be greatly appreciated.

Thanks in advance,
Dean

Subject: Entering a symbolic vector and sum

From: Steven_Lord

Date: 29 Oct, 2010 02:48:31

Message: 2 of 3



"Dean " <dchesterfield@hotmail.com> wrote in message
news:iad90r$8v6$1@fred.mathworks.com...
> Hi,
>
> I am new to Matlab so please bear with me. I have two questions that you
> may be able to help with:
>
> 1. Firstly, I would like to enter a 5-element vector, X, consisting of
> symbolic elements. The symbolic elements will eventually be substituted
> with positive real values, later on in the code. I know that I can enter
> such a symbolic vector as follows:
>
>>> sym('x1','positive');
>
> For x1 to x5. Then
>
>>> X = [x1 x2 x3 x4 x5]'

X = sym(zeros(1, 5));
for k = 1:5
    X(k) = sym(sprintf('x%d', k));
end

*snip*

> 2. Secondly, I need to create symbolic sums. As a simple example, I would
> like Matlab to assign to variable S the sum from i=1 to 5, of
> X(i)*log(X(i)), where X(i) refers to element i of vector X. I know I can
> explicitly enter the whole formula. However, the sums get considerably
> more complicated (including sums of sums) and thus I would prefer not to
> have to enter every term. I don't know why the following doesn't work:
>
>>> syms i
>>> S = symsum(X(i)*log(X(i)),i,1,5)

*snip*

sum(X.*log(X))

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Entering a symbolic vector and sum

From: Dean

Date: 31 Oct, 2010 23:55:05

Message: 3 of 3

Thanks very much for your help Steve.

"Steven_Lord" <slord@mathworks.com> wrote in message <iadclv$225$1@fred.mathworks.com>...
>
>
> "Dean " <dchesterfield@hotmail.com> wrote in message
> news:iad90r$8v6$1@fred.mathworks.com...
> > Hi,
> >
> > I am new to Matlab so please bear with me. I have two questions that you
> > may be able to help with:
> >
> > 1. Firstly, I would like to enter a 5-element vector, X, consisting of
> > symbolic elements. The symbolic elements will eventually be substituted
> > with positive real values, later on in the code. I know that I can enter
> > such a symbolic vector as follows:
> >
> >>> sym('x1','positive');
> >
> > For x1 to x5. Then
> >
> >>> X = [x1 x2 x3 x4 x5]'
>
> X = sym(zeros(1, 5));
> for k = 1:5
> X(k) = sym(sprintf('x%d', k));
> end
>
> *snip*
>
> > 2. Secondly, I need to create symbolic sums. As a simple example, I would
> > like Matlab to assign to variable S the sum from i=1 to 5, of
> > X(i)*log(X(i)), where X(i) refers to element i of vector X. I know I can
> > explicitly enter the whole formula. However, the sums get considerably
> > more complicated (including sums of sums) and thus I would prefer not to
> > have to enter every term. I don't know why the following doesn't work:
> >
> >>> syms i
> >>> S = symsum(X(i)*log(X(i)),i,1,5)
>
> *snip*
>
> sum(X.*log(X))
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

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