How do I declare a symbolic matrix in Octave?
In MatLab, you can declare symbols pretty easily:
syms a,b
m开发者_如何学JAVAat = [a,b]
I'm getting an error, however, when I try to replicate this in Octave. This is the code I'm using:
> symbols
> a = sym("a")
a =
a
> b = sym("b")
b =
b
> mat = [a,b]
error: octave_base_value::resize (): wrong type argument `ex'
error: octave_base_value::resize (): wrong type argument `<unknown type>'
octave-3.2.3.exe:4:C:\Octave\3.2.3_gcc-4.4.0\bin
How do you declare a symbolic matrix in octave?
If you don't already have the symbolic package, download it. From Octave command line, or gui command line. e.g.
octave> pkg install -forge symbolic
If you have python and sympy installed, that will install the package for you from octave forge. I used google to figure out how to get sympy installed, hit me up if you need help.
With symbolic package installed, use "pkg load" to import the package functions, and then use syms function to declare symbols.
octave> pkg load symbolic
octave> syms a b
This defined symbols a and b.
octave> syms
Symbolic variables in current scope:
a
b
"syms" by itself will print all the symbols you have defined.
octave> mat = [a,b]
mat = (sym) [a b] (1×2 matrix)
octave:34> mat * 2
ans = (sym) [2⋅a 2⋅b] (1×2 matrix)
I found this package very helpful in computing Rotation matrices for my Robotic Manipulators class. Hope this helps.
Here's part of my script for more examples:
pkg load symbolic
syms psi phi theta psidot phidot thetadot
RzPsi = [[cos(psi), -sin(psi), 0]; [sin(psi), cos(psi), 0]; [0,0,1]]
RyTheta = [[cos(theta), 0, sin(theta)];[0,1,0];[-sin(theta), 0, cos(theta)]]
RzPhi = [[cos(phi), -sin(phi), 0]; [sin(phi), cos(phi), 0]; [0,0,1]]
RzPsi = (sym 3×3 matrix)
⎡cos(ψ) -sin(ψ) 0⎤
⎢ ⎥
⎢sin(ψ) cos(ψ) 0⎥
⎢ ⎥
⎣ 0 0 1⎦
RyTheta = (sym 3×3 matrix)
⎡cos(θ) 0 sin(θ)⎤
⎢ ⎥
⎢ 0 1 0 ⎥
⎢ ⎥
⎣-sin(θ) 0 cos(θ)⎦
RzPhi = (sym 3×3 matrix)
⎡cos(φ) -sin(φ) 0⎤
⎢ ⎥
⎢sin(φ) cos(φ) 0⎥
⎢ ⎥
⎣ 0 0 1⎦
octave> RzPhi * RyTheta
ans = (sym 3×3 matrix)
⎡cos(φ)⋅cos(θ) -sin(φ) sin(θ)⋅cos(φ)⎤
⎢ ⎥
⎢sin(φ)⋅cos(θ) cos(φ) sin(φ)⋅sin(θ)⎥
⎢ ⎥
⎣ -sin(θ) 0 cos(θ) ⎦
Would this help ?
It looks like you might need the symbolic toolbox package, reference here.
After installing the symbolic toolbox (you can do this on some environments by issuing sudo apt-get install octave-symbolic
), you have to do the following:
symbols
x = sym('x')
but beware that Octave's functions for manipulating symbolic expressions are much worse than MATLAB's.
Symbolic Toolbox for Octave is more or less useless. You cannot resize a matrix as in your case, you cannot use the "-" operator. For example, you can differentiate a simple symbolic operation:
octave:1> symbols
octave:2> q1 = sym("q1");
octave:3> differentiate(Sin(q1)*Cos(q1),q1)
ans =
-sin(q1)^2+cos(q1)^2
but if you try to do this:
octave:6> -Sin(q1)
error: unary operator `-' not implemented for `ex' operands
octave:6> -q1
error: unary operator `-' not implemented for `ex' operands
Same happens in your case, i.e. resizing a matrix containing symbolic values
Another example for posterity.
I used http://octave-online.net/ to develop and run this octave script.
NB: I included output as comments to show results.
disp("2-state markov chain symbolic analysis");
syms lambda mu
L = [lambda,0]
# L = (sym) [λ 0] (1×2 matrix)
U = [1;0]
#U =
# 1
# 0
C = [ [1,1]; [lambda,-mu]]
#C = (sym 2×2 matrix)
# ⎡1 1 ⎤
# ⎢ ⎥
# ⎣λ -μ⎦
C^-1
#ans = (sym 2×2 matrix)
# ⎡ λ -1 ⎤
# ⎢────── + 1 ──────⎥
# ⎢-λ - μ -λ - μ⎥
# ⎢ ⎥
# ⎢ -λ 1 ⎥
# ⎢ ────── ──────⎥
# ⎣ -λ - μ -λ - μ⎦
P = C^-1 * U
#P = (sym 2×1 matrix)
#
# ⎡ λ ⎤
# ⎢────── + 1⎥
# ⎢-λ - μ ⎥
# ⎢ ⎥
# ⎢ -λ ⎥
# ⎢ ────── ⎥
# ⎣ -λ - μ ⎦
lambda_sys = L * C^-1 * U
#lambda_sys = (sym)
#
# ⎛ λ ⎞
# λ⋅⎜────── + 1⎟
# ⎝-λ - μ ⎠
Array of Handles
You can use the Octave Struct Array to create a symbolic matrix like this:
b(1,1).vector = @sin;
b(1,2).vector = @cos;
b(2,1).vector = @sec;
b(2,2).vector = @csc;
b
b.vector
printf( "\n\nCalling each element:\n" )
b(1,1).vector
b(1,2).vector
b(2,1).vector
b(2,2).vector
printf( "\nCalculatin the sin of 1:\n" )
b(1,1).vector(1)
Running this above returns:
b =
2x2 struct array containing the fields:
vector
ans = @sin
ans = @sec
ans = @cos
ans = @csc
Calling each element:
ans = @sin
ans = @cos
ans = @sec
ans = @csc
Calculatin the sin of 1:
ans = 0.841470984807897
Array of Symbols
You could replace the @sin
, @cos
, etc by sym("a")
, sym("b")
, sym("c")
, etc.
pkg load symbolic;
b(1,1).vector = sym("a");
b(1,2).vector = sym("b");
b(2,1).vector = sym("c");
b(2,2).vector = sym("d");
b
b.vector
printf( "\n\nCalling each element:\n" )
b(1,1).vector
b(1,2).vector
b(2,1).vector
b(2,2).vector
Running this above returns:
b =
2x2 struct array containing the fields:
vector
ans = (sym) a
ans = (sym) c
ans = (sym) b
ans = (sym) d
Calling each element:
ans = (sym) a
ans = (sym) b
ans = (sym) c
ans = (sym) d
References:
- https://www.gnu.org/software/octave/doc/v4.0.0/Structure-Arrays.html
- http://mattpap.github.io/scipy-2011-tutorial/html/installing.html
- https://github.com/cbm755/octsympy
- https://askubuntu.com/questions/737746/installing-symbolic-package-in-octave
- https://github.com/sympy/sympy
精彩评论