MATLAB的符号运算基础

本文介绍了MATLAB中的符号运算基础知识,包括如何创建符号变量及常数,并通过实例演示了符号运算的基本操作,如展开、合并同类项、因式分解及化简等。此外,还对比了数值运算与符号运算的不同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

    在数学运算中,运算的结果如果是一个数值,可以称这类运算为数值运算;如果运算结果为表达式,在MATLAB中称为符号运算,符号计算是对未赋值的符号对象(可以是常数、变量、表达式)进行运算和处理。MATLAB具有符号数学工具箱(SymbolicMath Toolbox),将符号运算结合到MATLAB的数值运算环境。符号数学工具箱是建立在Maple软件基础上的。

(一)  符号变量建立符号变量和符号常数

       建立符号变量的方法有两种,应用,应用sym与syms函数,通常应用sym建立符号表达式,应用syms同时定义多个符号变量。

(1)函数:sym

 功能:用来建立单个符号量

 语法形式:sym(‘表达式或变量’)

 举例

 

a = sym('a');     %定义符号变量a,下同
b = sym('b')
c = sym('c')
d = sym('d')  %至此定义了4个符号变量
w = 10;x = 5;y = -8;z = 11;

A = [a,b;c,d] %建立符号矩阵A
B = [w,x;y,z] %建立数值矩阵B
C = det(A); %计算符号矩阵A的行列式
D = det(B); %建立数值矩阵B的行列式


可得到如下结果:

 

 

由上述结果我们不难看出,在符号运算中是以表达式形式呈现结果的,而在数值运算中是以数值表示结果的。

(2)函数:syms

 功能:定义多个符号变量

 语法:syms Var1 Var2 ……Varn

 说明:函数定义符号变量var1,var2,……,Varn等。在用这种格式定义符号变量时无需在变量名上加字符分解符(‘’),变量间用空格而不要用逗号分隔,要用空格来分隔。

例2:应用syms函数定义符号变量

 

syms x y %同时定义x,y为符号变量
A = [sin(x) sin(y);cos(x) cos(y)]</span>

(二)基本的符号运算

 

1基本符号运算函数

在MATLAB中,有很多应用于符号运算的函数,常用的函数如下表所示:

 

函数名

功能

用法举例

sym

字符串或数值到符号的切换

sym(‘a+b’)

expand

展开

syms  x

s = (-7*x^2-8*y^2)*(-x^2+3*y^2);

expand(s)

collect(s,x)

factor(ans)

g = simple(ans)

collect

合并同类项

factor

因式分解

simplify

simple

化简

sym2poly(S)

转化S为多项式系数向量

syms x;f=’2*x^2+3*x-5’;

n = sym2poly(f)

poly2sym(n)

poly2sym(c)

转换多项式系数向量c为符号多项式


2符号表达式运算

 (1)符号表达式的四则运算

 

syms x y z; %定义x,y,z为符号变量
f1 = 2*x+x^2*x-5*x+x^3
f2 = 2*x/(5*x)
f3 = (x+y)*(x-y)</span>


 (2)因式分解与多项式展开

 

函数1:factor

功能:分解因式

语法:factor(s)

说明:s是符号表达式或符号矩阵

 

函数2:collect

功能:合并同类项

语法:collect(s)

            collect(s,x)

说明:对s按符号变量x进行同类项合并,s是符号表达式或符号矩阵。

 

举例:对表达式因式分解

 

f = sym('2*(x-1)/(x^2+2*x-3)'); %输入表达式
F = factor(f)  %对符号表达式f进行因式分解</span>

 

 

 

(3)表达式化简

 

MATLAB提供的对符号表达式化简函数为simplify。

函数:simplify

功能:对表达式进行化简

语法:simplfy(S)

 

f=cos(x)^2+sin(x)^2
simplify(f)

 

 

