Symbolic Math Toolbox provides functions for solving, plotting, and manipulating symbolic math equations. You can create, run, and share symbolic math code. In the MATLAB Live Editor, you can get next-step suggestions for symbolic workflows. The toolbox provides functions in common mathematical areas such as calculus, linear algebra, algebraic and differential equations, equation simplification, and equation manipulation.
Symbolic Math Toolbox lets you analytically perform differentiation, integration, simplification, transforms, and equation solving. You can perform dimensional computations and convert between units. Your computations can be performed either analytically or using variable-precision arithmetic, with the results displayed in mathematical typeset.
You can share your symbolic work with other MATLAB users as live scripts or convert them to HTML, Word, LaTeX, or PDF documents. You can generate MATLAB functions, Simulink function blocks, and Simscape equations directly from symbolic expressions.
Interactively update and display symbolic math computations and get next-step suggestions for symbolic workflows using the MATLAB Live Editor. Share your work as live scripts or publish your code to create formatted documents including HTML, Word, LaTeX, and PDF.
The sym function refers to a symbolic variable, which you can then assign to a MATLAB variable with a different name. For example, the command f1 = sym('x') refers to the symbolic variable x and assigns it to the MATLAB variable f1.
Use the syms function to create a symbolic variable x and automatically assign it to a MATLAB variable x. When you assign a number to the MATLAB variable x, the number is represented in double-precision and this assignment overwrites the previous assignment to a symbolic variable. The class of x becomes double.
Use syms to create a symbolic variable that is assigned to a MATLAB variable with the same name. You get a fresh symbolic variable with no assumptions. If you declare a variable using syms, existing assumptions are cleared.
Use sym to refer to an existing symbolic variable. If this symbolic variable was used in your MATLAB session before, then sym refers to it and its current assumption. If it was not used before, then sym creates it with no assumptions.
The command syms a [1 3] creates a 1-by-3 symbolic array a and the symbolic variables a1, a2, and a3 in the workspace. The symbolic variables a1, a2, and a3 are automatically assigned to the symbolic array a.
The command a = sym('a',[1 3]) refers to the symbolic variables a1, a2, and a3, which are assigned to the symbolic array a in the workspace. The elements a1, a2, and a3 are not created in the workspace.
To declare a symbolic variable within a function, use sym. For example, you can explicitly define a MATLAB variable x in the parent function workspace and refer x to a symbolic variable with the same name.
Once more I am pleased to introduce guest blogger Kai Gehrs. Kai has been a Software Engineer at MathWorks for the past five years working on the Symbolic Math Toolbox. He has a background in mathematics and computer science. He already contributed to my BLOG in the past writing about Using Symbolic Equations And Symbolic Functions In MATLAB as well as on approaches for Simplifying Symbolic Results.
Computing area moments of inertia is an important task in mechanics. For example, area moments of inertia play a critical role in stress, dynamic, and stability analysis of structures. In this article, we use capabilities of the Symbolic Math Toolbox to compute area moments for cross sections of elliptical tubes.
Now, we only need to compute these two double integrals. The mathematical theory behind multi-dimensional integration tells us that we can rewrite each of these double integrals in terms of two integrals:
We use the same strategy to compute $I_2$. First, we compute the inner integral $\int_0^2 \, \sqrt1 - \fracx^29 y^2\, \mathrm dy$. Then, we integrate the resulting expression with respect to $x$ from $2$ to $3$:
We can use a numerical integrator, such as MATLAB's integral2, to compute the area moment of inertia in the previous example. A numerical integrator might return slightly less accurate results, but other than that there is not much benefit from using symbolic integration there.
What is SymPy? SymPy is a Python library for symbolic mathematics. Itaims to be an alternative to systems such as Mathematica or Maple while keepingthe code as simple as possible and easilyextensible. SymPy is written entirely in Python and does not require anyexternal libraries.
SymPy uses mpmath in the background, which makes it possible toperform computations using arbitrary-precision arithmetic. Thatway, some special constants, like , , (Infinity),are treated assymbols and can be evaluated with arbitrary precision:
Simplification is a somewhat vague term, and more precisesalternatives to simplify exists: powsimp (simplification ofexponents), trigsimp (for trigonometric expressions) , logcombine,radsimp, together.
SymPy has support for indefinite and definite integration of transcendentalelementary and special functions via integrate() facility, which usesthe powerful extended Risch-Norman algorithm and some heuristics and patternmatching. You can integrate elementary functions:
Sympy is able to solve a large part ofpolynomial equations, and is also capable of solving multipleequations with respect to multiple variables giving a tuple as secondargument. To do this you use the solve() command:
Keyword arguments can be given to this function in order to help iffind the best possible resolution system. For example, if you knowthat it is a separable equations, you can use keyword hint='separable'to force dsolve to resolve it as a separable equation:
The MATLAB Symbolic Math Toolbox is an essential yet powerful extension that brings extensive symbolic computation capabilities to MATLAB. Learn how to install this toolbox and start leveraging its features, including solving complex algebraic equations, evaluating derivatives, computing integrals, and more. This article will also provide code samples for better understanding of the concepts and application in practice.
MATLAB is a versatile programming language and environment, primarily used for numerical computations and visualization. The Symbolic Math Toolbox extends its functionality by providing an interface for symbolic mathematics in MATLAB. This toolbox enables you to work with equations, expressions, and functions involving symbolic variables, allowing for calculations that are otherwise difficult or impossible to perform using numerical methods alone.
Installing through MATLAB DesktopIf you already have MATLAB Desktop, you can install additional toolboxes such as the Symbolic Math Toolbox directly within the application. Here are the steps to follow:
Now that the Symbolic Math Toolbox is installed, you can begin exploring its various functions and applications using symbolic mathematics within your MATLAB sessions. Here are some of the essential concepts to grasp as you dive deeper into this topic:
Other Useful FunctionsThe Symbolic Math Toolbox includes additional functions that can be used for manipulating expressions and transforming them into alternative representations. These include simplifying expressions (simplify), collecting like terms (collect), factoring polynomials (factor, depolynomialize), isolating variables (isolate), and more.
Before using the program you must first download it from GitHub and save it on your computer where MATLAB can access it. Note: you must have the symbolic toolbox to run this code. The example netlists described on this page are also on GitHub.
The code is written as a MATLAB script instead of a function because I thought it would make learning easier because it can be stepped through, and all of the variables created by the code appear in the workspace so users can examine and manipulate them. If you don't want all of the variables in your workspace, it is straightforward to add a line at the top to turn it into a function. If you don't want all the intermediate results printed, simply comment out the lines you don't want.
The SCAM program cannot simply read a schematic diagram so we need to develop a method for representing a circuit textually. This can be done using a device called a netlist that defines the interconnection between circuit elements. If you have used SPICE (Simulation Program with Integrated Circuit Emphasis) this is a familiar concept. A good review is given by Jan Van Der Spiegel at the University of Pennsylvania. The process is easily demonstrated by example.
We start by defining the nodes. The only restriction here is that the nodes must be labeled such that ground is node 0, and the other nodes are named consecutively starting at 1. The choice of which number to assign to which node is entirely arbitrary.
SCAM requires a text file with one line for each component in the circuit. This circuit has 4 components (3 resistors and 1voltage source), and will require 6 lines to define it. Each type of component has its own format for its corresponding lines in a file. These are shown below. The labels N1, N2, etc... correspond to the nodes in the circuit.
and save it in the directory seen by MATLAB. I edited such a file (using the MATLAB editor) and saved it in my SCAM directory as example1.cir. To run the program, assign the filename of the circuit to be analyzed to the variable fname, and then call the program. The output from the MATLAB window is shown below:
The netlist is displayed, followed by A, x and z matrices as well as the matrix equations written out. Finally the values of the unknow variables are displayed. Let's look at the value of v_2 (the voltage at node 2):
In addition to the unknowns, several other variables are created in the workspace (this is why the SCAM program is a script instead of a function). The important variables created, in addition to the unknowns, are a value corresponding to each of the elements. We can examine the value of any element, for example V1 or R2
3a8082e126