Computational Mathematics in Symbolic Math Toolbox - MATLAB & Simulink Example - MathWorks United Kingdom (2024)

Open Live Script

This example provides an overview of the Symbolic Math Toolbox™ which offers a complete set of tools for computational and analytical mathematics.

Computational Mathematics in Symbolic Math Toolbox- MATLAB & Simulink Example- MathWorks United Kingdom (1)

This example includes

  • Variables, Expressions, Functions and Equations

  • Substitution and Solving

  • Simplification and Manipulation

  • Calculus (Differentiation, Integration, Limits, Series)

  • Differential Equations

  • Linear Algebra

  • Graphics

For more details see Get Started with Symbolic Math Toolbox. For more details on documenting and sharing your mathematics see Create Live Scripts in the Live Editor.

Variables, Expressions, Functions 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 using sym and with variable-precision using 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.

syms x y log(x) + exp(y)
ans =ey+log(x)

You can also create piecewise functions.

y(x) = piecewise(x<0, -1, x>0, 1)
y(x) =

{-1ifx<01if0<x

Create and evaluate Create Symbolic Functions. Find the value of f at x=-5.

syms f(x)f(x) = x^4-2*x^3+6*x^2-2*x+10
f(x) =x4-2x3+6x2-2x+10
f(-5)
ans =1045

Find the intersection between lines y1 and y2 using solve. Equate the lines using the == operator.

syms y1 y2y1 = x+3; y2 = 3*x;solve(y1 == y2)
ans =

32

Make assume on symbolic variables. There are 4 solutions to x4=1, two real and two complex. Assuming that x is real and x>0, there is only one solution.

syms xsolve(x^4 == 1)
ans =

(-11-ii)

ans =(xR0<x)
solve(x^4 == 1)
ans =1
assume(x,'clear')

Substitution and Solving

The Symbolic Math Toolbox supports evaluation of mathematical functions by substituting for any part of an expression using subs. You can substitute numeric values, other symbolic variables or expressions, vectors, or matrices. The Symbolic Math Toolbox supports the solving of equations and systems of equations using solve. It supports solving multivariate equations, solving inequalities and solving with assumptions. Solutions can be found symbolically or numerically with high precision by using variable-precision arithmetic.

Make substitutions with your symbolic variables. Substitute x=xo-1 into x2+1

syms x xosubs(x^2+1,x,xo-1)
ans =xo-12+1

Substitute multiple values. For example, evaluate cos(a)+sin(b)-e2C by substituting a=π2,b=π4,c=-1.

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

22-e-2

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

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

(-1313)

Solve the general quadratic equation ax2+bx+c=0 and 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+b2-4ac2a-b-b2-4ac2a)

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

(-1313)

Solve equations symbolically or with variable-precision arithmetic when exact results or high precision is needed. The graph of f(x)=6x72x6+3x38 is very flat near its root.

syms x f(x)assume(x>0)f(x) = 6*x^7-2*x^6+3*x^3-8;fplot(f)xlim([-10 10])ylim([-1e3 1e3])

Computational Mathematics in Symbolic Math Toolbox- MATLAB & Simulink Example- MathWorks United Kingdom (2)

doubleSol = roots([6 -2 0 0 3 0 0 -8]) % double-precision
doubleSol = 7×1 complex 1.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) % exact. The roots object stores the zeros for symbolic computations
symsSol =

root(z7-z63+z32-43,z,5)

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

(1.0240240759053702941448316563337-0.88080620051762149639205672298326+0.50434058840127584376331806592405i-0.88080620051762149639205672298326-0.50434058840127584376331806592405i-0.22974795226118163963098570610724+0.96774615576744031073999010695171i-0.22974795226118163963098570610724-0.96774615576744031073999010695171i0.7652087814927846556172932675903+0.83187331431049713218367239317121i0.7652087814927846556172932675903-0.83187331431049713218367239317121i)

Simplification and Manipulation

The Symbolic Math Toolbox supports the Formula Manipulation and Simplification of mathematical functions. Most mathematical expressions can be represented in different, but mathematically equivalent forms and the Symbolic Math Toolbox supports a number of operations, including factoring or expanding expressions, combining terms, rewriting or rearranging expressions, and simplification based on assumptions.

Perform polynomial multiplication and simplify the results, show that (x-1)(x+1)(x2+x+1)(x2+1)(x2-x+1)(x4-x2+1) simplifies to x12-1.

