符号变量及其运算

  • 绪:什么是符号计算?
  • 1.字符型数据变量的创建
  • 2.符号型数据变量的创建
  • 3.符号计算的运算符与函数
  • 4.寻找符号变量
  • 5.符号精度计算
  • 6.显示符号表达式
  • 7.合并符号表达式
  • 8.展开符号表达式

绪:什么是符号计算?

所谓符号计算是指在运算时,无须事先对变量赋值,而将所得到结果以标准的符号形式来表示。
例如,在符号变量运算过程中pi就用pi表示,而不是具体的近似数值在这里插入代码片3.14或3.1415926。使用符号变量进行运算能最大限度地减少运算过程中因舍入而造成的误差。符号变量也便于进行运算过程的演示。

1.字符型数据变量的创建

var = ‘expression

字符型变量是以矩阵的形式存在于Matlab的工作空间中的

示例

>> var = 'hello world'
var =
hello world
>> size(var)
ans =1    11
>> y = '1 + sin(2*x)'
y =
1 + sin(2*x)

2.符号型数据变量的创建

符号对象中的符号常量、变量、函数和表达式,可以用sym和syms函数创建。使用class函数测试建立的操作对象为何种操作对象类型及是否为符号对象类型。

以下为Matlab自带的sym函数解释:

>> help symsym    Construct symbolic numbers, variables and objects.S = sym(A) constructs an object S, of class 'sym', from A.If the input argument is a string, the result is a symbolic numberor variable.  If the input argument is a numeric scalar or matrix,the result is a symbolic representation of the given numeric values.If the input is a function handle the result is the symbolic formof the body of the function handle.x = sym('x') creates the symbolic variable with name 'x' and stores theresult in x.  x = sym('x','real') also assumes that x is real, so thatconj(x) is equal to x.  alpha = sym('alpha') and r = sym('Rho','real')are other examples.  Similarly, k = sym('k','positive') makes k apositive (real) variable.  x = sym('x','clear') restores x to aformal variable with no additional properties (i.e., insures that xis NEITHER real NOR positive).See also: SYMS.A = sym('A',[N1 N2 ... Nk]) creates an N1-by-N2-...-by-Nk array ofsymbolic scalar variables. Elements of vectors have names of the form Ai and elementsof matrices and higher-dimensional arrays have names of the formAi1_..._ik where each ij ranges over 1:Nj.The form can be controlled exactly by using '%d' in the firstinput (eg 'A%d%d' will make names Ai1i2).A = sym('A',N) creates an N-by-N matrix.sym(A,ASSUMPTION) makes or clears assumptions on A as described inthe previous paragraph.Statements like pi = sym('pi') and delta = sym('1/10') create symbolicnumbers which avoid the floating point approximations inherent in thevalues of pi and 1/10.  The pi created in this way temporarily replacesthe built-in numeric function with the same name.S = sym(A,flag) converts a numeric scalar or matrix to symbolic form.The technique for converting floating point numbers is specified bythe optional second argument, which may be 'f', 'r', 'e' or 'd'.The default is 'r'.'f' stands for 'floating point'.  All values are transformed fromdouble precision to exact numeric values N*2^e for integers N and e.'r' stands for 'rational'.  Floating point numbers obtained byevaluating expressions of the form p/q, p*pi/q, sqrt(p/q), 2^q and 10^qfor modest sized integers p and q are converted to the correspondingsymbolic form.  This effectively compensates for the roundoff errorinvolved in the original evaluation, but may not represent the floatingpoint value precisely.  If no simple rational approximation can befound, the 'f' form is used.'e' stands for 'estimate error'.  The 'r' form is supplemented by aterm involving the variable 'eps' which estimates the differencebetween the theoretical rational expression and its actual floatingpoint value.  For example, sym(3*pi/4,'e') is 3*pi/4-103*eps/249.'d' stands for 'decimal'.  The number of digits is taken from thecurrent setting of DIGITS used by VPA.  Using fewer than 16 digitsreduces accuracy, while more than 16 digits may not be warranted.For example, with digits(10), sym(4/3,'d') is 1.333333333, whilewith digits(20), sym(4/3,'d') is 1.3333333333333332593,which does not end in a string of 3's, but is an accurate decimalrepresentation of the double-precision floating point number nearestto 4/3.See also syms, class, digits, vpa.sym 的参考页

