25

在 MatLab 中,您可以很容易地声明符号:

syms a,b
mat = [a,b]

但是,当我尝试在 Octave 中复制它时,我遇到了一个错误。这是我正在使用的代码:

> 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

你如何在八度音阶中声明一个符号矩阵?

4

6 回答 6

17

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

octave> pkg install -forge symbolic

如果您安装了 python 和 sympy,它将从 octave forge 为您安装软件包。我用谷歌来弄清楚如何安装 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)

我发现这个包在为我的 Robotic Manipulators 类计算旋转矩阵时非常有帮助。希望这可以帮助。

这是我的脚本的一部分以获取更多示例:

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(θ)    ⎦
于 2016-03-09T05:30:22.070 回答
9

会有帮助吗?

看起来您可能需要符号工具箱包,请在此处参考。

于 2010-04-07T21:56:19.570 回答
7

安装符号工具箱后(您可以在某些环境中通过发出 来执行此操作sudo apt-get install octave-symbolic),您必须执行以下操作:

symbols
x = sym('x')

但请注意,Octave 处理符号表达式的函数比 MATLAB 的要差得多。

于 2013-02-28T23:09:28.897 回答
1

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

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

在您的情况下也会发生同样的情况,即调整包含符号值的矩阵的大小

于 2015-01-08T10:23:10.540 回答
1

后人的另一个例子。

我使用http://octave-online.net/来开发和运行这个 octave 脚本。

注意:我将输出作为注释包含在内以显示结果。

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⎟
#    ⎝-λ - μ    ⎠
于 2017-04-05T03:51:38.870 回答
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

符号数组

您可以将 , 等替换为, @sin,等。@cossym("a")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
于 2016-11-26T23:18:57.427 回答