Está en la página 1de 9

TUTORIAL B�SICO DE SCILAB

clc //clear the screen


clear a //mata o desaparece la variable a
help (con min�sculas) abre el help browser...
help for te lleva directamente al tema de la instrucci�n for loop
who lista todas las variables

comentarios empiezan con //

diary ("c:\sci.log") Empieza a grabar la sesi�n en dicho file.


diary(0) terminar sesi�n de grabado. Notepad ??/ mejor WORD

save('prac3.sci', x, y) Salva las variables listadas o el medio ambiente


completo si no hay variables.
load('prac3.sci') Reestablece lo salvado.
save&load disponible tambi�n desde scilab FILE

*SciLab es sensitivo a may�sculas y min�sculas a != A


*SciLab confirma con ECO cada estatuto excepto si estatuto termina con ;
*Diferentes estatutos pueden ir en un solo rengl�n separados por ;
Un vector rengl�n se define: A = [ 1 2 4 6 ]
Un vector rengl�n con elementos equidistantes se puede definir: X = 0:.1:1 y ser�a
equivalente a definirlo manualmente as�: X = [ 0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1.0 ]

Para graficar un polinimio se siguen 3 pasos


1 Definir vector de abcisas.
2 Definir relaci�n de funci�n
3 Graficar con plot(x,y)

Solamente se grafican los puntos discretos correspondientes a el vector de x


definido y esos puntos se unir�n mediante l�neas rectas para formar una funci�n
"continua"

Ejemplo:
Graficar el polinomio de p�gina 624 de CHAPRA COMENTARIOS

x = 0:.01:0.8 //define vector X desde 0 .01 .


02 ....0.79 0.80
y = 400*x^5-900*x^4+675*x^3-200*x^2+25*x+0.2 //define funci�n polinomial de
p�gina 624
plot(x,y) //graficar x vs y OJO: cuidar NO espacios

A = [ 1 2 1 ; 2 1 4 ; 1 2 2 ]
A^2 //Operaci�n MAtricial
A*A //Operaci�n MAtricial
A.^2 //Operaci�n por elemento
A.*A
Sistemas de ecuaci�n lineal.

1X1 + 2X2 + 3X3 = 1


4X1 + 5X2 + 6X3 = -2
7X1 + 8X2 + 10 X3 = 5

Definici�n de la matriz de coeficientes de 3x3


B = [ 1 ; -2 ; 5] Definici�n de vector columna de elem indep. del
sistema.
B = [ 1 -2 5 ]' Definici�n alterna como vector rengl�n
transpuesto

inv(A) Obtiene la inversa de la matriz A


det(A) Obtiene el determinante de la matriz A

Soluci�n de un sistema de ecuaciones lineales definido previamente.

A \ B Soluci�n de sistema (alternativa 1)


inv(A) * B Soluci�n de sistema (alternativa 2)
A^-1 * B Soluci�n de sistema (alternativa 3)
1/A * B

AUM = [ A B ] Definici�n de matriz aumentada de 3x4


(Coeficientes y B)

Podemos tambi�n tener subconjuntos de una matriz


A(:,:) Matriz que se forma con todos los reng. y todas
las cols de
A(1,:) Vector rengl�n formado por el primer rengl�n
completo de A
A(3,:) Vector rengl�n formado por el tercer rengl�n
completo de A
A(:,2) Vector columna formado por columna 2 completa
de A
A(:,2:3) Matriz firmada por las columnas 2 y 3 completas de A
A(1:2,2:3) Matriz formada por los reng 1 y 2 de la col 2 a la 3

REGLA DE CRAMER para soluci�n de sistemas de ecuaci�n lineal


Soluci�n del sistema definido arriba para una variable independiente a la vez:

C1 = [B A(:,2:3)] Matriz Original de 3x3 pero con elementos indep en


col 1
det(C1)/det(A) Valor de X1

C2 = [A(:,1) B A(:,3)] Matriz Original de 3x3 pero con elementos indep


