Extracting coefficient Matrix and vector from available polynomial Expressions with exponential terms

Hello All,
I have stuck in one problem, where I have variable matrix vector available and I want to extract the coefficient matrix and variable vector from that original polynomial vector matrix. For e.g. if A is matrix as:
and I want to extract its coefficients and variable vector as given below:
So my question is how to compute or Extract Matrix Y and Matrix Z from A which comprises of expressions with exponential terms of eigenvalues of system matrix.
Is there any direct command? I tried commands like - coeffs, sysvar, equationsToMatrix but in all those cases the variables are fixed.
So, please help me in solving this above problem.
Thank You.

 Accepted Answer

You have not shown what you already tried or what the results were.
In R2019a, this is possible with a for loop (since coeffs apparently doesn’t like matrices):
syms lambda1 lambda2 t
A = [exp(-lambda1*t)+3*exp(-lambda2*t); -2*exp(-lambda1*t)+4*exp(-lambda2*t)];
for k = 1:size(A,1)
[Ac{k},trm{k}] = coeffs(A(k,:));
end
Cfs = [Ac{1}; Ac{2}]
Trm = [trm{1}; trm{2}]
producing:
Cfs =
[ 1, 3]
[ -2, 4]
Trm =
[ exp(-lambda1*t), exp(-lambda2*t)]
[ exp(-lambda1*t), exp(-lambda2*t)]
That may be the best you can hope for.

13 Comments

Hello,
I have tried those (coeffs, sysvar, equationsToMatrix) commands, but it showed error.
Sorry that I have not mentioned how A is calculated in my question. Actually A matrix is multiplication of State Transistion Matrix (STM) and column input vector B of state space. So here we are not defining A, but it is computed by MATLAB only (STM*B). So everytime System matrix changes, then STM changes, so as Lymbda values.
As in your answer you have shown that A is defined and lymbda are taken as 'syms', but after calculation of STM*B those values may change, we don't know depending upon the System Matrix and Input Matrix of State Space. In such cases how we can find? It should be applicable to generalised system.
For e.g. First example have lyambdas as -1 and -0.5. And I want to MATLAB to extract Y and Z matrices from A1.
Screenshot 2019-05-12 at 6.39.52 PM.png
second example have lyamdas as -2 and -3 and here I need Y2 and Z2 matrices from A2.
Screenshot 2019-05-12 at 6.41.25 PM.png
So, we don't know values of A1 and A2 everytime.
Don't know whether you understand my question or not. I hope you got my querry.
Thank You.
My code should work with your matrices, giving the results you want:
syms t
A1 = [exp(-t)+3*exp(-0.5*t); -2*exp(-t)+4*exp(-0.5*t)];
for k = 1:size(A1,1)
[Ac{k},trm{k}] = coeffs(A1(k,:));
end
Cfs = [Ac{1}; Ac{2}]
Trm = [trm{1}].'
producing:
Cfs =
[ 1, 3]
[ -2, 4]
Trm =
exp(-t)
exp(-t/2)
That is likely the best you can hope for.
You may want to create this as a function, or just change the names of the matrices in the loop.
Thank You sir for your code.
But again here you have defined the A1 matrix, which is not feasible in my case. In my program first I am computing A1 matrix, and immidietly I want to extract Y and Z matrices, in sequence.
So, we can't define the A1 in the same program, without calculating it.
If I am using diferent file, then your program is Best suits. But I want to calulate A1 first then from it Y and Z.
Thank YOU.
Can't we make it generalised one? Applicable for any matrix with different lyamdas.
My pleasure.
If your only variable is ‘t’, you can probably put my code into a function and pass various symbolic ‘A’ matrices to it. Beyond that, my code is likely as general as it can be.
It does, however it may be difficult to display the results the way you want them:
syms t
A1 = [3*exp(-0.5*t); -2*exp(-t)+4*exp(-0.5*t)];
Ac = cell(2,1); % Preallocate
trm = cell(2,1); % Preallocate
for k = 1:size(A1,1)
[Ac{k},trm{k}] = coeffs(A1(k,:));
end
Ac{:}
trm{:}
Experiment to get the results you want.
Thank you for modified code,
but sorry, but this is not giving the answer (in Matrix form) as I am expecting.
My pleasure.
The coeffs function works on vectors only.
Hello Star Strider,
I have a 3x1 matrix as
A =
[exp(-1.0*t) - 1.0*exp(-1.0*t)*cos(t)
exp(-1.0*t)*cos(t) - 1.0*exp(-1.0*t) + exp(-1.0*t)*sin(t)
exp(-1.0*t) - 2.0*exp(-1.0*t)*sin(t)]
I want to extract the coefficients as posted querries earlier for a 3rd order system:
Trm = [exp(-1.0*t); exp(-1.0*t)*cos(t); exp(-1.0*t)*sin(t)]
and
Cfs = [1 -1 0; -1 1 1; 1 0 -2]
I used the following code, but its giving dimension error "CAT arguments dimensions not consistent."
for i = 1:size(A,1)
[Cfs{i}] = coeffs(A(i,:))
end
Cfs = [Cfs{1}; Cfs{2}; Cfs{3}]
There is 0 coefficient of sin term, so its making Cfs{1} is of 1x2 matrix and no cos term in Cfs{3} so it also 1x2, and all terms coefficients present in Cfs{2} so it is 1x3. Thus making dimension mismatch error.
Do you have any alternative, how to get Cfs as 3x3 matrix, I mentioned above.
Thank you.
Not everything is possible in this world, and this is one of them.
The best you can do is:
syms t
A = [(exp(-1.0*t) - 1.0*exp(-1.0*t)*cos(t))
(exp(-1.0*t)*cos(t) - 1.0*exp(-1.0*t) + exp(-1.0*t)*sin(t))
(exp(-1.0*t) - 2.0*exp(-1.0*t)*sin(t))]
for k = 1:size(A,1)
Cfs{k} = factor(A(k))
end
and go from there.
😃😉 Ok, Thank You STAR
I am entering that Cfs matrix manually now, may be that is what I have to continue.
Thank you again 😊.

Sign in to comment.

More Answers (1)

If the coefficients of lyamda in A matrix are either 0 (ZERO) or if the eigenvalues are repeated then there will be t term along with exponential term (For e.g. e^(-2t) and t.e^(-2t). So in these two cases Z Matrix is not correctly computed.
Apart from these two cases the above program given by Star Strider work.
Thank You.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!