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:
Declaring multiple symbols

Subject: Declaring multiple symbols

From: Dhruv

Date: 28 May, 2010 05:09:04

Message: 1 of 3

I need to perform symbolic differentiation but the number of variables keep changing

Ideally I would like to declare say N vars x1, x2, x3 .... xN - Can someone help me with how to define these symbols automatically given a value of N?
I tried using strings to declare the variables but was unsuccesful.

Thanks,
Dhruv

Subject: Declaring multiple symbols

From: Sean

Date: 28 May, 2010 12:41:27

Message: 2 of 3

"Dhruv " <dhruv.singh@gmail.com> wrote in message <htnj5g$jo$1@fred.mathworks.com>...
> I need to perform symbolic differentiation but the number of variables keep changing
>
> Ideally I would like to declare say N vars x1, x2, x3 .... xN - Can someone help me with how to define these symbols automatically given a value of N?
> I tried using strings to declare the variables but was unsuccesful.
>
> Thanks,
> Dhruv

One way without loops:

>>n = 10;
>>nvec = num2str([1:n]');
>>xnvec = bsxfun(@strcat,'x',nvec);
>>cxnvec = cellstr(xnvec);
>>cxnvec = cellfun(@(x)x(~isspace(x)),cxnvec,'UniformOutput',false);
>>syms(cxnvec{:})

-Sean

Subject: Declaring multiple symbols

From: Alan B

Date: 28 May, 2010 14:28:28

Message: 3 of 3

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <htodln$fuo$1@fred.mathworks.com>...
> "Dhruv " <dhruv.singh@gmail.com> wrote in message <htnj5g$jo$1@fred.mathworks.com>...
> > I need to perform symbolic differentiation but the number of variables keep changing
> >
> > Ideally I would like to declare say N vars x1, x2, x3 .... xN - Can someone help me with how to define these symbols automatically given a value of N?
> > I tried using strings to declare the variables but was unsuccesful.
> >
> > Thanks,
> > Dhruv
>
> One way without loops:
>
> >>n = 10;
> >>nvec = num2str([1:n]');
> >>xnvec = bsxfun(@strcat,'x',nvec);
> >>cxnvec = cellstr(xnvec);
> >>cxnvec = cellfun(@(x)x(~isspace(x)),cxnvec,'UniformOutput',false);
> >>syms(cxnvec{:})
>
> -Sean

Another way:

n=10;
names = regexp(sprintf('x%d/',1:n),'/','split');
syms(names{1:end-1})

Tags for this Thread

No tags are associated with 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