en col 2
det(C2)/det(A) Valor de X2

C3 = [A(:,1:2) B] Matriz Original de 3x3 pero con elementos indep en


col 3
det(C3)/det(A) Valor de X3

s="Hello World"
disp(s)

editor invoca al editor de scilab para crear/editar/correr scripts

end / resume / abort

Usando el editor de SCILAB podemos cargar y correr programas en SCILAB:


Por ejemplo el archivo puntofijo.sce puede correr un programa que hace el proceso
iterativo para soluci�n del ejemplo de sistemas de ecuaciones NO-lineales a trav�s
del m�todo de punto de la presentaci�n PPT de este tema.
1) teclear EDIT() en la consola SCILAB
En el editor de SCILAB:
2) FILE > OPEN > puntofijo.sce
3) EXECUTE > LOAD INTO SCILAB versi�n 5.0.3
3) Execute > ...until de caret (^)
El programa corre automaticamente y muestra la tabla de resultados de iteraciones.

Podemos correr directamente el script desde el editor o desde la consola con exec(
)

Ejemplos:

for i=1:5
disp(i)
end

for i=1:2:5
disp(i)
end

for i=5:-1:1
disp(i)
end

//Tabular valores de una funci�n


disp("Tabulaci�n de y=x^2")
for x=0.1:.1:1
y=x*x;
v = [x,y];
disp(v)
end

//Clasificar un tri�ngulo en base a sus �ngulos


a=60; b=60; c=60
if (a>90 | b>90 | c>90) then
disp("Es un tri�ngulo obtus�ngulo")
else
if (a==90 | b==90 | c==90) then
disp("Es un tri�ngulo rect�ngulo")
else
disp("Es un tri�ngulo acut�ngulo")
end
end

//Clasificar tri�ngulo en base a sus lados


a=5; b=5; c=5;
if (a<>b & a<>c & b<>c) then
disp("El tri�ngulo es escaleno")
else
if (a==b & b==c) then
disp("El tri�ngulo es equil�tero")
else
disp("el tri�ngulo es is�sceles")
end
end

x=input("x?")

x=input("x?"); //sin eco

//Clasificar tri�ngulo en base a sus lados


//a=5; b=5; c=5;
a=input("lado a?")
b=input("lado b?")
c=input("lado c?")
if (a<>b & a<>c & b<>c) then
disp("El tri�ngulo es escaleno")
else
if (a==b & b==c) then
disp("El tri�ngulo es equil�tero")
else
disp("el tri�ngulo es is�sceles")
end
end

//ejemplo 6.3 Chapra M�todo de Newton Raphsonx1=0.1;


x=0.0;
for i=0 : 6

V = [i, x];
disp(V)
xnew=x-(exp(-x)-x)/(-exp(-x)-1);
x=xnew;
end

//Serie de fibonacci

//Listar para cada uno de los n�meros del 2 al 20


//si son m�ltiplos de los n�meros del 2 al 5.
//Funciones definidas por el usuario:
function y = cubo (x)
y= x^3
endfunction

//Funci�n recursiva de Factorial


function y = homefact (x)
if x==0 then
y=1
else
y=x*homefact(x-1)
end
endfunction

//Funci�n con m�s de un valor de regreso.


function [x,y] = newfun(a,b)
x=a+b
y=a-b
endfunction

Si al usar la funci�n NO asigno a un vector s�lo se muestra el primero de los


elementos. Para obtener ambas respuestas hay que asignar a un vector:

[x y] = newfun(5,2)

funci�n para encontrar el factorial

VER: Efficient Scilab programming


1) Replacing loops by vector-based operations
2) Using submatrices
3) Using find()

http://audition.ens.fr/brette/Scilab/efficientscilab.html

El resto del archivo est� copiado de


http://turing.une.edu.au/~amth142/Practicals_2002/Practical_03/practical/

Scilab and Data

Scilab has two pairs of functions read/write and load/save for handling data and
files.
save and load

