Wednesday, September 19, 2012

Plot3d2: creating a cube

For creating a cube with plot3d2(.) function it's required to create three matrices for mapping each vertex of the cube.

Think the six faces of the cube, now open them on a plan and hold each vertex position of the cube. Empty elements of the matrix should be filled according spacial logic of the cube, with the same value of the neighbor vertexes.

-->x = [zeros(4, 2), ones(4, 2), zeros(4, 1)]
 x  =

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

-->y = ones(4, 5);

-->y(2:3,2:3) = 0
 y  =

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

-->z = [zeros(2, 5); ones(2, 5)];
 z  =

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

-->plot3d2(x, y, z);



Result is presented in the following image:
In this case, all faces of the cube are equal, which makes this structure symmetric.

Plot3D: rectangular surface

I received a request of how to create a rectangular surface.

This post is about plot3d(.) function, which I use to create surfaces in Scilab: http://usingscilab.blogspot.com.br/2009/06/plot-3d-surface.html

Now, look plot3d(.) has three arguments: x, y, and z, being the first two ones vectors and the last one is a matrix.

For a surface that x and y are in set [0, 10] and z is in set [0, 1], take the following code:

-->x = 0:10;

-->y = 0:10;

-->z = zeros(length(x), length(y)); // z matrix is initialized with zeros

-->z(($/4):(3*$/4),($/4):(3*$/4)) = 1 // a square in the matrix is set to 1
 z  =

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

-->plot3d(x, y, z); // shows the 3d graphic with x, y, and z components



The generated image is the following.

I wish this example has been helpful.

Obs.: for creating a cube, or parallelogram, plot3d2(.) function is more recommended: http://usingscilab.blogspot.com.br/2012/09/plot3d2-creating-cube.html

Monday, September 3, 2012

Gamma function

Last week I was teaching about combinatorial analysis and talking about factorial operator I remembered Gamma Function, this function is very useful in signal processing but particularly Gamma Function is equal to the factorial for non-negative integer numbers.

Theory about Gamma Function is very well described in Wikipedia: http://en.wikipedia.org/wiki/Gamma_function

The equation that defines Gamma Function is


Following figure presents the graph of Gamma Function



And when z is a non-negative integer is verified that


This consequence is because of the property






Typical values of Gamma function are

 In Scilab, there are both functions: gamma(.) and factorial(.), following are some examples of these functions


-->factorial(1)
 ans  =

    1. 

-->gamma(1)
 ans  =

    1. 

-->factorial(1.5)
 !--error 10000
factorial: Wrong value for input argument #1: Scalar/vector/matrix/hypermatrix of positive integers expected.
at line      14 of function factorial called by : 
factorial(1.5)


-->gamma(1.5)
 ans  =

    0.8862269 

-->sqrt(%pi)/2
 ans  =

    0.8862269 

Look factorial(.) is not possible to be applied in a non-integer number, the same happens with negative numbers.

And gamma(.) in 1.5 is equal to sqrt(%pi)/2 verifying the correspondence presented in the typical values figure.


Obs.: all equations and figures that I posted in this text were got from the Wikipedia page.