2010-03-18 100 views
24

在MATLAB中,你可以很容易地将符号声明:如何在Octave中声明一个符号矩阵?

syms a,b 
mat = [a,b] 

我发现了一个错误,但是,当我尝试在八度复制本。这是我使用的代码:

> 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 

你如何申报在八度的符号矩阵?

回答

10

请问this有帮助吗?

看起来你可能需要symbolic toolbox package,参考here

+1

不,我已经有符号包,但问题仍然存在。 – 2012-11-01 05:45:09

+1

@Gryllida不幸的是,我有一段时间没有使用Octave,并没有那么快的计算出东西。我热烈推荐在[Octave邮件列表](https://mailman.cae.wisc.edu/listinfo/help-octave)上询问:那里有很多有帮助和有知识的人。 – 2012-11-01 11:35:07

+0

“此”链接中的页面不存在。 – 2014-06-11 11:46:16

6

安装象征性的工具箱之后(您可以通过发出sudo apt-get install octave-symbolic做这在某些环境中),你必须做到以下几点:

symbols 
x = sym('x') 

但要注意八度的操纵符号表达式功能比MATLAB的差很多。

1

八度的符号工具箱或多或少没有用处。你不能像你的情况那样调整矩阵的大小,你不能使用“ - ”运算符。 例如,您可以区分一个简单的符号操作:

octave:1> symbols 
octave:2> q1 = sym("q1"); 
octave:3> differentiate(Sin(q1)*Cos(q1),q1) 
ans = 

-sin(q1)^2+cos(q1)^2 

,但如果你尝试这样做:

octave:6> -Sin(q1) 
error: unary operator `-' not implemented for `ex' operands 
octave:6> -q1 
error: unary operator `-' not implemented for `ex' operands 

同样的情况,你的情况,即调整包含符号值

11
矩阵

如果您还没有符号包,请下载它。从Octave命令行或gui命令行。例如

octave> pkg install -forge symbolic 

如果您安装了python和sympy,它会从八度锻造程序为您安装软件包。我用谷歌找出如何安装sympy,如果你需要帮助,请打我。

安装符号包后,使用“pkg load”导入包函数,然后使用syms函数声明符号。

octave> pkg load symbolic 

octave> syms a b 

这定义了符号a和b。

octave> syms 
Symbolic variables in current scope: 
    a 
    b 

“syms”本身将打印您定义的所有符号。

octave> mat = [a,b] 
mat = (sym) [a b] (1×2 matrix) 

octave:34> mat * 2 
ans = (sym) [2⋅a 2⋅b] (1×2 matrix) 

我发现这个软件包对我的机器人操纵器类的计算旋转矩阵很有帮助。希望这可以帮助。

下面是更多的例子我的脚本的一部分:

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(θ) ⎦ 

请了投票

0

手柄的阵列

可以使用Octave Struct Array创建这样一个符号矩阵:

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) 

运行宁本以上的回报:

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 

符号阵列

您可以通过sym("a")更换@sin@cos等,sym("b")sym("c")

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 

运行此上述回报:

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 

参考文献:

  1. https://www.gnu.org/software/octave/doc/v4.0.0/Structure-Arrays.html
  2. http://mattpap.github.io/scipy-2011-tutorial/html/installing.html
  3. https://github.com/cbm755/octsympy
  4. https://askubuntu.com/questions/737746/installing-symbolic-package-in-octave
  5. https://github.com/sympy/sympy
+0

这是一个句柄数组...不是符号变量 – rayryeng 2016-11-27 08:45:35

+0

我添加它也符合数组符号例。 – user 2016-11-27 13:08:05

1

为后代又如。

我以前http://octave-online.net/开发并运行此脚本八度。

NB:我包括作为注解输出到显示结果。

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⎟ 
# ⎝-λ - μ ⎠ 
相关问题