Categories
3. Matlab Functions

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. These are used to solve various expressions with the help of functions available in Symbolic Math Toolbox. The syms function creates a symbolic object that is automatically assigned to a MATLAB variable with the same name.

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 of the 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 so that the variable is created dynamically.

Code:

syms A[Creating symbolic variable A using syms]

A[Displaying the variable created]

The command syms A will create symbolic variable ‘A’ & will automatically assign it to MATLAB variable with same name.

This is how our input and output will look like in the MATLAB command window:

Input:

syms A
A

Output:

Matlab Syms-1.1

As we can see in the output, the command syms A has created a symbolic variable ‘A’ & assigned it to a variable with same name (A).

Example 2:

In this example, we will use syms function to create multiple variables. Here also, all the variables will be created dynamically.

Code:

syms A B C[Creating symbolic variables, A, B, C using syms]

A
B
C
[Displaying the variables created]

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:

Matlab Syms-2.1
Matlab Syms-2.2
Matlab Syms-2.3

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][Creating symbolic vector ‘x’ using syms]

The command syms x [1 5] will create a symbolic vector ‘x’ of the size 1 X 5

x[Displaying the vector created]

This is how our input and output will look like in MATLAB command window:

Input:

syms x [1 5] x

Output:

Output-3.1

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][Creating symbolic matrix ‘x’ using syms]

The command syms x [2 4] will create a symbolic matrix ‘x’ of the size 2 X 4

x[Displaying the matrix created]

This is how our input and output will look like in MATLAB command window:

Input:

syms x [2 4] x
Matlab Syms-4.1

Output:

Output-4.2

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:

  1. Create a symbolic function of required variables/arguments.
  2. Specify the formula for the function created.
  3. Pass the arguments to compute the value of the function.

Code:

syms f(x,y,z)[Creating symbolic function ‘f’ using syms]

f(x,y,z) = 2*x + 5*y - z^2[Specify the formula for the function created]

f(1,2,3)[Pass the arguments to compute the value of the function]

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:

Output-5.1
Matlab Syms-5.2

As we can see in the output, the command syms f (x, y, z) has created a symbolic function ‘f’.

Leave a Reply

Your email address will not be published. Required fields are marked *