MATLAB Answers

0

Outputting solutions to symbolic variables in a matrix.

Asked by Joe Turner on 17 Feb 2019
Latest activity Answered by Walter Roberson
on 17 Feb 2019
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?

  0 Comments

Sign in to comment.

2 Answers

Answer by 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

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
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.


Answer by 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.

  0 Comments

Sign in to comment.