1、什么是符号运算
数值运算中,必须先对变量赋值;在符号运算中没有这个必要;符号计算的指令调用十分的简单,但是计算索要的时间比较长。
字符串表达式: 用单引号' '括起来,其中可以是函数表达式,也可以是方程;
class( )返回对象的类型
2、创建符号对象以及返回数据类型
%% Define the symbolic
a=sym('a');
b=sym('b');
classa=class(a);
classb=class(b);
%% Using syms defining several symbolics
syms c d e f;
whos
%% Using sym(constant) defining constant symbolic
a1= 3/4;
b1= '3/4';
c1= sym(3/4);
d1= sym('3/4');
whos
3、符号函数和符号方程
%% Symbolic function and equation
syms x y z a b c;
f1=x*y/z;
f2=x^2+y^2+z^2;
f3=f1/f2;
e1=sym('a*x^2+b*x+c') % 可以不加' '.
e2=sym('sin(x)^2+2*cos(x)=1') % 必须加' ',由于有=
e3=sym('Dy-y=x') % 必须加' ',由于有D
4、符号矩阵
%% Symbolic Matrix
A=sym('[a, b; c, d]');
% Answer is :
% [ a, b]
% [ c, d]
5、符号表达式的运算
%% Usage of collect
syms x t;
f=(x-1)*(x-2)*(x-3);
collect(f) %合并同类项
%% Usage of expand
% polynomial, exp function, log function, angle function
syms x y;
f=(x+y)^3;
f1=expand(f)
%% 系数为有理数的多项式分解,不能分解就返回函数本身
syms x y;
factor(x^3-y^3);
% 化简
S=sym('[(x^2+5*x+6)/(x+2); sqrt(16)]');
simplify(S);
6、符号微分和积分
%% Differential and integration
% diff(f) differentiate f to the default variable x
% diff(f,v) differentiate f to the variable v
% diff(f,n) differentiate f n times to the default variable x
% diff(f,v,n)
syms a x;
f=sin(a*x);
dfx=diff(f)
dfa=diff(f,a)
dfx2=diff(f,2)
dfa2=diff(f,a,2)
%% The limitation of the function F
% limit(F, x, a) x->a
% limit(F, a) default variable->a
% limit(F) default variable->0
% limit(F, x, a, 'left')
% limit(F, x, a, 'right')
% Not existing limitation, return NaN
syms h n x;
S=[sin(x+h)-sin(x)]/h;
limit(S,h,0);
limit(S)
limit(S,0)
limit(S,h,0,'left')
limit(S,h,0,'right')
%% Integration
% int(f) indefinite integral about the default variable
% int(f,v) indefinite integral about the variable v
% int(f,v,a,b) definite integral about v between a and b
% int(formulation, variable, upper, down)
int(-2*x/(1+x^2)^2)
int(log(x))
int(log10(x))
int(sin(x),x, -pi,pi)
%% Taylor expansion
% taylor(f,n,v) 关于变量v的n阶Taylor expansion
syms x;
f=1/(2+cos(x));
r=taylor(f,8);
%% Sum
% symsum(f,v,a,b)
syms x k;
s1=symsum(1/k^2, 1, inf)
s2=symsum(x^k,k,0,inf)
syms x y;
F=int(int('x*exp(-x*y)','x'),'y')
7、符号求解
%% Solution
% solve(f,v) The solution of 'f=0'
clc
clear
% f='a*x^2+b*x+c'; % also OK
syms a b c x;
f=a*x^2+b*x+c;
solve(f);
solve(f,'b');
%% Solve the group of equations
g1='x+y+z=1';
g2='x-y+z=2';
g3='2*x-y-z=1';
g=solve(g1,g2,g3);
f=solve('x+y+z=1','x-y+z=2','2*x-y-z=1'); % equivalent to g
[x,y,z]=solve('x+y+z=1','x-y+z=2','2*x-y-z=1');
%% Sovle the differential equation
% dsolve(f,i) function f and initial condition i
% The derivate is devoted by D
% [y1,y2,...] = dsolve(x1,x2,...,xn)
dsolve('Dx=y','Dy=x','x(0)=0','y(0)=1');
dsolve('D2y=-a^2*y','y(0)=1','Dy(pi/a)=0')