Computational Mathematics in Symbolic Math Toolbox
This example provides an overview of the Symbolic Math Toolbox™ which offers a complete set of tools for computational and analytical mathematics.
此示例包括
变量、表达式、有趣ctions and Equations
Substitution and Solving
Simplification and Manipulation
Calculus (Differentiation, Integration, Limits, Series)
微分方程
Linear Algebra
图形
For more details seeGet Started with Symbolic Math Toolbox。For more details on documenting and sharing your mathematics see在实时编辑器中创建实时脚本。
变量、表达式、有趣ctions and Equations
Variables in MATLAB® are by default double-precision. The Symbolic Math Toolbox extends this by allowing you to express numbers in exact symbolic form usingsym
并使用可变精确使用vpa
。
pi/6 + pi/4
ANS = 1.3090
sym(pi/6) + sym(pi/4)
ans =
vpa(pi/6) + vpa(pi/4)
ans =
Symbolic variables can be used in mathematical expressions, functions and equations including trigonometric, logarithmic, exponential, and special functions. You can create symbolic expressions and perform mathematical calculations on them.
symsxylog(x) + exp(y)
ans =
You can also create piecewise functions.
y(x)=零件(x <0,-1,x> 0,1)
y(x)=
Create and evaluate创建符号功能。找到价值f
at
。
symsf(x)f(x) = x^4-2*x^3+6*x^2-2*x+10
f(x) =
f(-5)
ans =
Find the intersection between lines
和
usingsolve
。使用==运算符等等同。
symsy1y2y1 = x+3;y2 = 3*x;解决(y1 == y2)
ans =
Makeassume
在符号变量上。有4种解决方案万博 尤文图斯
, two real and two complex. Assuming that
是真实的
,只有一个解决方案。
symsx解决(x^4 == 1)
ans =
假设(x,'真实的')假设(x> 0)假设(x)
ans =
解决(x^4 == 1)
ans =
假设(x,'clear')
Substitution and Solving
The Symbolic Math Toolbox supports evaluation of mathematical functions by substituting for any part of an expression using潜艇
。您可以替换数字值,其他符号变量或表达式,向量或矩阵。符号数学工具箱支持使用方程式和方程式的求解万博1manbetxsolve
。它支持万博1manbetx求解多元方程,解决不平等问题并通过假设解决。万博 尤文图斯通过使用可变精确算术,可以在高精度上象征性或数字上找到解决方案。
用符号变量进行替换。代替 into
symsxxosubs(x^2+1,x,xo-1)
ans =
代替multiple values. For example, evaluate 通过替换 。
symsabc潜艇(cos(a) + sin(b) - exp(2*c), [a b c], [pi/2 pi/4 -1])
ans =
Create and solve equations. Find the zeros of 。
solve(9*x^2 - 1 == 0)
ans =
求解一般二次方程 和use subs to evaluate that solution for 。
eqn = a*x^2 + b*x + c == 0;sol = solve(eqn)
sol =
潜艇(sol,[a b c],[9 0 -1])
ans =
当需要确切的结果或高精度时,象征性或具有可变精确算术的求解。图 is very flat near its root.
symsxf(x)assume(x>0) f(x) = 6*x^7-2*x^6+3*x^3-8; fplot(f) xlim([-10 10]) ylim([-1e3 1e3])
DoubleSol = roots([6 -2 0 0 3 0 0 -8])% double-precision
doubleSol =7×1 complex1.0240 + 0.0000i 0.7652 + 0.8319i 0.7652 - 0.8319i -0.8808 + 0.5043i -0.8808 - 0.5043i -0.2297 + 0.9677i -0.2297 - 0.9677i
symssol =solve(f)% 精确的。根对象将零存储用于符号计算
symssol =
vpasol =vpasolve(f)% variable-precision
vpasol =
Simplification and Manipulation
符号数学工具箱支持万博1manbetxFormula Manipulation and Simplification数学功能。大多数数学表达式可以以不同的方式表示,但是数学上等效的形式和符号数学工具箱都支持许多操作,包括分解或扩展表达式,结合术语,重写或重新排列的表达式以及基于假设的简化。万博1manbetx
执行多项式乘法并简化结果,表明 simplifies to 。
simplify((x - 1)*(x + 1)*(x^2 + x + 1)*(x^2 + 1)*(x^2 - x + 1)*(x^4 - x^2 + 1))
ans =
Apply trigonometric identities to simplifications, for example 。
组合(2*sin(x)*cos(x) +(1- cos(2*x))/2 + cos(x)^2,'sincos')
ans =
Factor or expand multivariate polynomials.
symsxyfactor(y^6-x^6)
ans =
f(x)=(x^3 + 7);扩展(f(y-1))
ans =
找到功能组成 。
f(x) = sqrt(log(x)); g(x) = sqrt(1-x); h = compose(g,f,x)
h(x) =
微积分(分化,集成,极限,系列等)
The Symbolic Math Toolbox has a full set of calculus tools for applied mathematics. It can perform multivariate symbolic integration and differentiation. It can generate, manipulate, and perform calculations with series.
Find the derivative of 。
diff(sin(x))
ans =
Find the derivative of 使用链条规则。
diff(x^2+sin(2*x^4)+1,x)
ans =
Find the indefinite integral for 。
int(exp(-x^2/2),x)
ans =
找到确定的积分 for 从 to 。
int(x*log(1+x),0,1)
ans =
Show that at 通过计算泰勒系列扩展 for around the point 。
symsxt =泰勒(sin(x)/x)
t =
潜艇(T, x, 0)
ans =
Show that 是不连续的 by showing that the left and right limits are not equal. 。
limit(tan(x),x,pi/2,'剩下')
ans =
limit(tan(x),x,pi/2,'正确的')
ans =
limit(tan(x),x,pi/2)
ans =
微分方程
符号数学工具箱可以分析求解Solve a System of Differential EquationsusingDSOLVE
。
解决一阶ode 。
symsaby(x)dsolve(diff(y)== -a*y)
ans =
Solve the same ODE with the initial condition 。
DSOLVE(diff(y)== -a*y,y(0)==b)
ans =
求解耦合的一阶ODES系统 和 。
symsx(t)y(t)z = dsolve(diff(x)== y,diff(y)== -x);disp([Z.X; Z.Y])
Linear Algebra
The Symbolic Math Toolbox can work with symbolic vectors and matrices. It can computeeig
of symbolic matrices.
Perform matrix multiplication where 和
symsabcdsymsx1x2x = [x1;x2];a = [a b;c d];b = a*x
b =
Find the determinant of A.
det(a)
ans =
找到A的特征值。
lambda =eig(A)
lambda =
图形
The Symbolic Math Toolbox supports analytical plotting in 2D and 3D.
fplot(tan(x))
Plot the parametric curve 和 。
symstx = t*sin(5*t);y = t*cos(5*t);fplot(x,y)网格上
绘制3D参数曲线
,
和
从[-10,10]
with a dashed red line.
symstXT = EXP(ABS(t)/10)。*sin(5*abs(t));yt = exp(abs(t)/10)。*cos(5*abs(t));zt = t;h = fplot3(XT,YT,ZT,[-10,10],“——r”);
绘制3D表面 。
symsxyfsurf(sin(x) + cos(y))
Plot the 2D contours of the same surface.
fcontour(sin(x) + cos(y))