sym函数:可以生成单个的符号变量

  • var = sym(‘var’,set):创建一个符号变量,并设置符号对象的格式,set可以不选。

    • ‘position’:限定var表示正的实型符号变量
    • ‘real’:限定var为实型符号变量
    • ‘unreal’:限定var为非实型符号变量
  • sym(‘var’,‘clear’):清除先前设置的符号变量var
  • num = sym(num,flag):将一个数值转换为符号形式,输入参数flag为转换的符号对象应该符合的格式类型
    • ‘r’:最接近有理表示,为系统默认设置
    • ‘e’:带估计误差的有理表示
    • ‘f’:十六进制浮点表示
    • ‘d’:最接近十进制浮点精确表示
  • A = sym(‘A’,dim):创建一个矢量或矩阵的符号变量
  • A = sym(‘A’,set):创建一个符号矩阵,set用于设置矩阵的维数
  • sym(A,‘clear’):清除前面已创建的符号矩阵A
  • f(arg1,…,argN) = sym(‘f(arg1,…,argN’):根据f指定的输入参数arg1,…,argN创建符号变量f(arg1,…,argN)

示例:利用sym函数创建符号对象

>> %利用sym函数创建符号对象
>> sqrt(3)
ans =1.7321
>> a = sqrt(sym(3))
a =
3^(1/2)
>> a = sym(sqrt(3))
a =
3^(1/2)

syms函数:可以创建任意多个符号变量

  • syms var…varN:创建符号变量var…varN
  • syms var…varN set:set指定符号对象的格式
    • ‘position’:限定var表示正的实型符号变量
    • ‘real’:限定var为实型符号变量
  • syms var…varN clear:清除前面已经定义好的符号对象
  • syms f(arg1,…,argN):创建符号函数f,函数中包含多个符号变量

示例:利用syms函数创建符号表达式

>> %利用sym函数创建符号表达式
>> syms s(t) f(x,y)
>> s(t) = 3*t + 2
s(t) =
3*t + 2
>> s(2)
ans =y
8
>> f(x,y) = x + 3*y
f(x, y) =
x + 3*y
>> f(2,3)
ans =
11

也可以利用sym和syms生成符号矩阵
示例:

>> %利用sym和syms生成符号矩阵
>> m1 = [1,2+x,1;3-x,1,4+y;1,2+y,0]
m1 =
[     1, x + 2,     1]
[ 3 - x,     1, y + 4]
[     1, y + 2,     0]
>> m2 = sym('[[1,2+x,1;3-x,1,4+y;1,2+y,0]]')
m2 =
[ [1, x + 2, 1], [3 - x, 1, y + 4], [1, y + 2, 0]]

3.符号计算的运算符与函数

Matlab采用了全新的数据结构、面向对象编程和重载技术,使得符号计算和数值计算在形式上和风格上浑然统一
算数运算符号:
(1)运算符号“+”,“-”,“*”,“/”,“\”,“^”分别实现符号矩阵的加法、减法、乘法、左除、右除和求幂运算
示例:

%利用sym实现矩阵的加法
>> A = sym('[x^2 3;4 * xcos(x)]');
>> B = sym('[1/x^2 2*x;3 x^2+x]');
>> C = A + B
C =
[   1/x^2 + x^2, 2*x + 3]
[ 4*xcos(x) + 3, x^2 + x]

(2)运算符号“.*”,“./”,“.\”,“.^”分别实现“元素对元素”的乘法、左除、右除和求幂运算

>> %利用syms实现矩阵的点乘
>> syms a b c d e f g h;
>> A = sym('[a,b;c,d]')
A =
[ a, b]
[ c, d]
>> B = sym('[e,f;g,h]')
B =
[ e, f]
[ g, h]
>> R = A * B
R =
[ a*e + b*g, a*f + b*h]
[ c*e + d*g, c*f + d*h]
>> R1 = A .* B
R1 =
[ a*e, b*f]
[ c*g, d*h]

(3)运算符号“ ’ ”," .’ "分别实现符号矩阵的共轭和非共轭转置

>> %符号矩阵的共轭和非共轭转置
>> syms a b c d;
>> R = A'
R =
[ conj(a), conj(c)]
[ conj(b), conj(d)]
>> R1 = A.'
R1 =
[ a, c]
[ b, d]

关系运算符号:

与数值计算中关系运算符号相区别的是,符号计算中的关系运算符还有以下两种:

  • 运算符号“==”表示对运算符两边的符号对象进行“相等”的比较,返回值“1”表示相等,“0”表示不等
  • 运算符号“~=”表示对运算符号两边的符号对象进行不相等的标记。“1”表示不相等,“0”表示相等

复数函数:

复数函数包括复数的共轭、实部、虚部和模函数,与数值计算中是一致的

矩阵代数函数:

符号计算中,常用的矩阵代数函数有:diag函数,triu函数,tril函数,inv函数,det函数,rank函数,rref函数,null函数,colspace函数,ploy函数、expm函数,eig函数和svd函数

%利用diag函数取符号矩阵对角线元素向量
>>  f = sym('[1 2 1; 2 3 5;1 7 9]')
f =
[ 1, 2, 1]
[ 2, 3, 5]
[ 1, 7, 9]
>> a = diag(f)
a =139

4.寻找符号变量

Matlab中的符号对象可以是符号常量也可以是符号变量,findsym函数可以找到符号表达式中的符号变量

  • findsym(s,n):寻找符号表达式s中的n个符号变量,若没有指定n,则返回s中的全部符号变量。
%利用findsym函数寻找符号表达式中的符号变量
>> syms a b x y;
>> f = a ^ 2 + 6 * b + cos(x - 2) + log(5 + y) + 4 - 5i
f =
a^2 + 6*b + cos(x - 2) + log(y + 5) + (4 - 5i)
>> findsym(f)
ans =
a,b,x,y
>> findsym(f,2)
ans =
x,y

5.符号精度计算

符号计算的一个显著特点是:由于计算过程中不会出现舍入误差,从而可以得到任意精度的数值解。因此,如果想要使得计算结果精确,就可以牺牲计算时间和存储空间,用符号计算来获得计算精度。

在符号运算工具箱中,有三种不同类型的算术运算。

  • 数值类型:Matlab的浮点算术运算,最快的运算,需要的计算机内存很小,但是计算结果不精确
  • 有理数类型:Maple的精度符号计算
  • VPA类型:Maple的任意精度算术运算

digits函数:digits函数用于设定所用数值的精度

  • digits(d):符号对象的近似解的精度为d位有效数字,参数d的默认值为32位
  • d = digits:得到当前采用的数值计算的精度
%Matlab符号函数的的精度
>> digits
Digits = 32
>> a = sym(1.3,'d')
a =
1.3000000000000000444089209850063
>> digits(50)
>> a = sym(1.3,'d')
a =
1.3000000000000000444089209850062616169452667236328

vpa函数:用于进行可控精度运算

  • R = vpa(A):计算符号矩阵A的近似解,精度为函数digits(d)指定的有效位数
  • R = vpa(A,d):计算符号矩阵A的近似解,有效位数由参数d指定
%使用vpa函数进行可控精度运算
>> a = vpa(hilb(2))
a =
[ 1.0,                                0.5]
[ 0.5, 0.33333333333333333333333333333333]
>> b = vpa(hilb(3),6)
b =
[      1.0,      0.5, 0.333333]
[      0.5, 0.333333,     0.25]
[ 0.333333,     0.25,      0.2]

6.显示符号表达式

符号表达式的显示过程中,默认采用Matlab形式的显示,除了默认的显示方式外,还可以使用pretty函数,允许用户将符号表达式显示为符合一般数学表达习惯的数学表达式。

pretty(x):将符号表达式用书写方式显示出来,使用默认的宽

%显示符号表达式
>> sym x;
>> s = solve(x^4 + 2*x + 1,x,'MaxDegree',3);
>> pretty(s)
/         -1         \
|                    |
|           2    1   |
|    #2 - ---- + -   |
|         9 #2   3   |
|                    |
|   1         #2   1 |
| ---- - #1 - -- + - |
| 9 #2         2   3 |
|                    |
|        1    #2   1 |
| #1 + ---- - -- + - |
\      9 #2    2   3 /
where/   2       \sqrt(3) | ---- + #2 | 1i\ 9 #2      /#1 == ------------------------2/ sqrt(11) sqrt(27)   17 \1/3#2 == | ----------------- - -- |\         27          27 /

7.合并符号表达式

collect函数用于实现将符号表达式中的同类项进行合并
ss

  • R = collect(s):将表达式s中相同次幂的项合并,系统默认为按照x的相同次幂项进行合并
  • R = collect(s,v):将表达式s按照v的次幂项进行合并,输入参数s可以是表达式,也可以是一个符号矩阵
%合并符号表达式
>> syms x y
>> collect((exp(x) + x)*(x + 2))
ans =x^2 + (exp(x) + 2)*x + 2*exp(x)
>> collect(x^2*y + y*x - x^2 - 2*x,x)
ans =
(y - 1)*x^2 + (y - 2)*x

8.展开符号表达式

expand函数用于将符号表达式展开
s

  • expand(s):表达式S中如果包含函数,Matlab会利用恒等式变型将其写成相应的形s式。
  • expand(s,Name,Value):设置展开式的参数名Name及其对应的参数值Value
%展开符号表达式
>> syms x y
>> expand((x - 2) * (x - 4))
ans =
x^2 - 6*x + 8
>> expand(cos(x + y))
ans =
cos(x)*cos(y) - sin(x)*sin(y)

Matlab学习笔记(1) - 符号变量及其运算相关推荐

  1. matlab学习笔记之——符号函数应该怎么作图?

    如何用符号函数来做图 最近在学习使用符号函数,在此过程中遇到了许多问题,尤其是我不太清楚应该用什么函数来画符号函数的图.之后我问了我的老师,也看了很多博文,总结了一些符号函数画图的方法,希望可以帮到大 ...

  2. MATLAB学习笔记(一)

    MATLAB 一.安装的目录结构 二.常用的菜单及工具 布局 设置路径 设置工作路径 三.常用命令 四. 基础知识 数据类型 复数类型 `inf 和 NaN` 字符与字符串 函数句柄 结构体 结构体的 ...

  3. MATLAB学习笔记2:MATLAB基础知识(下)

    阅读前请注意: 1. 该学习笔记是华中师范大学HelloWorld程序设计协会2021年寒假MATLAB培训的学习记录,是基于培训课堂内容的总结归纳.拓展阅读.博客内容由 @K2SO4钾 撰写.编辑, ...

  4. MATLAB学习笔记3:MATLAB编程基础(前半)

    阅读前请注意: 1. 该学习笔记是华中师范大学HelloWorld程序设计协会2021年寒假MATLAB培训的学习记录,是基于培训课堂内容的总结归纳.拓展阅读.博客内容由 @K2SO4钾 撰写.编辑, ...

  5. MATLAB学习笔记1:MATLAB概述

    阅读前请注意: 1. 该学习笔记是华中师范大学HelloWorld程序设计协会2021年寒假MATLAB培训的学习记录,是基于培训课堂内容的总结归纳.拓展阅读.博客内容由 @K2SO4钾 撰写.编辑, ...

  6. MATLAB学习笔记0:学习须知

    阅读前请注意: 1. 该学习笔记是华中师范大学HelloWorld程序设计协会2021年寒假MATLAB培训的学习记录,是基于培训课堂内容的总结归纳.拓展阅读.博客内容由 @K2SO4钾 撰写.编辑, ...

  7. 数字图像处理MATLAB学习笔记(一)

    数字图像处理MATLAB学习笔记(一) 灰度转换与空间滤波 本节主要使用Matlab语言进行灰度转换与空间滤波的使用 并对相关数学原理进行总结 1. Intensity Transformer Fun ...

  8. MATLAB学习笔记(二)

    MATLAB学习笔记(二) 一.矩阵运算 矩阵分析 向量和矩阵的范数运算 矩阵的秩 矩阵的化零矩阵 矩阵的化简rref()函数 线性方程组 超定线性方程组求解 矩阵分解 1.对称正定矩阵的Choles ...

  9. linspace函数matlab_从零开始的matlab学习笔记——(29)泰勒逼近函数

    matlab应用--求极限,求导,求积分,解方程,概率统计,函数绘图,三维图像,拟合函数,动态图....更多内容尽在个人专栏:matlab学习 上一节我们成功制作了能自己转圈的三维螺旋线,这里我们再来 ...

最新文章

  1. asp.NET自定义服务器控件内部细节系列教程五
  2. SIGIR 2020 | 第四范式提出深度稀疏网络模型,显著提升高维稀疏表数据分类效果...
  3. StoryBoard之User Defined Runtime Attributes的使用
  4. 【Django】基于Django架构网站代码的目录结构---转载
  5. 抢占乡镇渠道 中国手机厂商比苹果有经验
  6. java invokeall 阻塞_ExecutorService.invokeAll并关闭
  7. java string类型详解_Java字符串类型详解
  8. easyui修改css样式,修改easyui的easyloader的默认css目录路径
  9. 我的conky 配置(拆分版)
  10. Docker默认存储路径修改
  11. 中南大学计算机网络期末试卷,中南大学计算机网络期末复习试卷1
  12. APP扫码登录WEB系统
  13. html js格式化,Js/html格式化在线工具
  14. 文字编码和Unicode
  15. imitate wechat - 4
  16. 能删除Windows下“本地安装源 (Msocache)”吗?
  17. lammps复杂形状建模案例——胶囊粒子分子模型
  18. 基于量化交易回测的金融股票案例基础知识
  19. 一款炫丽的网页播放器插件
  20. web3d-手机产品展示_onePlus6

热门文章

  1. 春联大全·七字联(1)
  2. MFC的进度栏的编程
  3. Tensorflow Test1
  4. self和Self、== 和===的区别
  5. 高鸿业微观经济学第7版笔记和课后习题答案
  6. C语言:extern用法
  7. android图形框架之surfaceflinger分析(一)
  8. Marshmallow 库
  9. skipped: maximum number of running instances reached (1)
  10. 文本分类和聚类有什么区别