simplify和simple是Matlab符号数学工具箱提供的两个简化函数,区别如下:
simplify的调用格式为:simplify(S);对表达式S进行化简。
simple是通过对表达式尝试多种不同的方法(包括simplify)进行化简,以寻求符号表达式S的最简形式。
调用方式为:
[r,how]=simple(S);r为返回的简化形式,how为化简过程中使用的一种方法。how有以下几种形式:
(1)simplify 函数对表达式进行化简;
(2)radsimp函数对含根式的表达式进行化简;
(3)combine 函数将表达式中以求和、乘积、幂运算等形式出现的项进行合并;
(4)collet合并同类项
(5)factor函数实现因式分解
(6)convert函数完成表达式形式的转换

 

参考文献

[1] 刘加海,严冰等主编. MATLAB可视化科学计算[M]:浙江: 浙江大学出版社,2014.6

[2] 薛定宇,陈阳泉主编.高等应用数学问题的MATLAB求解[M]:第二版.北京: 清华大学出版社,2015.6
 

SAX符号化序列范例源码 -------------------- timeseries2symbol.m: -------------------- This function takes in a time series and convert it to string(s). There are two options: 1. Convert the entire time series to ONE string 2. Use sliding windows, extract the subsequences and convert these subsequences to strings For the first option, simply enter the length of the time series as "N" ex. We have a time series of length 32 and we want to convert it to a 8-symbol string, with alphabet size 3: timeseries2symbol(data, 32, 8, 3) For the second option, enter the desired sliding window length as "N" ex. We have a time series of length 32 and we want to extract subsequences of length 16 using sliding windows, and convert the subsequences to 8-symbol strings, with alphabet size 3: timeseries2symbol(data, 16, 8, 3) Input: data is the raw time series. N is the length of sliding window (use the length of the raw time series instead if you don't want to have sliding windows) n is the number of symbols in the low dimensional approximation of the sub sequence. alphabet_size is the number of discrete symbols. 2 <= alphabet_size > mindist_demo sax_version_of_A = 3 4 2 1 1 3 4 2 sax_version_of_B = 1 1 3 4 3 1 1 4 euclidean_distance_A_and_B = 10.9094 ans = 5.3600 ---> This is the mindist ----------------- symbolic_visual.m ----------------- This demo presents a visual comparison between SAX and PAA and shows how SAX can represent data in finer granularity while using the same, if not less, amount of space as PAA. The input parameter [data] is optional. The default # of PAA segments is 16, and the alphabet size is 4. -------- Examples: -------- You can type this up in your matlab: Recall that there are two options for timeseries2symbol. The first option is demonstrated in sax_demo.m Now here is an example of the latter. We are going to convert time series of length 50, with a sliding window of 32, into 8 symbols, with and alphabet size of 3. >> [symbolic_data, pointers] = timeseries2symbol(long_time_series,32,8,alphabet_size) symbolic_data = 1 1 3 3 3 3 1 1 1 2 3 3 3 2 1 1 1 3 3 3 3 1 1 1 2 3 3 3 2 1 1 1 3 3 3 3 1 1 1 1 3 3 3 2 1 1 1 2 3 3 3 1 1 1 1 3 3 3 2 1 1 1 2 3 3 3 1 1 1 1 3 3 3 2 1 1 1 2 3 3 pointers = 1 2 5 6 9 10 13 14 17 18 Note that each row corresponds to a subsequence (with overlap) The SAX word at 3 and 4 were omitted, since they where the same as the word at 2, same for 7 and 8, which were the same as 6 etc (look at the pointers) It might be helpful to view the data this way >> [pointers symbolic_data ] ans = 1 1 1 3 3 3 3 1 1 2 1 2 3 3 3 2 1 1 5 1 3 3 3 3 1 1 1 6 2 3 3 3 2 1 1 1 9 3 3 3 3 1 1 1 1 10 3 3 3 2 1 1 1 2 13 3 3 3 1 1 1 1 3 14 3 3 2 1 1 1 2 3 17 3 3 1 1 1 1 3 3 18 3 2 1 1 1 2 3 3 So the first word is (1 1 3 3 3 3 1 1) , the 9th word is (3 3 3 3 1 1 1 1) , the 14 word is (3 3 2 1 1 1 2 3)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值