From: Louis on 2 Apr 2010 17:35 Hi, I have a bunch of linear expressions that I'll have to multiply by each other. For example if: a = 2 * x1 + x2 + 3 * x3 b = 5 * x1 + x2 + 2 * x3 c = 6 * x1 + x2 + 3 * x3 I would like a way to calculate a * b * c. I've looked into using convn but am unsure the format to pass the arguments.
From: John D'Errico on 2 Apr 2010 18:55 "Louis" <tanlouiss+matlab(a)gmail.com> wrote in message <hp5nup$kvr$1(a)fred.mathworks.com>... > Hi, > > I have a bunch of linear expressions that I'll have to multiply by each other. For example if: > > a = 2 * x1 + x2 + 3 * x3 > b = 5 * x1 + x2 + 2 * x3 > c = 6 * x1 + x2 + 3 * x3 > > I would like a way to calculate a * b * c. > > I've looked into using convn but am unsure the format to pass the arguments. No, you will not be able to do this in any general case with convn. Do you have the symbolic toolbox? If not, then you can do it with my sympoly tools, found on the file exchange. http://www.mathworks.com/matlabcentral/fileexchange/9577 sympoly x1 x2 x3 a = 2 * x1 + x2 + 3 * x3 b = 5 * x1 + x2 + 2 * x3 c = 6 * x1 + x2 + 3 * x3 a*b*c ans = 18*x3^3 + 21*x2*x3^2 + 8*x2^2*x3 + x2^3 + 93*x1*x3^2 + 70*x1*x2*x3 + 13*x1*x2^2 + 144*x1^2*x3 + 52*x1^2*x2 + 60*x1^3 HTH, John
From: Louis on 2 Apr 2010 20:12 Hi thanks for the help but there's just a couple more issues: 1. Is there any way to extract the coefficients and powers from the answer? If I have n terms and m variables in my answer then I would like to store the exponents of all the m variables in an n x m array A, where A(i, j) is the exponent of the jth variable in the ith term For example if ans = 3 * x2^2 * x3^5 + 8 * x1^7 * x2 then n = 2, m = 3 and I will need an exponent array [0 2 5; 7 1 0] and also a coefficient array [3; 2] 2. If I know that I have m variables (m is not known beforehand) is there any easy way for me to declare them as p1, p2, ... , pm with one call to syms? Or would I have to put the strings 'p1' ... in a cell array and create a loop calling sym each time?
From: John D'Errico on 2 Apr 2010 21:13 "Louis" <tanlouiss+matlab(a)gmail.com> wrote in message <hp614k$na9$1(a)fred.mathworks.com>... > Hi thanks for the help but there's just a couple more issues: > > 1. Is there any way to extract the coefficients and powers from the answer? > > If I have n terms and m variables in my answer then I would like to store the exponents of all the m variables in an n x m array A, where A(i, j) is the exponent of the jth variable in the ith term > > For example if > > ans = > 3 * x2^2 * x3^5 + 8 * x1^7 * x2 > > then n = 2, m = 3 and I will need an exponent array > > [0 2 5; 7 1 0] > > and also a coefficient array > [3; 2] > > 2. If I know that I have m variables (m is not known beforehand) is there any easy way for me to declare them as p1, p2, ... , pm with one call to syms? Or would I have to put the strings 'p1' ... in a cell array and create a loop calling sym each time? 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
From: Bruno Luong on 3 Apr 2010 03:12 "John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <hp5skr$ob9$1(a)fred.mathworks.com>... > > No, you will not be able to do this in any general > case with convn. > Why not? % Data a=zeros(2,2,2); a(2,1,1) = 2; a(1,2,1) = 1; a(1,1,2) = 3; b=zeros(2,2,2); b(2,1,1) = 5; b(1,2,1) = 1; b(1,1,2) = 2; c=zeros(2,2,2); c(2,1,1) = 6; c(1,2,1) = 1; c(1,1,2) = 3; % Engine ab=convn(a,b); abc=convn(ab,c); % Print out fprintf('abc = ') for n=1:numel(abc) p=abc(n); if p>0 [i j k] = ind2sub(size(abc),n); i = i-1; j = j-1; k = k-1; if i==0 xi = ''; elseif i==1 xi = '*x'; else xi = sprintf('*x^%d', i); end if j==0 yj = ''; elseif j==1 yj = '*y'; else yj = sprintf('*y^%d', j); end if k==0 zk = ''; elseif k==1 zk = '*z'; else zk = sprintf('*z^%d', k); end fprintf('%+g%s%s%s', p, xi, yj, zk) end end fprintf('\n') % Bruno
|
Next
|
Last
Pages: 1 2 3 Prev: Finding a stationary point of a function Next: genetic algorithm - inner matrix error message |