Outputting solutions to symbolic variables in a matrix.

11 views (last 30 days)
I created a system of 22 symbolic varriables for a rather large system of euqation I was solving. I was able to solve for all the varriables and file them into a 22x1 matrix. However, now I need to extract the solve values of those matrix elements. When I try to use an element like Matrix(7,1) it simply outputs 'x7', and I cant use the 'sol.' on the matrix or its elements to get these numbers. Is there an efficent way to get the solved values from all the matrix elements?

Answers (3)

Star Strider
Star Strider on 17 Feb 2019
Assuming the vector elements are not functions of symbolic variables (i.e. are purely numeric) see if using the double (link) function will convert them to an array that will allow you to access specific numeric elements. (It should.)
  2 Comments
Joe Turner
Joe Turner on 17 Feb 2019
Trying to use that gives me an error:
Error using symengine
Unable to convert expression into double array.
Error in sym/double (line 672)
Xstr = mupadmex('symobj::double', S.s, 0);
Hopefully I was clear in the original question, but the matrix I'm trying to get the values for looks something like M=[x1;x2;x3...x22]. I've already got the solutions for the xs, and can view them with sol.x. I just need to convert the matrix to store all the solved values
Star Strider
Star Strider on 17 Feb 2019
Your vector likely contains symbolic variables in the solutions, so double will not work in that event. (I thought I covered that in: ‘Assuming the vector elements are not functions of symbolic variables...’)
You need to evaluate them with respect to those variables, and then export them as double values.
A simple example:
syms x y
a(1) = sym(pi);
a(2) = pi*x + y;
D(1) = double(a(1))
D(2) = double(a(2))
produces:
D =
3.14159265358979
Error using symengine
Unable to convert expression into double array.
Error in sym/double (line 672)
Xstr = mupadmex('symobj::double', S.s, 0);
Error in TestScript (line ###)
D(2) = double(a(2))
I suspect this is essentially the same error you are seeing.

Sign in to comment.


Walter Roberson
Walter Roberson on 17 Feb 2019
It sounds like Matrix is now an array of symbolic expressions, some probably representing just numbers, and some probably representing a mix of numbers and symbolic variables.
Once you have the expressions, you can replace the variables with particular numeric values by using subs()
subs(Matrix, x7, pi/11)
for example.
  1 Comment
Walter Roberson
Walter Roberson on 21 Jul 2021
Re-reading the question now, I realized that the difficulty might be that MATLAB did not find a convenient closed-form solution, and ended up representing the solutions parametricly. When that happens and you did not pass in the "returnconditions" option, matlab will give you a warning, and you usually cannot get the actual values without rewriting. If you pass in "returnconditions" true then the structure returned will have a member called Conditions, on which case sometimes you are able to get the actual value with careful analysis.
If the equations were polynomials of degree 3 or 4 then you can pass in 'Maxdegree', 4 to get the full expansion. The full expansion is not useful most of the time; it is pages of expressions that humans basically cannot grasp the meaning of and have to hope were generated correctly.

Sign in to comment.


Praful Gagrani
Praful Gagrani on 21 Jul 2021
I had the same problem, and I am doing something like the following:
Say you have symbolic variable array 'x' and equations 'eqns'.
x = sym('x',[1,n])
var = [x(1) x(2) ... x(n)]
sol = solve(eqns, var)
You can get the values by:
for i = 1:n
sol.(['x' num2str(i)])
end
Hope this helps!

Community Treasure Hunt

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

Start Hunting!