Pulling coefficients out of symbolic matrix to solve

6 views (last 30 days)
I am having some issues making a model I've constructed useful for every case. I have two 2x2 matrices of constants. Currently I have MATLAB construct symbolic expressions for each point with expressions such as: 78(A11) + 33(A21) + 66(A33) + 89(B22) etc.
I take the coefficients out using coeffs and sym2poly and form a matrix, Q, and using Q\b where b is my collection of target values for each expression, I'm able to solve for the A and B matrices of constants. This is great.
My problem is, I have no idea what order my constants are in without checking them by hand. If an expression has just an a21 value, I need to pad it out to 0 a21 0 0 in the matrix so my regression will work correctly. But since each expression is a 1x1 symbolic variable I can't use any line search techniques I know of. I really need to be able to trust my code to pull out the coefficients and assemble them in my matrix in the correct order, putting zeros in the right places rather than changing the code every time I use new data.
I appreciate your help on this matter. Thanks!

Answers (1)

Prateekshya
Prateekshya on 7 Oct 2024
Hello @B K,
To automate the process of extracting coefficients and assembling them into a consistent matrix format, you can use symbolic manipulation techniques in MATLAB. The goal is to ensure that each expression's coefficients are extracted in a consistent order corresponding to a predefined list of symbolic variables. Here is how you can approach this problem:
  • Define the Variables: Start by defining all the symbolic variables consistently across your expressions. This ensures that you always have the same reference for coefficient extraction.
  • Symbolic Expressions: Construct your symbolic expressions using these predefined variables.
  • Extract Coefficients: Use MATLAB's symbolic toolbox functions to extract coefficients in the correct order.
  • Assemble the Matrix: Construct your matrix by placing the extracted coefficients in the correct positions, filling in zeros where necessary.
I hope this helps!

Community Treasure Hunt

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

Start Hunting!