From: Gideon Simpson on
Is it possible to define symbolic variables of arrays?

I want to do

syms a(1) a(2) a(3)...

so that I can compute the jacobian of a vector valued function.
From: Steven Lord on

"Gideon Simpson" <gideon.simpson(a)gmail.com> wrote in message
news:hr4bca$k7g$1(a)fred.mathworks.com...
> Is it possible to define symbolic variables of arrays?
>
> I want to do
>
> syms a(1) a(2) a(3)...
>
> so that I can compute the jacobian of a vector valued function.

A = sym(zeros(4));
for k = 1:numel(A)
A(k) = sym(sprintf('a%d', k));
end
A

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: Alan Weiss on
On 4/26/2010 1:08 PM, Steven Lord wrote:
> "Gideon Simpson"<gideon.simpson(a)gmail.com> wrote in message
> news:hr4bca$k7g$1(a)fred.mathworks.com...
>> Is it possible to define symbolic variables of arrays?
>>
>> I want to do
>>
>> syms a(1) a(2) a(3)...
>>
>> so that I can compute the jacobian of a vector valued function.
>
> A = sym(zeros(4));
> for k = 1:numel(A)
> A(k) = sym(sprintf('a%d', k));
> end
> A
>
You might also want to see a doc example on computing symbolic jacobians:
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1

Or view a similar demo:
http://www.mathworks.com/products/optimization/demos.html?file=/products/demos/shipping/optim/symbolic_optim_demo.html

Alan Weiss
MATLAB mathematical toolbox documentation
From: Gideon Simpson on
Alan Weiss <aweiss(a)mathworks.com> wrote in message <hr4j9a$k5i$1(a)fred.mathworks.com>...
> On 4/26/2010 1:08 PM, Steven Lord wrote:
> > "Gideon Simpson"<gideon.simpson(a)gmail.com> wrote in message
> > news:hr4bca$k7g$1(a)fred.mathworks.com...
> >> Is it possible to define symbolic variables of arrays?
> >>
> >> I want to do
> >>
> >> syms a(1) a(2) a(3)...
> >>
> >> so that I can compute the jacobian of a vector valued function.
> >
> > A = sym(zeros(4));
> > for k = 1:numel(A)
> > A(k) = sym(sprintf('a%d', k));
> > end
> > A
> >
> You might also want to see a doc example on computing symbolic jacobians:
> http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1
>
> Or view a similar demo:
> http://www.mathworks.com/products/optimization/demos.html?file=/products/demos/shipping/optim/symbolic_optim_demo.html
>
> Alan Weiss
> MATLAB mathematical toolbox documentation

This isn't quite working as I had wished. Suppose my function is

f(x_1, x_2) = x_1^2 + 3 *x_1 x_2 - sin(x_2)

and I intend to define x as a 2d column vector, and wish to compute the jacobian of f.

if I try:

x = sym(zeros(2,1));

for k = 1:numel(x)
x(k) = sym(sprintf('x(%i)',k));
end

f = x(1)^2 + 3 * x(1) * x(2) - sin(x(2))

But if I now do

jacobian(f, x)

I get:

[ diff(3*x(1)*x(2) - sin(x(2)) + x(1)^2, x(1)), diff(3*x(1)*x(2) - sin(x(2)) + x(1)^2, x(2))]


Obviously, I can switch to having my variables be x1, x2, as in the examples, but suppose I want to avoid that. Is it possible?
From: Alan Weiss on
"Gideon Simpson" <gideon.simpson(a)gmail.com> wrote in message <hrkbqu$sui$1(a)fred.mathworks.com>...
> Alan Weiss <aweiss(a)mathworks.com> wrote in message <hr4j9a$k5i$1(a)fred.mathworks.com>...
> > On 4/26/2010 1:08 PM, Steven Lord wrote:
> > > "Gideon Simpson"<gideon.simpson(a)gmail.com> wrote in message
> > > news:hr4bca$k7g$1(a)fred.mathworks.com...
> > >> Is it possible to define symbolic variables of arrays?
> > >>
> > >> I want to do
> > >>
> > >> syms a(1) a(2) a(3)...
> > >>
> > >> so that I can compute the jacobian of a vector valued function.
> > >
> > > A = sym(zeros(4));
> > > for k = 1:numel(A)
> > > A(k) = sym(sprintf('a%d', k));
> > > end
> > > A
> > >
> > You might also want to see a doc example on computing symbolic jacobians:
> > http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1
> >
> > Or view a similar demo:
> > http://www.mathworks.com/products/optimization/demos.html?file=/products/demos/shipping/optim/symbolic_optim_demo.html
> >
> > Alan Weiss
> > MATLAB mathematical toolbox documentation
>
> This isn't quite working as I had wished. Suppose my function is
>
> f(x_1, x_2) = x_1^2 + 3 *x_1 x_2 - sin(x_2)
>
> and I intend to define x as a 2d column vector, and wish to compute the jacobian of f.
>
> if I try:
>
> x = sym(zeros(2,1));
>
> for k = 1:numel(x)
> x(k) = sym(sprintf('x(%i)',k));
> end
>
> f = x(1)^2 + 3 * x(1) * x(2) - sin(x(2))
>
> But if I now do
>
> jacobian(f, x)
>
> I get:
>
> [ diff(3*x(1)*x(2) - sin(x(2)) + x(1)^2, x(1)), diff(3*x(1)*x(2) - sin(x(2)) + x(1)^2, x(2))]
>
>
> Obviously, I can switch to having my variables be x1, x2, as in the examples, but suppose I want to avoid that. Is it possible?

I am not sure it is possible. The basic data type for symbolic variables is complex scalar. When you make a vector out of these scalars, I believe the scalars should each have a different name. So a vector of scalars x = [x1,x2,...,xn], for example, has x(2) = x2. I believe trying to do it your way has x(2) essentially undefined.

But I could be wrong.

In any case, it certainly works to define each element of a symbolic vector as a separate symbolic scalar, so I suggest you employ that technique for now.

Alan Weiss
MATLAB mathematical toolbox documentation