ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 02 Feb 2016 21:04:32 -0600How do you Append a Symbolic Matrix?http://ask.sagemath.org/question/32406/how-do-you-append-a-symbolic-matrix/To All:
I am trying to make a function that will take a set of dynamics and a set of independent variables that can or cannot be within the dynamics equations to create a "frozen" state space matrix, A. I would like to tell you all now that I am mostly a FORTRAN coder and am in the process of trying to understand SageMath, which is why I am coding in a brute force method.
I previously checked to make sure that the derivative function was producing reasonable answers and created the matrices like this.
rmag = r[0]^2 + r[1]^2 + r[2]^2;
dyn = -mu*r/rmag^3; # spherical gravity assumption
ddyn_dx = dyn.derivative(r[0]);ddyn_dy = dyn.derivative(r[1]);ddyn_dz = dyn.derivative(r[2]);
pdyn_pr = (transpose(matrix([ddyn_dx,ddyn_dy,ddyn_dz])));
As you might have discovered, this is exceptionally tedious when the number of independent variables get larger.
Therefore, I desired to code something like this:
def FindDynMatrix(dynamics,xvect):
# Find the length of the vector defining the internal variables
# within dynamics are independent variables:
leng = len(xvect);
for j in range(leng):
vect = dynamics.derivative(xvect[j]);
if j == 0:
mat = vect;
else:
mat=matrix([mat,vect])
#mat = mat.append(vect) # didn't work
return mat
In "FindDynMatrix", symbolic vector that is dependent on a multitude of variables including those within the symbolic vector "xvect". The hope was to "black box" the production of the A matrix for a little controls tool that I am coding up.
However, I can not find a way to get this to work. Help would be appreciated.
Sun, 31 Jan 2016 17:56:36 -0600http://ask.sagemath.org/question/32406/how-do-you-append-a-symbolic-matrix/Comment by PleaseNoCyrillicVariables for <p>To All:</p>
<p>I am trying to make a function that will take a set of dynamics and a set of independent variables that can or cannot be within the dynamics equations to create a "frozen" state space matrix, A. I would like to tell you all now that I am mostly a FORTRAN coder and am in the process of trying to understand SageMath, which is why I am coding in a brute force method.</p>
<p>I previously checked to make sure that the derivative function was producing reasonable answers and created the matrices like this.</p>
<pre><code>rmag = r[0]^2 + r[1]^2 + r[2]^2;
dyn = -mu*r/rmag^3; # spherical gravity assumption
ddyn_dx = dyn.derivative(r[0]);ddyn_dy = dyn.derivative(r[1]);ddyn_dz = dyn.derivative(r[2]);
pdyn_pr = (transpose(matrix([ddyn_dx,ddyn_dy,ddyn_dz])));
</code></pre>
<p>As you might have discovered, this is exceptionally tedious when the number of independent variables get larger.
Therefore, I desired to code something like this:</p>
<pre><code>def FindDynMatrix(dynamics,xvect):
# Find the length of the vector defining the internal variables
# within dynamics are independent variables:
leng = len(xvect);
for j in range(leng):
vect = dynamics.derivative(xvect[j]);
if j == 0:
mat = vect;
else:
mat=matrix([mat,vect])
#mat = mat.append(vect) # didn't work
return mat
</code></pre>
<p>In "FindDynMatrix", symbolic vector that is dependent on a multitude of variables including those within the symbolic vector "xvect". The hope was to "black box" the production of the A matrix for a little controls tool that I am coding up.</p>
<p>However, I can not find a way to get this to work. Help would be appreciated.</p>
http://ask.sagemath.org/question/32406/how-do-you-append-a-symbolic-matrix/?comment=32424#post-id-32424I believe that I have found the solution for part of this problem. By calling Jacobian(dyn,r), I can produce 'pdyn_pr'. Reference: http://ask.sagemath.org/question/9830/generating-a-jacobian-matrix-for-a-set-of-multivariate-polynomials/
However, I would still like to know how to append to symbolic matrices for future reference.Tue, 02 Feb 2016 21:04:32 -0600http://ask.sagemath.org/question/32406/how-do-you-append-a-symbolic-matrix/?comment=32424#post-id-32424Comment by PleaseNoCyrillicVariables for <p>To All:</p>
<p>I am trying to make a function that will take a set of dynamics and a set of independent variables that can or cannot be within the dynamics equations to create a "frozen" state space matrix, A. I would like to tell you all now that I am mostly a FORTRAN coder and am in the process of trying to understand SageMath, which is why I am coding in a brute force method.</p>
<p>I previously checked to make sure that the derivative function was producing reasonable answers and created the matrices like this.</p>
<pre><code>rmag = r[0]^2 + r[1]^2 + r[2]^2;
dyn = -mu*r/rmag^3; # spherical gravity assumption
ddyn_dx = dyn.derivative(r[0]);ddyn_dy = dyn.derivative(r[1]);ddyn_dz = dyn.derivative(r[2]);
pdyn_pr = (transpose(matrix([ddyn_dx,ddyn_dy,ddyn_dz])));
</code></pre>
<p>As you might have discovered, this is exceptionally tedious when the number of independent variables get larger.
Therefore, I desired to code something like this:</p>
<pre><code>def FindDynMatrix(dynamics,xvect):
# Find the length of the vector defining the internal variables
# within dynamics are independent variables:
leng = len(xvect);
for j in range(leng):
vect = dynamics.derivative(xvect[j]);
if j == 0:
mat = vect;
else:
mat=matrix([mat,vect])
#mat = mat.append(vect) # didn't work
return mat
</code></pre>
<p>In "FindDynMatrix", symbolic vector that is dependent on a multitude of variables including those within the symbolic vector "xvect". The hope was to "black box" the production of the A matrix for a little controls tool that I am coding up.</p>
<p>However, I can not find a way to get this to work. Help would be appreciated.</p>
http://ask.sagemath.org/question/32406/how-do-you-append-a-symbolic-matrix/?comment=32423#post-id-32423'r' is a vector so the equation is correct.
x,y,z = var('x,y,z')
mu = var('mu')
r = vector([x,y,z])
The variable 'dyn' is just an assumed spherical earth gravitational equation. 'mu' is a constant. One should be able to get 'pdyn_pr' as produced by the first set of equations by calling 'FindDynMatrix' like this, FindDynMatrix(dyn,r)Tue, 02 Feb 2016 20:32:41 -0600http://ask.sagemath.org/question/32406/how-do-you-append-a-symbolic-matrix/?comment=32423#post-id-32423Comment by tmonteil for <p>To All:</p>
<p>I am trying to make a function that will take a set of dynamics and a set of independent variables that can or cannot be within the dynamics equations to create a "frozen" state space matrix, A. I would like to tell you all now that I am mostly a FORTRAN coder and am in the process of trying to understand SageMath, which is why I am coding in a brute force method.</p>
<p>I previously checked to make sure that the derivative function was producing reasonable answers and created the matrices like this.</p>
<pre><code>rmag = r[0]^2 + r[1]^2 + r[2]^2;
dyn = -mu*r/rmag^3; # spherical gravity assumption
ddyn_dx = dyn.derivative(r[0]);ddyn_dy = dyn.derivative(r[1]);ddyn_dz = dyn.derivative(r[2]);
pdyn_pr = (transpose(matrix([ddyn_dx,ddyn_dy,ddyn_dz])));
</code></pre>
<p>As you might have discovered, this is exceptionally tedious when the number of independent variables get larger.
Therefore, I desired to code something like this:</p>
<pre><code>def FindDynMatrix(dynamics,xvect):
# Find the length of the vector defining the internal variables
# within dynamics are independent variables:
leng = len(xvect);
for j in range(leng):
vect = dynamics.derivative(xvect[j]);
if j == 0:
mat = vect;
else:
mat=matrix([mat,vect])
#mat = mat.append(vect) # didn't work
return mat
</code></pre>
<p>In "FindDynMatrix", symbolic vector that is dependent on a multitude of variables including those within the symbolic vector "xvect". The hope was to "black box" the production of the A matrix for a little controls tool that I am coding up.</p>
<p>However, I can not find a way to get this to work. Help would be appreciated.</p>
http://ask.sagemath.org/question/32406/how-do-you-append-a-symbolic-matrix/?comment=32410#post-id-32410It would be easier for us to understand your question if you provide the definition if the list `r` in your first line (which seems different from the `r` you used in your second line), the type and an example of `mu`, `xvect`, `dynamics`, ... so that we can try.Mon, 01 Feb 2016 15:51:04 -0600http://ask.sagemath.org/question/32406/how-do-you-append-a-symbolic-matrix/?comment=32410#post-id-32410