This pair of functions is used to save and reload data produced by Scilab. These
are associated with binary files which cannot be read (sensibly) by other programs.
Here is an example:
-->x = rand(4, 4)
x =

! 0.2214747 0.1335728 0.4425213 0.4500451 !


! 0.9484589 0.4476816 0.0008488 0.7655987 !
! 0.7692214 0.5382251 0.6587566 0.7237357 !
! 0.6254166 0.8118657 0.1749406 0.7900157 !

-->y = rand(4, 4)
y =

! 0.1131045 0.2698470 0.3557944 0.4540130 !


! 0.8529308 0.9794279 0.9300245 0.8000723 !
! 0.6521550 0.8359301 0.5113602 0.9489469 !
! 0.3969470 0.8677244 0.5315443 0.0760549 !
These are a pair of random matrices (because they are random you are not going to
get the same numbers as above). Now we can save them to a file, say prac3.sci
-->save('prac3.sci', x, y)
We now clear the variables and check that they are no longer defined in Scilab:
-->clear x

-->clear y

-->x
!--error 4
undefined variable : x

-->y
!--error 4
undefined variable : y
We can now reload these variables from the file we have created:
-->load('prac3.sci')

-->x
x =

! 0.2214747 0.1335728 0.4425213 0.4500451 !


! 0.9484589 0.4476816 0.0008488 0.7655987 !
! 0.7692214 0.5382251 0.6587566 0.7237357 !
! 0.6254166 0.8118657 0.1749406 0.7900157 !

-->y
y =

! 0.1131045 0.2698470 0.3557944 0.4540130 !


! 0.8529308 0.9794279 0.9300245 0.8000723 !
! 0.6521550 0.8359301 0.5113602 0.9489469 !
! 0.3969470 0.8677244 0.5315443 0.0760549 !
The most important use of save and load is save work when you leave Scilab and then
reload it when you start Scilab again. Note That if you use save without specifying
the variables to be saved, all variables from the current session will be saved.
save and load can be accessed from the file menu in Scilab. This is not often
useful under Linux but can be handy for Windows users.
read and write

These operate on text files containing numerical data. Their most important use is
to produce data for other programs and to manipulate and plot data from other
sources.
-->z = rand(8,4)
z =

! 0.0230195 0.6462853 0.7603933 0.6026597 !


! 0.4854525 0.8153841 0.3486790 0.6117310 !
! 0.5553231 0.8279698 0.6411709 0.4927101 !
! 0.2435235 0.6809521 0.4087678 0.2173720 !
! 0.1461701 0.9092572 0.2372431 0.4418458 !
! 0.3894849 0.0130122 0.5667381 0.9798274 !
! 0.4810411 0.0500522 0.7038741 0.5259225 !
! 0.0406748 0.9779498 0.5660378 0.6909824 !

-->write('prac3.out', z)
If you examine the file prac3.out it will look something like:
0.0230194512 0.646285265 0.760393302 0.602659694
0.48545251 0.815384094 0.348678976 0.611731049
0.555323126 0.827969833 0.641170944 0.49271012
0.243523459 0.680952108 0.408767792 0.217371969
0.146170148 0.909257194 0.237243087 0.441845817
0.389484918 0.0130122323 0.566738097 0.979827398
0.481041052 0.0500522447 0.703874052 0.525922459
0.0406747693 0.977949818 0.56603785 0.690982413
Note that only one matrix can be written to a file at any one time. We can read the
data back in to a matrix:
-->z1 = read('prac3.out', 8, 4)
z1 =

! 0.0230195 0.6462853 0.7603933 0.6026597 !


