主要内容

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 =

5 π 12

vpa(pi/6) + vpa(pi/4)
ans =
                  
                   
                    
                     1.3089969389957471826927680763665
                   
                  

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 =
                  
                   
                    
                     
                      
                       
                        
                         e
                       
                       
                        
                         y
                       
                      
                      
                       +
                      
                       
                        
                         日志
                       
                       
                        
                         (
                        
                         
                          
                           x
                         
                        
                        
                         )
                       
                      
                     
                    
                   
                  

You can also create piecewise functions.

y(x)=零件(x <0,-1,x> 0,1)
y(x)=

{ - 1 如果 x < 0 1 如果 0 < x

Create and evaluate创建符号功能。找到价值fat x = - 5

symsf(x)f(x) = x^4-2*x^3+6*x^2-2*x+10
f(x) =
                  
                   
                    
                     
                      
                       
                        
                         x
                       
                       
                        
                         4
                       
                      
                      
                       -
                      
                       
                        
                         2
                        
                        
                        
                         
                          
                           x
                         
                         
                          
                           3
                         
                        
                       
                      
                      
                       +
                      
                       
                        
                         6
                        
                        
                        
                         
                          
                           x
                         
                         
                          
                           2
                         
                        
                       
                      
                      
                       -
                      
                       
                        
                         2
                        
                        
                        
                         x
                       
                      
                      
                       +
                      
                       10
                     
                    
                   
                  
f(-5)
ans =
                  
                   
                    
                     1045
                   
                  

Find the intersection between lines y 1 y 2 usingsolve。使用==运算符等等同。

symsy1y2y1 = x+3;y2 = 3*x;解决(y1 == y2)
ans =

3 2

Makeassume在符号变量上。有4种解决方案万博 尤文图斯 x 4 = 1 , two real and two complex. Assuming that x 是真实的 x > 0 ,只有一个解决方案。

symsx解决(x^4 == 1)
ans =

( - 1 1 - i i )

假设(x,'真实的')假设(x> 0)假设(x)
ans =
                  
                   
                    
                     
                      (
                     
                      
                       
                        
                         
                          
                           
                            
                             x
                           
                           
                           
                            
                             R
                           
                          
                         
                        
                       
                       
                        
                         
                          
                           
                            0
                           
                            <
                           
                            x
                          
                         
                        
                       
                      
                     
                     
                      )
                    
                   
                  
解决(x^4 == 1)
ans =
                  
                   
                    
                     1
                   
                  
假设(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求解多元方程,解决不平等问题并通过假设解决。万博 尤文图斯通过使用可变精确算术,可以在高精度上象征性或数字上找到解决方案。

用符号变量进行替换。代替 x = x o - 1 into x 2 + 1

symsxxosubs(x^2+1,x,xo-1)
ans =
                  
                   
                    
                     
                      
                       
                        
                         
                          
                           
                            
                             
                              xo
                             
                              -
                             
                              1
                            
                           
                          
                         
                        
                       
                       
                        
                         2
                       
                      
                      
                       +
                      
                       1
                     
                    
                   
                  

代替multiple values. For example, evaluate c o s ( a ) + s i n ( b ) - e 2 C 通过替换 a = π 2 , b = π 4 , c = - 1

symsabc潜艇(cos(a) + sin(b) - exp(2*c), [a b c], [pi/2 pi/4 -1])
ans =

2 2 - e - 2

Create and solve equations. Find the zeros of 9 x 2 - 1 = 0

solve(9*x^2 - 1 == 0)
ans =

( - 1 3 1 3 )

求解一般二次方程 a x 2 + b x + c = 0 和use subs to evaluate that solution for a = 9 , b = 0 , c = - 1

eqn = a*x^2 + b*x + c == 0;sol = solve(eqn)
sol =

( - b + b 2 - 4 a c 2 a - b - b 2 - 4 a c 2 a )

潜艇(sol,[a b c],[9 0 -1])
ans =

( - 1 3 1 3 )

当需要确切的结果或高精度时,象征性或具有可变精确算术的求解。图 f ( x ) = 6 x 7 2 x 6 + 3 x 3 8 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 =

root ( z 7 - z 6 3 + z 3 2 - 4 3 , z , 5 )

vpasol =vpasolve(f)% variable-precision
vpasol =

( 1.0240240759053702941448316563337 - 0.88080620051762149639205672298326 + 0.50434058840127584376331806592405 i - 0.88080620051762149639205672298326 - 0.50434058840127584376331806592405 i - 0.22974795226118163963098570610724 + 0.9677461576744031073999010695171 i - 0.22974795226118163963098570610724 - 0.9677461576744031073999010695171 i 0.7652087814927846556172932675903 + 0.83187331431049713218367239317121 i 0.7652087814927846556172932675903 - 0.83187331431049713218367239317121 i )

Simplification and Manipulation

符号数学工具箱支持万博1manbetxFormula Manipulation and Simplification数学功能。大多数数学表达式可以以不同的方式表示,但是数学上等效的形式和符号数学工具箱都支持许多操作,包括分解或扩展表达式,结合术语,重写或重新排列的表达式以及基于假设的简化。万博1manbetx

执行多项式乘法并简化结果,表明 ( x - 1 ) ( x + 1 ) ( x 2 + x + 1 ) ( x 2 + 1 ) ( x 2 - x + 1 ) ( x 4 - x 2 + 1 ) simplifies to x 1 2 - 1

simplify((x - 1)*(x + 1)*(x^2 + x + 1)*(x^2 + 1)*(x^2 - x + 1)*(x^4 - x^2 + 1))
ans =
                  
                   
                    
                     
                      
                       
                        
                         x
                       
                       
                        
                         12
                       
                      
                      
                       -
                      
                       1
                     
                    
                   
                  

Apply trigonometric identities to simplifications, for example s i n 2 ( x ) = 1 - c o s ( 2 x ) 2

组合(2*sin(x)*cos(x) +(1- cos(2*x))/2 + cos(x)^2,'sincos')
ans =
                  
                   
                    
                     
                      
                       
                        
                         sin
                       
                       
                        
                         (
                        
                         
                          
                           
                            
                             2
                            
                            
                            
                             x
                           
                          
                         
                        
                        
                         )
                       
                      
                      
                       +
                      
                       1
                     
                    
                   
                  

Factor or expand multivariate polynomials.

symsxyfactor(y^6-x^6)
ans =
                  
                   
                    
                     
                      (
                     
                      
                       
                        
                         
                          
                           
                            -
                           
                            1
                          
                         
                        
                       
                       
                        
                         
                          
                           
                            x
                           
                            -
                           
                            y
                          
                         
                        
                       
                       
                        
                         
                          
                           
                            x
                           
                            +
                           
                            y
                          
                         
                        
                       
                       
                        
                         
                          
                           
                            
                             
                              x
                            
                            
                             
                              2
                            
                           
                           
                            +
                           
                            
                             
                              x
                             
                             
                             
                              y
                            
                           
                           
                            +
                           
                            
                             
                              y
                            
                            
                             
                              2
                            
                           
                          
                         
                        
                       
                       
                        
                         
                          
                           
                            
                             
                              x
                            
                            
                             
                              2
                            
                           
                           
                            -
                           
                            
                             
                              x
                             
                             
                             
                              y
                            
                           
                           
                            +
                           
                            
                             
                              y
                            
                            
                             
                              2
                            
                           
                          
                         
                        
                       
                      
                     
                     
                      )
                    
                   
                  
f(x)=(x^3 + 7);扩展(f(y-1))
ans =
                  
                   
                    
                     
                      
                       
                        
                         y
                       
                       
                        
                         3
                       
                      
                      
                       -
                      
                       
                        
                         3
                        
                        
                        
                         
                          
                           y
                         
                         
                          
                           2
                         
                        
                       
                      
                      
                       +
                      
                       
                        
                         3
                        
                        
                        
                         y
                       
                      
                      
                       +
                      
                       6
                     
                    
                   
                  

找到功能组成 f ( g ( x ) )

f(x) = sqrt(log(x)); g(x) = sqrt(1-x); h = compose(g,f,x)
h(x) =
                  
                   
                    
                     
                      
                       
                        
                         1
                        
                         -
                        
                         
                          
                           
                            
                             日志
                           
                           
                            
                             (
                            
                             
                              
                               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 d d x ( sin ( x ) )

diff(sin(x))
ans =
                  
                   
                    
                     
                      
                       cos
                     
                     
                      
                       (
                      
                       
                        
                         x
                       
                      
                      
                       )
                     
                    
                   
                  

Find the derivative of d d x ( x 2 + sin ( 2 x 4 ) + 1 ) 使用链条规则。

diff(x^2+sin(2*x^4)+1,x)
ans =
                  
                   
                    
                     
                      
                       
                        
                         2
                        
                        
                        
                         x
                       
                      
                      
                       +
                      
                       
                        
                         8
                        
                        
                        
                         
                          
                           x
                         
                         
                          
                           3
                         
                        
                        
                        
                        
                         
                          
                           cos
                         
                         
                          
                           (
                          
                           
                            
                             
                              
                               2
                              
                              
                              
                               
                                
                                 x
                               
                               
                                
                                 4
                               
                              
                             
                            
                           
                          
                          
                           )
                         
                        
                       
                      
                     
                    
                   
                  

Find the indefinite integral f ( x ) d x for f ( x ) = e - x 2 2

int(exp(-x^2/2),x)
ans =

2 π ERF ( 2 x 2 ) 2

找到确定的积分 a b f ( x ) d x for f ( x ) = x 日志 ( 1 + x ) 0 to 1

int(x*log(1+x),0,1)
ans =

1 4

Show that s i n ( x ) x = 1 at x = 0 通过计算泰勒系列扩展 ( x - a ) n f ( n ) ( a ) n ! for f ( x ) = s i n ( x ) x around the point x = 0

symsxt =泰勒(sin(x)/x)
t =

x 4 120 - x 2 6 + 1

潜艇(T, x, 0)
ans =
                  
                   
                    
                     1
                   
                  

Show that t a n ( x ) 是不连续的 x = π 2 by showing that the left and right limits are not equal. lim x π 2 + t a n ( x ) lim x π 2 - t a n ( x )

limit(tan(x),x,pi/2,'剩下')
ans =
                  
                   
                    
                   
                  
limit(tan(x),x,pi/2,'正确的')
ans =
                  
                   
                    
                     
                      
                       -
                      
                     
                    
                   
                  
limit(tan(x),x,pi/2)
ans =
                  
                   
                    
                     NaN
                   
                  

微分方程

符号数学工具箱可以分析求解Solve a System of Differential EquationsusingDSOLVE

解决一阶ode d y d x = - a y

symsaby(x)dsolve(diff(y)== -a*y)
ans =
                  
                   
                    
                     
                      
                       
                        
                         C
                       
                       
                        
                         1
                       
                      
                      
                      
                      
                       
                        
                         e
                       
                       
                        
                         
                          
                           -
                          
                           
                            
                             a
                            
                            
                            
                             x
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  

Solve the same ODE with the initial condition y ( 0 ) = b

DSOLVE(diff(y)== -a*y,y(0)==b)
ans =
                  
                   
                    
                     
                      
                       b
                      
                      
                      
                       
                        
                         e
                       
                       
                        
                         
                          
                           -
                          
                           
                            
                             a
                            
                            
                            
                             x
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  

求解耦合的一阶ODES系统 d x d t = y d y d t = - x

symsx(t)y(t)z = dsolve(diff(x)== y,diff(y)== -x);disp([Z.X; Z.Y])
                 

( C 1 cos ( t ) + C 2 sin ( t ) C 2 cos ( t ) - C 1 sin ( t ) )

Linear Algebra

The Symbolic Math Toolbox can work with symbolic vectors and matrices. It can computeeigof symbolic matrices.

Perform matrix multiplication A x = b where A = [ a b c d ] x = [ x 1 , x 2 ]

symsabcdsymsx1x2x = [x1;x2];a = [a b;c d];b = a*x
b =

( a x 1 + b x 2 c x 1 + d x 2 )

Find the determinant of A.

det(a)
ans =
                  
                   
                    
                     
                      
                       
                        
                         a
                        
                        
                        
                         d
                       
                      
                      
                       -
                      
                       
                        
                         b
                        
                        
                        
                         c
                       
                      
                     
                    
                   
                  

找到A的特征值。

lambda =eig(A)
lambda =

( a 2 + d 2 - a 2 - 2 a d + d 2 + 4 b c 2 a 2 + d 2 + a 2 - 2 a d + d 2 + 4 b c 2 )

图形

The Symbolic Math Toolbox supports analytical plotting in 2D and 3D.

fplot(tan(x))

图包含一个轴对象。轴对象包含2个类型函数线的对象。

Plot the parametric curve x ( t ) = t * s i n ( 5 t ) y ( t ) = t * c o s ( 5 t )

symstx = t*sin(5*t);y = t*cos(5*t);fplot(x,y)网格

图包含一个轴对象。The axes object contains an object of type parameterizedfunctionline.

绘制3D参数曲线 x ( t ) = e | t | 1 0 s i n ( 5 | t | ) , y ( t ) = e | t | 1 0 c o s ( 5 | t | ) z ( t ) = t [-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”);

图包含一个轴对象。The axes object contains an object of type parameterizedfunctionline.

绘制3D表面 f ( x , y ) = s i n ( x ) + c o s ( y )

symsxyfsurf(sin(x) + cos(y))

图包含一个轴对象。轴对象包含一个functionsUrface的对象。

Plot the 2D contours of the same surface.

fcontour(sin(x) + cos(y))

图包含一个轴对象。轴对象包含类型functionContour的对象。