sym, syms, symsum in MatLab

sym, syms, and symsum in MatLab

Sym in MatLab

The short name of 'sym' comes from the word 'symbolic'. Symbolic is the adjective of the 'symbol' word. Symbolic means serving as a symbol or acting as a symbol. Now, what is sym in MatLab programming language? Before that, read the polyfit() in MatLab. This function is used to solve polynomial equations.

Sym is the process of creating symbols in a  MatLab program in order to express variables, expressions, equations, functions, and matrices in an oriented way.

Why do we need this sym in MatLab?

It is assumed that the human mind learns better and easier when the presentation of a subject is clear and readable. Suppose, you are a teacher and you are teaching your student about MatLab. You gave a lecture about mathematical equations and you want to share the source code of this lecture with students. Some of the students do not understand the MatLab expressional way in the compiler. (What is the expressional way of x2 + 2x + 3. That is x.^2+2*x+3. This is a simple example to understand the importance of sym in MatLab) But you want to express it more clearly. Use the 'sym' function of MatLab.

Some short points of the sym in MatLab

1. To create symbolic expression.

2. To make your MatLab code presentation clear.

3. Short your implementation technique in MatLab.

Important sub-functions of sym in MatLab

#Function
Sym('x')
#Description
Return a symbolic variable 'x'.

#Function
Sym('x', [n1, n2, n3, ........, nm])
#Description
Return some row vertors of variable 'x' (x1, x2, x3, ...., xm).

#Function
Sym('x', [1, 2, 3])
#Description
Return only three [x1, x2, x3] row vector of variable 'x'.

#Function
sym('x', n)
#Description
Return n x n matrix for varibale 'x'.

#Function
Sym('x', 2)
#Description
Return
[[x­1,11,2­][x­2,12,2­]] matrix.

#Function
Sym(number)
#Description
Return a symbolic number.

#Function
Sym(number, flag)
#Description
Return a floating point in a symbolic number that is specified by the 'flag'.


Examples of sym in MatLab

How to create a symbolic variable using in sym in MatLab?

sym('x')

Suppose, you have a variable 'x' and you want to create the symbolic of 'x'. Just put the variable 'x' with commas ' ' in the sym function in MatLab.

How to create some symbolic variables of a variable using sym in MatLab?

Defined a variable 'x'. We want to create 10 symbolic variables of 'x'.  Then, defined like below in MatLab.

sym('x', [1,10])

Output

[ x1, x2, x3, x4, x5, x6, x7, x8, x9, x10]

How to create 2 by 3 (2 x 3) matrices of a variable using sym in MatLab?

sym('x',[2 3])
Output
[ x1_1, x1_2, x1_3]
[ x2_1, x2_2, x2_3]

Similar functions of 'sym' in MatLab

syms in MatLab

'syms' is the zip version of function 'sym' in MatLab programming language. Syms is the plural name of the word sym. In other words, syms is the plural function of sym function in MatLab. Now, what is syms?
Syms is a pre-defined function in MatLab used to create symbolic variables of functions and matrices. Generally, syms create scalar symbolic variables in MatLab. 

How can we create symbolic variables using sym in MatLab?

It is as simple as sym function. Type the 'sym' word in MatLab then write the variable or variables as you want.

Examples of syms in MatLab

Define some variables

# create a single variable 'x' using syms in MatLab
syms x

# create a multiple variables ('x', 'y', and 'z') using syms in MatLab
syms x y z

Make an assumption for this statement xy, yz, and zx in MatLab, where x,y are integer and z is rational.

syms x y integer
syms z rational
assume([x*y y*z z*x],'clear')
assumptions
Output
Empty sym: 1-by-0

symsum in MatLab

Symsum is the short form of the sum of symbolic series or the sum of mathematical series. The defined symbolic variables can be fed in the symsum function to find out the summation of the defined variables.

When will we use the 'symsum' function?

Hope that you already read about the sym and syms function. You are declaring the symbolic representation and sometimes you need to use them in mathematical calculation, especially to find out the sum of symbolic variables. At that point, you can use this function in MatLab to find out the sum of a series.

Description of 'symsum' function in MatLab

The symsum function takes 4 or 2 parameters according to the mathematical series requirement.

#4 parameter symsum
f = symsum(function, index value, lower range, upper range)
The 'f' of symsum returns four-parameter and those are
function -> sum of series of the 'f' function.
index value -> sum of 'f' series in the limit of index range.
lower range -> lower limit of the function 'f'.
upper range -> upper limit of the function 'f'.

# 2 parameter symsum
f = symsum(function, index value)
These two parameters' symbolic function is used to calculate the summation of an indefinite series.


The sum of square integer series in the range of 0 to 10 with index value i. (#4 parameters)
sum of square integer

The inverse sum of square integer series in the range of 0 to indefinite with index value i. (#2 parameter)
sum of square inverse integer

Example of symsum in MatLab

# Find the sum of square integer series (range 0 to 100)
syms i
F = symsum(i^2,i,0,100)

Output: 338350


# Find the inverse sum of square integer series (range 1 to-infinitive)
syms i
F = symsum(1/i^2,i,1,Inf)

Output: pi^2/6

Simplify the equation x(x+1) + 2x2

Code
syms x y
%% symbolic equation
z = x*(x+1)+2*x^2;
%% expanding the z eqation
z = expand(z);
disp(z)
%% simplification of the z eqaution
simplify(z);
%% Find the pretty expression of z
pretty(z);

Output

3x2 + x


Find the value of f1, f2, and f3 functions

f(x) = x3

f(x,y) = x2 + y

f(x,y,z) = xy + yz + zx

Code

syms x y z
f1(x)=x^3;
f2(x,y) = x^2+y
f3(x,y,z) = x*y+y*z+z*x
%% sending values to the functions
f1(44)
f2(12,7)
f3(1,2,3)
Output
85184 #f1
151 #f2
11 #f3

syms in MatLab to plot an equation

Read the post to learn how to plot a function in MatLab.

Code

syms x
f=exp(1./(1-x.^2))/(1+x.^2);
G = ezplot(f,[-7,7]);
set(G, 'Color', 'b', 'Linewidth', 1.2);
grid on
hold on
y=[-1,6];
x = [-1,-1];
plot(x,y,'Color','r','Linewidth', 1.7);
x=x.*(-1);
plot(x,y,'Color','r','Linewidth', 1.7);
f=diff(f);
x=solve(f);
for i=1:length(x)
plot(x(i),exp(1/(1-x(i)^2))/(1+x(i)^2),'r*')
end
f=diff(f);
x=solve(f);
for i=1:length(x)
plot(x(i),exp(1/(1-x(i)^2))/(1+x(i)^2),'Red o')
end

Output

syms in MatLab to plot an equation
syms in MatLab to plot an equation

Recommended Video (Solving Symbolic Expressions and Equations)



Post a Comment

Previous Post Next Post