Saturday, February 7, 2009

Vectors and Matrices - 2

In the last post, I wrote about simple operations with matrices. Now, I want to talk about the functions and we will use them on matrices and vectors.

All right, let's start. The Scilab has many math functions, like sin, cos, tan, asin, acos, atan, exp, log, etc....

All functions can be used on any type of numeric variable (scalar, vector, matrix or tensor).

First example:
-->x = [1 0 1;
-->0 1 0;
-->1 0 1]
x =

1. 0. 1.
0. 1. 0.
1. 0. 1.

-->y = cos(%pi*x) // the Scilab has some common values, I know PI (%pi = 3.1415927) and E (%e = 2.7182818)
y =

- 1. 1. - 1.
1. - 1. 1.
- 1. 1. - 1.


Look that cos(%pi) = -1 and cos(0) = 1 (remember the trigonometric circle).

So, operations with matrices create new matrices, and the new matrices can be used on the new operations or functions.

Let's go to the second example.
-->x = [1 2 3]
x =

1. 2. 3.

-->y = [3;
-->2;
-->1]
y =

3.
2.
1.

-->z1 = exp(x*y)
z1 =

22026.466

-->z2 = exp(y*x)
z2 =

20.085537 403.42879 8103.0839
7.3890561 54.59815 403.42879
2.7182818 7.3890561 20.085537


The operation x*y results in
x*y = [1*3 + 2*2 + 3*1] = [3 + 4 + 3] = 10.
The operation y*x results in
y*x =
[3*1 3*2 3*3;
2*1 2*2 2*3;
1*1 1*2 1*3] =
[3 6 9;
2 4 6;
1 2 3].

The function exp(.) returns the natural exponential (exp(x) = %e^x).

Ok, let's finish here. Who wants more, do comments.

3 comments:

ktres said...

Why in scilab sin cos and tan results are different from what I get on my calculator. Sorry but I just started using it

Alex Carneiro said...

Take a look at the type of the arguments, when you work with trygonometric functions, your arguments may be defined in degrees or radians.

Best regards!

ktres said...

Thanks man. Now it makes sense!