! 0.4854525 0.8153841 0.3486790 0.6117310 !
! 0.5553231 0.8279698 0.6411709 0.4927101 !
! 0.2435235 0.6809521 0.4087678 0.2173720 !
! 0.1461701 0.9092572 0.2372431 0.4418458 !
! 0.3894849 0.0130122 0.5667381 0.9798274 !
! 0.4810411 0.0500522 0.7038741 0.5259225 !
! 0.0406748 0.9779498 0.5660378 0.6909824 !
Some care has to be taken when using read. read assumes that the data is written in
columns, as in the file prac3.out' above. When read is used the number of rows and
columns in the data file are usually specified as in the example above, but there
are some variations.
-->z2 =read('prac3.out', 3, 3)
z2 =

! 0.0230195 0.6462853 0.7603933 !


! 0.4854525 0.8153841 0.3486790 !
! 0.5553231 0.8279698 0.6411709 !

-->z3 =read('prac3.out', 2, 5)
z3 =

! 0.0230195 0.6462853 0.7603933 0.6026597 0.4854525 !


! 0.5553231 0.8279698 0.6411709 0.4927101 0.2435235 !

-->z4 =read('prac3.out', -1, 4)


z4 =

! 0.0230195 0.6462853 0.7603933 0.6026597 !


! 0.4854525 0.8153841 0.3486790 0.6117310 !
! 0.5553231 0.8279698 0.6411709 0.4927101 !
! 0.2435235 0.6809521 0.4087678 0.2173720 !
! 0.1461701 0.9092572 0.2372431 0.4418458 !
! 0.3894849 0.0130122 0.5667381 0.9798274 !
! 0.4810411 0.0500522 0.7038741 0.5259225 !
! 0.0406748 0.9779498 0.5660378 0.6909824 !
In the first example above, we just read the first three rows and columns of the
data. In the second example the first ten data values were read into a matrix. If
you know the number of columns in a data file, but can't be bothered counting the
number of rows, you can simply use -1 for the number of rows and all rows of the
data will be read.
Graphics Files

Saving Graphs

Graphs can be saved in Scilab specific binary files and then reloaded much the same
way that save and load work. The easiest way to save or load a graph is via the
file menu on the graphics window. Again, this is typically used to save graphs when
you leave Scilab to be reloaded when you start Scilab again. Only one graph can be
saved at a time.
Exporting Graphs

Scilab can export graphics in a number of formats. We will only be concerned with
the .eps (encapsulated postscript) format. We will redo the graph from Practical 2.
-->x = linspace(-10, 10, 1000);

-->y = 2*sin(x) + 3*sin(2*x) + 7*sin(3*x) + 5*sin(4*x);

-->plot2d(x,y)
To export the graph, go to the file menu on graphics window and select export.
Select Postscript, Landscape and choose a filename. You should then be able to view
the graph outside Scilab using a postscript viewer (e.g. gv on Linux).
Doing Assignment 2

For Linux users this should now be pretty straightforward since we have already
covered everything you need to know. For Windows users the only thing that could
cause problems is keeping track of where the various files are. This can be a bit
tricky since Scilab and WinEdt/MiKTeX tend to look in different places for files.
Again, all of this can vary from user to user so there is no definitive way to
handle this problem. Basically it is up to you to know where yuor files live.
Scilab has to know the file dj.dat is.
WinEdt/MiKTeX has to know where the .eps produced by Scilab is.
Once you have worked this out the mechanics should be more or less straightforward.
About this document ...

AMTH142 Practical 3

Scilab -- Files and Data


This document was generated using the LaTeX2HTML translator Version 2K.1beta (1.61)

Copyright � 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit,
University of Leeds.
Copyright � 1997, 1998, 1999, Ross Moore, Mathematics Department, Macquarie
University, Sydney.

The command line arguments were:


latex2html -split +0 practical.tex

The translation was initiated by Applied Mathematics 142 on 2002-08-09


A=input('jugador1')
B=input('jugaodr2')
if (A==1)&(B==2)|(A==2)&(B==3)|(A==3)&(B==1) then
disp('jugador i gana')
elseif (B==1)&(A==2)|(B==2)&(A==3)|(B==3)&(A==1)then
disp('jugador 2 gana')
else
disp('nadie gana')
end

También podría gustarte