From: Bruno Luong on
A note: Feel free to flip the coefficients in the arrays to be more inline with Matlab polynomial storing convention.

Bruno
From: Louis on
>
> If you would but look at sympoly, you would find
> exactly what you desire.
>
> struct(a*b*c)
> ans =
> Var: {'x1' 'x2' 'x3'}
> Exponent: [10x3 double]
> Coefficient: [10x1 double]
>
> John

Wow thanks a lot the toolbox works really well. Still a few issues though:

1. If I have some sympoly expression "x + y + z" is there a quick built in way for me to multiply each term so I get (x*y*z)? Right now I'm looping and calling the "terms" function each time and storing the products in a seperate variable (I won't know how many terms I have)

2. For a term r = x^2 * y^5 * z^2, I would like to evaluate this at
[x, y, z] = [1 2 3].

Is there a good way for me to do this? The hard part is I don't know if any of the variables x, y or z will be in the term.

I was able to do this by having a cell array c = {'x'; 'y'; 'z'} and a value array, val = [1 2 3]. Then I loop and use

for i = 1:3
r = subs(r, char(c(i)), val(i));
end

Is there a better way to do this though?

3. When I do matrix multiplication with sympoly variables it looks like an extra empty variable ('') is added. Below is a script that reproduces this.

psym = sympoly('p0');
for i = 1:2
psym = [psym; sympoly(['p', num2str(i)])];
end

a = [1 2 3; 4 5 6];
b = a*psym;

b_struct = struct(b);
b_struct(1)
From: John D'Errico on
"Louis" <tanlouiss+matlab(a)gmail.com> wrote in message <hp6rev$23h$1(a)fred.mathworks.com>...
> >
> > If you would but look at sympoly, you would find
> > exactly what you desire.
> >
> > struct(a*b*c)
> > ans =
> > Var: {'x1' 'x2' 'x3'}
> > Exponent: [10x3 double]
> > Coefficient: [10x1 double]
> >
> > John
>
> Wow thanks a lot the toolbox works really well. Still a few issues though:
>
> 1. If I have some sympoly expression "x + y + z" is there a quick built in way for me to multiply each term so I get (x*y*z)?

But this is an operation that has no standard
mathematical definition, and therefore there
is/was no reason for me to have provided such
functionality in this toolbox.

I suppose one might do it via a power though.
If you cube the expression x+y+z, you will find
one term in there that is the cube of the individual
terms.


> 2. For a term r = x^2 * y^5 * z^2, I would like to evaluate this at
> [x, y, z] = [1 2 3].
>
> Is there a good way for me to do this? The hard part is I don't know if any of the variables x, y or z will be in the term.
>
> I was able to do this by having a cell array c = {'x'; 'y'; 'z'} and a value array, val = [1 2 3]. Then I loop and use
>
> for i = 1:3
> r = subs(r, char(c(i)), val(i));
> end
>
> Is there a better way to do this though?

Nope. Subs is the way to do it, although you can
do things like this to do it in one line:

r = subs(r, 'x', val(1), 'y',val(2), 'z',val(3));



> 3. When I do matrix multiplication with sympoly variables it looks like an extra empty variable ('') is added. Below is a script that reproduces this.
>

It sometimes happens, but hurts nothing. I do clean
them up periodically. In fact, there is a tool called
clean_sympoly that should catch things like that,
and remove those spurious happenings.

John
From: Louis on
> > 2. For a term r = x^2 * y^5 * z^2, I would like to evaluate this at
> > [x, y, z] = [1 2 3].
> >
> > Is there a good way for me to do this? The hard part is I don't know if any of the variables x, y or z will be in the term.
> >
> > I was able to do this by having a cell array c = {'x'; 'y'; 'z'} and a value array, val = [1 2 3]. Then I loop and use
> >
> > for i = 1:3
> > r = subs(r, char(c(i)), val(i));
> > end
> >
> > Is there a better way to do this though?
>
> Nope. Subs is the way to do it, although you can
> do things like this to do it in one line:
>
> r = subs(r, 'x', val(1), 'y',val(2), 'z',val(3));
>

Thanks, the only problem again is that I don't know how many variables I have until the program starts. But I can still try to build a string with that command while the program is running then use "eval"

> > 3. When I do matrix multiplication with sympoly variables it looks like an extra empty variable ('') is added. Below is a script that reproduces this.
> >
>
> It sometimes happens, but hurts nothing. I do clean
> them up periodically. In fact, there is a tool called
> clean_sympoly that should catch things like that,
> and remove those spurious happenings.

For my program, I need the Exponent field which has one extra column. Is it safe to just index struct.Exponent(:, 2:end) or should I check if there's a Var called '' and then index?

Thanks
Louis
From: Louis on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hp6q3d$fld$1(a)fred.mathworks.com>...
> A note: Feel free to flip the coefficients in the arrays to be more inline with Matlab polynomial storing convention.
>
> Bruno

Thanks for the help. If I do it this way though, is there a simple way to evaluate the polynomial? (ie is there a multivariable version of polyval)

Also in your script, I take it that every non zero element in abc represents a term. Is it possible to multiply each of these terms together?