EXAMPLES

summary

Graphics

1.     2 D graphics

1.2 The functions plot2d and fplot2d

Graphics of the function y = f(x).

There is two way in order to draw this functions. The primitive plot2d and the primitive fplot2d.If you define the function f with a function of SCILAB, you have to use the primitive fplot2d.

Examples : Graphic of cos(x) on [0 2pi]

1.2.1 fplot2d

Syntax: fplot2d(x,f) where x is a vector and f a function.

Instructions:

- - > x = 0 : 0.1 : 2* %pi    // generates the vector x on the interval [0 2pi] with a step of 0.1
- - > function y=f(x), y=cos(x), endfunction
- - > fplot2d ( x , f )

1.2.2. plot2d

syntax fplot2d(x,y) where x and y are two vectors.

Instructions:

- - > x = 0 : 0.1 : 2* %pi    // generates the vector x on the intervalle [0 2pi] with a step of 0.1
- - > y=cos(x), endfunction
- - > plot2d ( x , f )

If you do not clear the graphic window, the graphics add automatically.

2. 3D graphics

As the 2D graphic, we have the equivalent primitive for 3D graphic, that is to say fplot3d and plot3d.

2.1 Graphic of the surface z = f (x, y)

Use of fplot3d. Use this primitive if you define f as a function.

Examples:

- - > function z=f(x,y),z=(2*x^2+y^2)/(x^2+2*y^2),endfunction
- - > x= -7:0.55:7;
- - >xbasc()    // clear the previous graphic
- - >fplot3d (x, x, f)

You obtain then the following graph:

But you can also use the function plot3d, but the values of the function f must be stocked in a matrices.

For examples :

- - > z = feval (x,x,f) // create a matrix with the value of f(x,y)
- - > plot2d (x,x,z)

2.2 Color in function of the elevation

In order to color the surface in function of the elevation, use the function xset.

Examples

- - > xset ("colormap",hotcolormap(128)) // use of the color table
- - > xbasc()
- - > plot3d1(x,x,z)

You obtain :

2.3 2D representation of 3D graphics

You can represented the values of a matrix with the primitive grayplot.

Example

- - > xbasc()
- - > grayplot(x,x,z)

Moreover, you can add the iso-contour with the primitive contour2d

- - > contour2d(x,x,z,10)

2.4 obtain the value of a point by click with the mouse on the graphic (for a 2D graphic)

- - > [btn,x,y] = xclick ( )

This primitive wait for a click and returns the values of x and y co-ordinates in SCILAB environment.

2.5 Save, load and export graphics.

The best way to achieve these operations is to use the control button of the graphic window. If you use the command save in the button file , the graph will be saved as a binary file and you can load it by file, load.
If you want to export your result, for example in order to use it in a HTML document, use the command file, export. In this menu, you can choose the file destination and the format of the file, for example .gif.

back to example summary

Data processing

The aim of this example is to use scilab as a pre-processor. The problem is the following : students are often confronted with the task of visualise curve whose the data come from a fortran routines and which are stocked in an ASCII file.
This example deals with the treatment of a file which contains data. The goal is to visualise a 3 dimensional graph.

Composition of the file res.dat

The data are disposed as the following:

The first and second lines contain the size of the matrix
The third and fourth lines contain the values of x axis and y axis.
The other lines represent the matrix A.

In scilab, there is several instructions which permit the treatment of data. Some instructions are reserved for binary file and some instructions are reserved for ASCII file. You can find the syntax of all these instructions in the previous part.

We present thus the function which allows to treat this example. Moreover, this example enable to use the conditional instructions and screen data. At first, we define the syntax of a function :

function [ a1, a2 .. ] = name_of_function (b1, b2)

a1, a2 ... are the results. b1, b2, ... are the entry.

1. The different functions.

We define several functions which allows to treat the file. So we present the text for this functions and then, we present the results.

The file lecture.sci

//------------------------------------------------------

function [fd]=open_file(fil)

// opening of the file
fd=mopen(fil,"r");

endfunction

//------------------------------------------------------

function [n,m,x,y,a] =lect_data(fd)

// function which read the data of an ASCII file
// the file is disposed as the following
// first and second line : dimension of the vector x and y
// third and fourth line:coordinate of x and y
// values of the temperature at the point (x,y) set in the matrix m*n

// read the size of the matrix
ligne=mgetl(fd,1); // mgetl read a line as text
execstr("n=["+ligne+"]"); // execstr create the vector from //the previous line

ligne=mgetl(fd,1);
execstr("m=["+ligne+"]");

// read the values of x and y

ligne=mgetl(fd,1);
execstr("x=["+ligne+"]");

ligne=mgetl(fd,1);
execstr("y=["+ligne+"]");

a=matrix(mfscanf(m*n,fd,"%f"),n,m);

endfunction

//----------------------------------------------------------

function draw=drawgraph(x,y,a)

test=input(" 3d graph (1) or 2d contour (2)");

if test = = 1 then
xset("colormap",hotcolormap(32));
xbasc();
plot3d1(x,y,a);
else
xset("colormap",hotcolormap(32));
xbasc();
grayplot(x,y,a);
end

endfunction

//----------------------------------------------------------

Compile these functions

In SCILAB window, tape    - - > getf ("name_of_the_file.sci")

Run these functions.

You have to respect the syntax of your functions. For example, in this case, you tape the following instruction for run the functions:

- - > [fd]=open_file("res.dat")
- - > [n,m,x,y,a] =lect_data(fd)
- - > draw=drawgraph(x,y,a)

Then you obtain the following graphics

back to example summary

Matlab to Scilab conversion

The Matlab to Scilab translator tool.

The matlab file are containing in m-files, that is to say program are written in file : name_of_file.m
Scilab dispose of a translator which is available to translate the file .m in a file .sci

The command is :  - - > mfile2sci name_of_file.m

Nevertheless, this translator is limited because it does not recognise lots of Matlab instructions. For example, if we consider the treatment of the previous file which contains the matrix, the m-file is the following :

% Ouverture du fichier 'MatRes'
fich = fopen('MatRes','r');
% Lecture des dimensions de X et Y, soit nx et ny
nx = fscanf(fich,'%d',1);
ny = fscanf(fich,'%d',1);
% Lecture de X et Y et placement dans les vecteurs x et y
% de dimensions nx et ny
x = fscanf(fich,'%f',nx);
y = fscanf(fich,'%f',ny);
% Lecture de la matrice a des temperatures, de dimension (nx,ny)
a= fscanf(fich,'%f',[ nx  ny])
After the translation, the scilab file which is obtain is:

function []=essai()
// Ouverture du fichier 'MatRes'
[fich,%v] = mopen('MatRes','r',0)
if %v<0 then fich = -1;end
// Lecture des dimensions de X et Y, soit nx et ny
nx = mtlb_fscanf(fich,'%d',1);
ny = mtlb_fscanf(fich,'%d',1);
// Lecture de X et Y et placement dans les vecteurs x et y
// de dimensions nx et ny
x = mtlb_fscanf(fich,'%f',nx);
y = mtlb_fscanf(fich,'%f',ny);
// Lecture de la matrice a des temperatures, de dimension (nx,ny)
a = mtlb_fscanf(fich,'%f',[nx,ny]);
We can see that the translator does not recognize the matlab function fscanf.

Back to example summary

Exit