Updated June 30, 2023
Introduction to Matlab Syms
In MATLAB, syms is used as a shortcut to the inbuilt function sym. This function can be used to create symbolic variables. The Symbolic variables used in MATLAB are not constants like the regular variables; we do not assign values to them. One important function in this toolbox is the syms function, which creates a symbolic object and automatically assigns it to a MATLAB variable with the same name. This allows for convenient manipulation and evaluation of symbolic expressions in MATLAB.
Syntax of Matlab Syms
Syntax for Syms Function in Matlab:
syms variable1 variable2 …... variableN
syms variable1 variable2 …... variabeN [n1 …... nM]
syms f(variable1, variable2, …..., variableN)
Description:
- syms variable1 variable2 …… variableN is used to create symbolic variables variable1 … variableN. Separate different variables by spaces. ‘syms’ function will clear all the assumptions from variables.
- syms variable1 variable2….. variableN [n1 … nM] is used to create symbolic arrays variable1 …. variable. Each array will have the size n1- X -…- X -nM & will contain automatically generated symbolic variables.
- syms f(variable1, vaiable2, ….., variableN)is used to create the symbolic function & symbolic variables, which will represent input arguments of function ‘f.’ Please note that a single call can be used to create more than one symbolic function.
Examples of Matlab Syms
Let us now understand the code to use syms in MATLAB.
Example #1
In the first example, we will use syms function to create a variable. Please keep in mind that we are using the ‘syms’ function here to create the variable dynamically.
Code:
syms A
A
The command syms A will create a symbolic variable ‘A’ & will automatically assign it to a MATLAB variable with the same name.
This is how our input and output will look in the MATLAB command window:
Input:
syms A
A
Output:
As we can see in the output, the command syms A has created a symbolic variable ‘A’ & assigned it to a variable with the same name (A).
Example #2
In this example, we will use syms function to create multiple variables.
Code:
syms A B C
A
B
C
The command syms A B C will create 3 symbolic variables A, B & C & will automatically assign these to MATLAB variables with the same name.
This is how our input and output will look like in MATLAB command window:
Input:
syms A B C
A
B
C
Output:
As we can see in the output, the command ‘syms A B C’ has created 3 symbolic variables and assigned them to variables with the same name (A, B, C).
Example #3
In this example, we will use syms function to create a symbolic vector. This output vector will have its elements generated automatically in the workspace.
Code:
syms x [1 5]
The command syms x [1 5] will create a symbolic vector ‘x’ of the size 1 X 5
x
This is how our input and output will look like in MATLAB command window:
Input:
syms x [1 5]
x
Output:
As we can see in the output, the command syms x [1 5] has created a symbolic vector of the size 1 X 5.
Example #4
In this example, we will use syms function to create a symbolic matrix with multiple rows. This output matrix will have its elements generated automatically in the workspace.
Code:
syms x [2 4]
The command syms x [2 4] will create a symbolic matrix ‘x’ of the size 2 X 4
x
This is how our input and output will look like in MATLAB command window:
Input:
syms x [2 4]
x
Output:
As we can see in the output, the command syms x [2 5] has created a symbolic matrix of the size 2 X 4.
Example #5
In this example, we will use syms function to create a symbolic function with 3 variables x, y, z. Below are the steps we will follow:
- Create a symbolic function of required variables/arguments.
- Specify the formula for the function created.
- Pass the arguments to compute the value of the function.
Code:
syms f(x,y,z)
f(x,y,z) = 2*x + 5*y - z^2
f(1,2,3)
This is how our input and output will look like in MATLAB command window:
Input:
syms f(x,y,z)
f(x,y,z) = 2*x + 5*y - z^2
f(1,2,3)
Output:
As we can see in the output, the command syms f (x, y, z) has created a symbolic function ‘f’.
Conclusion
- Syms function is used in creating symbolic variables dynamically.
- These are used to solve various expressions with the help of functions available in MATLAB.
- Syms function can also be used in creating symbolic functions dynamically.
Recommended Articles
This is a guide to Matlab Syms. Here we also discuss the introduction and syntax of Matlab syms, different examples, and code implementation. You may also have a look at the following articles to learn more –