MATLAB Symbolic Math Toolbox

来源:互联网 发布:lol mac国服怎么取消了 编辑:程序博客网 时间:2024/06/05 11:56

Symbolic Math Toolbox 是MATLAB进行符号运算的工具箱,其符号运算引擎是MuPAD,具有比较强大的符号计算功能。但与专业的符号运算软件Mathematics比较,还是有欠缺,具体的比较不在本文考虑范围。


符号变量及普通变量的差别

符号变量是一个精确的值,而普通的变量是一个近似数值。比如在 matlab 中运行如下代码:

sin(sym(pi))sin(pi)

第一个是符号变量,第二个是普通的变量,结果的差别非常明显。


sym 及 syms 函数

matlab 中使用 sym 以及 syms 函数来定义符号变量,比如使用

x = sym('x');syms x

来定义符号变量 x,两种方法是等价的。我比较喜欢第二种,可以简便地定义多个符号变量,如

syms x y z

但在定义一个符号常数时,一般使用 sym 函数,比如

f = sym(5)

此外,还可以定义符号函数变量,如 (记得清除已定义的 f 变量)

syms x y a bf(x,y) = sin(x) + yf(a,b)  %将符号变量 a,b 代入 f 的表达式

符号运算

符号运算大概包括加减乘除、微积分、多项式运算、解方程和一些特殊的函数运算等等。这里主要介绍微积分、简单代数方程的求解以及一些特殊运算。

微积分

主要是 diff 和 int 函数。
1.diff 函数:

syms x yf = sin(x) + cos(y)diff(f)      %对 x 求偏导diff(f,x)    %对 x 求偏导diff(f,x,3)  %对 x 求三次偏导diff(f,y)    %对 y 求偏导

2.int 函数

syms x yf = sin(x) + cos(y) int(f)           %对 x 积分int(f,x)         %对 x 积分int(f,y)         %对 y 积分int(f,x,[0,1])   %对 x 求定积分,上下限分别为 0 和 1

多次求导以及多重积分就是对 diff 以及 int 函数的嵌套重复使用。
关于变分下次另开专题,先给自己立个flag。

解代数方程

求解代数方程主要就是使用 solve 函数,例子如下:

syms xf = x^2-1 == 0solx = solve(f)syms x y a[solx,soly] = solve(x^2*y^2 == 0, x-y/2 == a)

此外重要的是 assume 函数的使用,这一函数可以对符号变量进行限制假定,例如:

syms xsolve(x^7 + 2*x^6 - 59*x^5 - 106*x^4 + 478*x^3 + 284*x^2 - 1400*x + 800, x)assume(x > 0)solve(x^7 + 2*x^6 - 59*x^5 - 106*x^4 + 478*x^3 + 284*x^2 - 1400*x + 800, x)

两次的结果是不一样的,可以看到第二次的结果 x 都大于 0,满足了对 x 的假设。清楚对 x 的假设应该使用下述代码:

assume(x,'clear')

此外第二个方程也可用如下形式:

solve(x^7 + 2*x^6 - 59*x^5 - 106*x^4 + 478*x^3 + 284*x^2 - 1400*x + 800, x>0)
特殊的函数

一些常用的函数列举如下:

---------------------------------------------------------------------------syms x yf = x^3 + 2*x^2 - x^2 + y^4simplify(f) %化简表达式---------------------------------------------------------------------------syms xexpand((x - 2)*(x - 4)) %展开表达式---------------------------------------------------------------------------syms xF = factor(x^2-1)   %因式分解---------------------------------------------------------------------------pretty(F)   %使表达式显示好看点,但在新版本的 matlab 中,使用 Live Scripts 可以显示更加符合数学书写习惯的表达式---------------------------------------------------------------------------syms xtaylor(exp(x))  %泰勒级数,最高阶为5,可以使用'order'选项修改taylor(sin(x))taylor(cos(x))---------------------------------------------------------------------------syms xf = sin(x)/xlimit(f,x,0)    %求 x->0 时的极限---------------------------------------------------------------------------syms tf = exp(-t^2)ft_f = fourier(f)   %傅里叶变换---------------------------------------------------------------------------syms wF = exp(-w^2/4)ifourier(F) %傅里叶逆变换---------------------------------------------------------------------------syms xf = 1/sqrt(x)laplace(f)  %拉普拉斯变换---------------------------------------------------------------------------syms x y zf(x, y, z) = 1/x + y^2 + z^3L = laplacian(f, [x y z])   %求标量函数的拉普拉斯

最后一个是关于卷积的,只是从 Github 上复制来的代码,原网址在这里

function c = symconv(a, b)    % In short, this function takes a sym, returns a sym (usually)    %    % Convolve two symbolic expressions, a and b.    %    % Author: Edward Peguillan III    % Date: 12 April, 2016, 21:49 UTC    %    %                 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE    %                     Version 2, December 2004    %     %  Copyright (C) 2016 Ed Peguillan III <yankee14.ed@gmail.com>    %     %  Everyone is permitted to copy and distribute verbatim or modified    %  copies of this license document, and changing it is allowed as long    %  as the name is changed.    %     %             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE    %    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION    %     %   0. You just DO WHAT THE FUCK YOU WANT TO.    %    % If u and v arent of class sym, then get out    is_a_sym = isa(a, 'sym'); is_b_sym = isa(b, 'sym');    if (isa(a, 'sym') + isa(b, 'sym')) < 2        error('Input to symconv must be (sym, sym), not (%s, %s).', ...            class(a), class(b));    end    var = symvar(a);    if(var ~= symvar(b))        error('Input to symconv must be functions of the same variable.');    end    syms tau real;    % By the definition of convolution:    atau = subs(a, var, var - tau); % transform all to the tau axis    btau = subs(b, var, tau); % give one the t-tau    c = int(atau * btau, tau, -inf, inf);    % Preemptively repair dumb expressions:    c = rewrite(c, 'heaviside');    c = rewrite(c, 'exp');end
原创粉丝点击