当前位置:网站首页>Matlab基础(5)——符号运算
Matlab基础(5)——符号运算
2022-07-30 11:28:00 【Myster_KID】
Matlab基础(5)——符号运算
符号运算是数值计算的扩展,在运算过程中以符号表达式或符号矩阵为运算对象,实现了符号计算和数值计算的相互结合,使应用更灵活。
创建符号表达式
创建符号表达式,需要先创建符号变量,再使用它们编写表达式。
使用关键字syms创建符号变量:
syms a b c % 一次可以创建多个变量,变量之间只能用空格衔接
syms A [3 4] % 创建符号矩阵
% A =
%
% [ A1_1, A1_2, A1_3, A1_4]
% [ A2_1, A2_2, A2_3, A2_4]
% [ A3_1, A3_2, A3_3, A3_4]
syms 'A%d%d' [2 2] % 可以通过占位符%d来改变默认格式
% A =
%
% [ A11, A12]
% [ A21, A22]
syms M 3 % 3阶方阵
% M =
%
% [ M1_1, M1_2, M1_3]
% [ M2_1, M2_2, M2_3]
% [ M3_1, M3_2, M3_3]
先将变量创建好,才能将含有该变量字符串转化为符号表达式
syms x
str = 'x^3+2*x+1'; % 不识别2x,即*不可省略
S = eval(str); % 将字符串转化为符号表达式
% S =
%
% x^3 + 2*x + 1
也可以通过多项式部分提到的函数ploy2sym(p),将系数向量转化为符号表达式
P = [1 2 2 1];
S = poly2sym(P);
% S =
%
% x^3 + 2*x^2 + 2*x + 1
可以通过函数sym(A)将矩阵 A A A转化为符号表达式sym格式。只有符号表达式可以与符号表达式计算,数值表达式无法直接与符号表达式进行计算。
A = ones(2,3);
S = sym(A) % 2*3 sym
% S =
%
% [ 1, 1, 1]
% [ 1, 1, 1]
使用sym()函数处理数值表达式时,应从尽量小的单位入手,以免产生精度上的误差,如
>> sym(1/1234567) % 错误
ans =
7650239286923505/9444732965739290427392
>> 1/sym(1234567) % 正确
ans =
1/1234567
% ------------------------------------------------
>> sym(exp(pi)) % 错误
ans =
6513525919879993/281474976710656
>> exp(sym(pi)) % 正确
ans =
exp(pi)
符号矩阵运算
转置
Matlab默认符号属于复数,在使用'求转置时,会自动求出共轭转置。因此若只想求转置,应该使用.'
syms 'A%d%d' [2 3]
% A =
%
% [ A11, A12, A13]
% [ A21, A22, A23]
B = A.'
% B =
%
% [ A11, A21]
% [ A12, A22]
% [ A13, A23]
行列式
syms 'A%d%d' 2
% A =
%
% [ A11, A12]
% [ A21, A22]
d = det(A)
% d =
%
% A11*A22 - A12*A21
求逆
inv(A); % A必须是方阵,结果用A中元素表示
求秩
rank(A); % 返回一个整数
其他
| 函数 | 说明 |
|---|---|
inv(A) | 求矩阵的逆,结果用 A A A中的元素表示 |
rank(A) | 求矩阵的秩,返回一个整数 |
eig(A) | 求特征值、特征向量 |
svd(A) | 奇异值分解 |
jordan(A) | Jordan标准形运算 |
符号运算
因式分解
使用函数factor(S)实现
S = poly2sym([1 3 2]); % S = X^2+3*x+2
factor(S)
% ans =
%
% [ x + 2, x + 1]
也可用于质因数分解
S = sym(276);
factor(S) % [2 2 3 23]
表达式展开
syms x
S = eval('(x+1)*(x+2)');
expand(S) % x^2 + 3*x + 2
也可以用于三角函数、指数函数、对数函数的展开
syms x y
S = eval('sin(x+y)');
expand(S) % cos(x)*sin(y) + cos(y)*sin(x)
表达式化简
syms x
S = eval('sin(x)^2+cos(x)^2');
simplify(S) % x+1
分式通分
syms x y
S = eval('1/x+1/y');
[n, d] = numden(S)
% n - 分子 n=x+y
% d - 分母 d=x*y
代入/计算结果
通过函数subs(S,old,new)实现,返回值仍是sym类型。
syms F m a Ff
str = 'Ff+F';
S = eval(str);
S = subs(S,F,a*m) % 用a*m代换F
% S =
%
% Ff + a*m
res = subs(S,[a m Ff],[2 10 15]) % 分别给[a m Ff]赋值为[2 10 15]
% res =
%
% 35
边栏推荐
- Scheduling of combined electric-heating system based on multi-objective two-stage stochastic programming method
- 单片机开发之LCD1602显示实验
- 基于MySQL数据库,Redis缓存,MQ消息中间件,ES搜索引擎的高可用方案解析
- Apifox 生成接口文档 教程与操作步骤
- 《跟唐老师学习云网络》 - 问题定位 - 主机通但容器不通
- win下怎么搭建php环境的方法教程
- [Cloud-Building Co-creation] Huawei Cloud and Hongmeng collaborate to cultivate innovative developers
- 基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)
- External Force Estimation Based on Time Delay Estimation with Perturbed Kalman Filter
- Drools 规则引擎一文读懂
猜你喜欢

Current relay JL-8GB/11/AC220V

24. 两两交换链表中的节点

LeetCode_236_Last Common Ancestor of a Binary Tree

Drools 规则引擎一文读懂

基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)

Verilog grammar basics HDL Bits training 08

真正懂经营管理的CIO具备哪些特质

The battle-hardened programmer was also deceived by a fake programmer from a certain fish. The trust between programmers should be the highest, and he alone destroyed this sense of trust

contentDocument contentWindow,canvas 、svg,iframe

Horizontal comparison of 5 commonly used registration centers, whether it is used for interviews or technical selection, is very helpful
随机推荐
2022-07-29 顾宇佳 学习笔记 异常处理
我又造了个轮子:GrpcGateway
360闷声干大事获赞无数,数字安全如何保障?还得看企业安全云
Typroa alternative tool marktext
京东校招笔试题+知识点总结
TensorFlow custom training function
Horizontal comparison of 5 commonly used registration centers, whether it is used for interviews or technical selection, is very helpful
单片机工程师笔试题目归纳汇总
电压继电器HDY-A/1-220VAC-1
Testability of Fuzzy Discrete Event Systems
面试官:Redis中的布隆过滤器与布谷鸟过滤器,你了解多少?
高能产出!腾讯内部的MyCat中间件手册,理论实操齐下
云原生应用的概念和云原生应用的 15 个特征
Current relay JL-8GB/11/AC220V
Based on sliding mode control of uncertain neutral system finite time stable
优酷VIP会员周卡只需7.5元,看《沉香如屑》用优酷视频
又爆神作!阿里爆款MySQL高级宝典开源,直抵P7
AIX shell获取前几个月时间
The use and principle of distributed current limiting reduction RRateLimiter
API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试