Professional Documents
Culture Documents
The command cmb_lin, with four arguments, say, a, x, b, y, can be used to create a linear
combination of the arguments, a*x + b*y, e.g.,
-->cmb_lin('c(1)','x+1','c(2)','y+2')
ans =
c(1)*(x+1)-c(2)*(y+2)
-->cmb_lin('A','x^2+x+2','B','y^2-y-2')
ans =
A*(x^2+x+2)-B*(y^2-y-2)
Functions eval and evstr can be used to evaluate symbolic expressions created with any of the
functions shown above, after the variable names used have been assigned values, for example:
-->y = cmb_lin(mulf('3','x'),'x^2+1',addf('5','x'),'x^3')
y =
3*x*(x^2+1)-(x+5)*x^3
-->x = 5
x =
5.
-->y
y =
3*x*(x^2+1)-(x+5)*x^3
-->eval(y)
ans =
- 860.
-->x = poly(0,'x')
x =
x
-->y
y =
3*x*(x^2+1)-(x+5)*x^3
-->eval(y)
ans =
3
4
3x - 2x - x
Functions addf, subf, mulf, rdivf, ldivf, cmb_lin, eval, and evstr can be combined to form
expressions and evaluate them in scripts and functions. However, SCILAB is not a full symbolic
environment and one should not expect to have the same capabilities of, say, Maple, MuPad, or
Mathematica for processing symbolic expressions.
Since the symbolic expressions produced by functions addf, subf, mulf, rdivf, ldivf, and
cmb_lin are strings, you can use other string manipulation functions such as length, part,
strindex, strsubst, string, strcat, etc., to manipulate those results. Examples of string
manipulation are available in Chapter 2 of the textbook: Urroz, G., 2001, Numerical and
Statistical Methods with SCILAB for Science and Engineering - volume 1,
www.greatunpublished.com, Charleston, S.C.
SCILAB also provides functions trianfml and solve for manipulating matrices with symbolic
elements. Function trianfml produces an upper triangular matrix out of a matrix with
symbolic elements, for example:
-->A = ["a11","a12";"a21","a22"]
A =
!a11
!
!a21
a12
!
!
!
a22
-->trianfml(A)
ans =
!a21
!
!0
a22
a21*a12-a11*a22
!
!
!
Function solve is used to obtain the solution of a matricial equation Ax = b in which the
components of A and b are symbolic elements, and A is an upper triangular matrix. For
example,
-->A = ["a1","a2";"0","a3"]
A =
!a1
!
!0
a2
a3
!
!
!
-->b = ["b1";"b2"]
b =
!b1
!
!b2
!
!
!
-->solve(A,b)
ans =
!a1\(b1-a2*(a3\b2))
!
!a3\b2
!
!
!
To solve a symbolic matrix equation Ax = b in which matrix A is not an upper triangular matrix,
follow these steps:
1 - Create an augmented matrix Aaug = [A|b]
2 - Use function trainfml on the augmented matrix to produce an upper triangular matrix, say
Aaug_up.
3 - Extract an upper triangular matrix of the same dimensions as A from Aaug_up, call it A1
4 - Make the last column in Aaug_up equal to a vector b1.
5 - The solution to the matrix equation Ax = b is obtained by using function solve with
arguments A1 and b1.
Here is an example:
-->A = ["a11","a12";"a21","a22"], b = ["b1";"b2"]
A =
!a11
!
!a21
b
!b1
!
!b2
a12
a22
!
!
!
=
!
!
!
-->A_aug = [A b]
A_aug =
!a11
!
!a21
a12
b1
a22
b2
!
!
!
-->A_aug_up = trianfml(A_aug)
ans =
!a21
!
!0
a22
b2
a21*a12-a11*a22
a21*b1-a11*b2
!
!
!
a22
a21*a12-a11*a22
!
!
!
b1
!b2
!
!a21*b1-a11*b2
!
!
!
-->x = solve(A1,b1)
x =
!a21\(b2-a22*((a21*a12-a11*a22)\(a21*b1-a11*b2)))
!
!(a21*a12-a11*a22)\(a21*b1-a11*b2)
!
!
!
With this function, the solution to the symbolic matrix equation Ax = b is straightforward:
-->A = ["a11","a12";"a21","a22"], b = ["b1";"b2"]
A =
!a11
!
!a21
b
!b1
!
!b2
a12
a22
!
!
!
=
!
!
!
-->getf('symbol_solve.sci')
-->x = symbol_solve(A,b)
x =
!a21\(b2-a22*((a21*a12-a11*a22)\(a21*b1-a11*b2)))
!
!(a21*a12-a11*a22)\(a21*b1-a11*b2)
!
!
!
!a11 a12
!
!a21 a22
B =
!1
!
!0
0
1
!
!
!
!
!
!
-->Ainv = symbol_solve(A,B)
Ainv =
column 1
!-a21\a22*((a21*a12-a11*a22)\a21)
!
!(a21*a12-a11*a22)\a21
!
!
!
column 2
!a21\(-a22*(-(a21*a12-a11*a22)\a11)+1)
!
!-(a21*a12-a11*a22)\a11
!
!
!
2.
5.
3. !
8. !
-->An_inv = eval(Ainv)
An_inv =
!
8.
! - 5.
1.
0.
- 3. !
2. !
-->clean(An*An_inv)
ans =
!
!
0. !
1. !
column 1
!-a21\a22*((a21*a12-a11*a22)\a21)
!
!(a21*a12-a11*a22)\a21
!
!
!
column 2
!a21\(-a22*(-(a21*a12-a11*a22)\a11)+1)
!
!-(a21*a12-a11*a22)\a11
!
!
!
The determinant of a symbolic square matrix of dimensions nxn can be obtained by using
element (n,n) of the upper triangular matrix, multiplied by (-1)n, for example:
-->A = ['a11','a12';'a21','a22']
A =
!a11
!
!a21
a12
a22
!
!
!
-->AT = trianfml(A)
AT =
!a21
!
!0
a22
a21*a12-a11*a22
!
!
!
-->[m n] = size(A)
n =
2.
=
2.
-->detA = string((-1)^n)
detA =
1
-->detA = mulf(detA,AT(n,n))
detA =
a21*a12-a11*a22
Here is the calculation of the determinant of the 2x2 matrix A defined above:
-->getf('c:\symbol_det.sci')
-->symbol_det(A)
ans =
a21*a12-a11*a22
The following example shows the calculation of the determinant of a 3x3 matrix, verifying the
results by using some numerical values:
-->A=['a11','a12','a13';
-->
'a21','a22','a23';
-->
'a31','a32','a33']
A =
!a11
!
!a21
!
!a31
a12
a13
a22
a23
a32
a33
!
!
!
!
!
-->detA = symbol_det(A)
detA =
-((a31*a12-a11*a32)*(a31*a23-a21*a33)-(a31*a22-a21*a32)*(a31*a13-a11*a33))
-->a11=1;a12=2;a13=5;a21=5;a22=6;a23=7;a31=-1;a32=2;a33=5;
-->det(eval(A))
ans =32.
-->eval(detA)
ans =32.
The examples shown above illustrate the use of some SCILAB functions and some user-defined
functions for the manipulation of symbolic expressions in SCILAB. While SCILAB is not a
symbolic environment, these functions allow some limited symbolic manipulation of
expressions. There is, however, a type of SCILAB data that can be manipulated symbolically
more extensively that with the functions shown above. These are polynomial expressions.
Details on the use of polynomials are presented in Chapters 6 and 7 in of the textbook: Urroz,
G., 2001, Numerical and Statistical Methods with SCILAB for Science and Engineering - volume
1, www.greatunpublished.com, Charleston, S.C.