From: Dhruv on
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
From: Sean on
"Dhruv " <dhruv.singh(a)gmail.com> wrote in message <htnj5g$jo$1(a)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
From: Alan B on
"Sean " <sean.dewolski(a)nospamplease.umit.maine.edu> wrote in message <htodln$fuo$1(a)fred.mathworks.com>...
> "Dhruv " <dhruv.singh(a)gmail.com> wrote in message <htnj5g$jo$1(a)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})