simplify((x - 1)*(x + 1)*(x^2 + x + 1)*(x^2 + 1)*(x^2 - x + 1)*(x^4 - x^2 + 1))

Apply trigonometric identities to simplifications, for example sin2(x)=1-cos(2x)2.

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

Factor or expand multivariate polynomials.

syms x yfactor(y^6-x^6)
ans =(-1x-yx+yx2+xy+y2x2-xy+y2)
f(x) = (x^3 + 7);expand(f(y-1))
ans =y3-3y2+3y+6

Find the functional composition f(g(x)).

f(x) = sqrt(log(x));g(x) = sqrt(1-x);h = compose(g,f,x)
h(x) =1-log(x)

Calculus (Differentiation, Integration, Limits, Series, etc)

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 ddx(sin(x)).

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

Find the derivative of ddx(x2+sin(2x4)+1) using the chain rule.

diff(x^2+sin(2*x^4)+1,x)
ans =2x+8x3cos(2x4)

Find the indefinite integral f(x)dx for f(x)=e-x22.

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

2πerf(2x2)2

Find the definite integral abf(x)dx for f(x)=xlog(1+x) from 0 to 1.

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

14

Show that sin(x)x=1 at x=0 by computing the Taylor series expansion (x-a)nf(n)(a)n! for f(x)=sin(x)x around the point x=0.

syms x T = taylor(sin(x)/x)
T =

x4120-x26+1

subs(T,x,0)
ans =1

Show that tan(x) is discontinuous at x=π2 by showing that the left and right limits are not equal. limxπ2+tan(x)limxπ2-tan(x).

limit(tan(x),x,pi/2,'left')
ans =
limit(tan(x),x,pi/2,'right')
ans =-
limit(tan(x),x,pi/2)
ans =NaN

Differential Equations

The Symbolic Math Toolbox can analytically solve systems of Solve a System of Differential Equations using dsolve.

Solve the first order ODE dydx=-ay.

syms a b y(x)dsolve(diff(y) == -a*y)
ans =C1e-ax

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

dsolve(diff(y)== -a*y,y(0)==b)
ans =be-ax

Solve the system of coupled first order ODEs dxdt=y and dydt=-x.

syms x(t) y(t)z = dsolve(diff(x) == y, diff(y) == -x);disp([z.x;z.y])

(C1cos(t)+C2sin(t)C2cos(t)-C1sin(t))

Linear Algebra

The Symbolic Math Toolbox can work with symbolic vectors and matrices. It can compute eig of symbolic matrices.

Perform matrix multiplicationAx=b where A=[abcd]and x=[x1,x2]

syms a b c dsyms x1 x2x = [x1; x2];A = [a b ; c d];b = A*x
b =

(ax1+bx2cx1+dx2)

Find the determinant of A.

det(A)
ans =ad-bc

Find the eigenvalues of A.

lambda = eig(A)
lambda =

(a2+d2-a2-2ad+d2+4bc2a2+d2+a2-2ad+d2+4bc2)

Graphics

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

fplot(tan(x))

Computational Mathematics in Symbolic Math Toolbox- MATLAB & Simulink Example- MathWorks United Kingdom (3)

Plot the parametric curve x(t)=t*sin(5t) and y(t)=t*cos(5t).

syms tx = t*sin(5*t); y = t*cos(5*t);fplot(x, y)grid on

Computational Mathematics in Symbolic Math Toolbox- MATLAB & Simulink Example- MathWorks United Kingdom (4)

Plot the 3D parametric curve x(t)=e|t|10sin(5|t|), y(t)=e|t|10cos(5|t|) and z(t)=t from [-10,10] with a dashed red line.

syms txt = 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');

Computational Mathematics in Symbolic Math Toolbox- MATLAB & Simulink Example- MathWorks United Kingdom (5)

Plot the 3D surface f(x,y)=sin(x)+cos(y).

syms x yfsurf(sin(x) + cos(y))

Computational Mathematics in Symbolic Math Toolbox- MATLAB & Simulink Example- MathWorks United Kingdom (6)

Plot the 2D contours of the same surface.

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

Computational Mathematics in Symbolic Math Toolbox- MATLAB & Simulink Example- MathWorks United Kingdom (7)

Computational Mathematics in Symbolic Math Toolbox
- MATLAB & Simulink Example
- MathWorks United Kingdom (2024)
Top Articles
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 5564

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.