A preliminary introduction to Scilab

Source: Internet
Author: User
Tags rand rewind sin
matlab use old error, decided to try Scilab.
A few examples to learn Scilab

Example 1: The command to draw the contour line

Define a vector
x=[1 2 3; 1 3 5; 8 2 8];
The same command as Matlab
A=1:3;
B=1:3;
nz=2;
Draw an isoline graph, and NZ represents the value points of the isoline.
Contour2d (A,B,X,NZ)

The above command can exist in a file, such as A.sci, which can then be used in the Scilab command line
EXEC ("A.sci")
To execute, you will see the picture below


example 2:matlab's reshape command, under the Scilab is
Define a vector
-->x=[1 2 3 4 5 6 6 7 7 8]
x =

1.2.    3.4.    5.6.    6.7. 7.8.
Dimensions of a vector
-->size (x)
Ans =

1.10.
Define a matrix, equivalent to the y=reshape of Matlab (x,2,5)
-->y=matrix (x,2,5)
y =

1.3.    5.6. 7.
2.4.    6.7. 8.

Example 3: file, input and output
Create a new file and enter a random number into the file
U=file (' Open ', ' aa.txt ', ' unknown ')
For K=1:4
Random matrices for generating 1-by-4
A=rand (1,4)
Write (U,a)
End
Rewind makes the file's pointer return to the file header.
File (' Rewind ', u)
read out the 2-by-4 matrix.
X=read (u,2,4)
Close File
File (' Close ', u)


Example 4: Drawing functions with waterfall and image

X if it is a matrix, then
Mesh (x) will get an image, the following figure


Example 5: Convert the generated image into an EPS format to save
Set Title
Xtitle (' nk=1024, k=431:592 ')
Files stored in EPS format, the filename will be f1024zoom.eps
Xs2eps (0, ' f1024zoom ')

Example 6: Opening and writing files can use the format of the C program

P=mopen (' S2xx1024.sci ', ' R ');
nx=81;
nk=1024;
d=512; d1=nk/2-d+1; D2=nk/2+d;
The file holds a matrix of the NX row Nk column.
The following F reads all of the data, and%LG represents the read double precision
F=MFSCANF ( -1,p, '%lg ');
To convert the one-dimensional vector read above into a matrix, we should pay attention to the order of row and column parameters and turn to the matrix of the Nk row Nx
F=matrix (F,NK,NX);
Transpose again to get what we want. The matrix of the Nx row Nk column
F=f ';
Mclose (P);
P1=mopen (' K1.txt ', ' W ');
K1=1:nk;
k= (k1-nk/2-0.5)/nk;
Kk=k (D1:D2);
b = KK ';
pos = 1;
For POS
Y=f (POS,1:NK);
Yy=y (D1:D2);
MV = mean (yy);
b = [b yy '];
mfprintf (P1, '%16.15e%16.15e\n ', b)
File (' Close ', p1);

Example 7: Find a good spline curve to fit the data
This is a artifical example where the datas XD and yd
are build from a perturbed sin function
A = 0; b = 2*%pi;
Sigma = 0.1; Standard deviation of the Gaussian noise
m = 200; Number of experimental points
The following function gives 200 dividing points on [a,b], including two endpoints.
xd = linspace (a,b,m) ';
yd = sin (XD) + Grand (XD, "nor", 0,sigma);

We're going to construct a spline curve that makes the curve at the point
X (i), i=1,2,..., the function value of 6 equals y (i),
The guide value of the curve at point x (i) equals d (i)

n = 6; Number of breakpoints
x = Linspace (a,b,n) ';

Compute the Spline
The Lsq_splin function is to compute such a spline curve, remembering the spline curve and
{Y (i), d (i), = 1,2,。。。 , 6} has these one by one corresponding relationships, Lsq_splin
One of the calculated Xd,yd is the best approximation of the curve given by the original line.
The approximation is good or bad.
_m_ _m_
\ 2 \ 2
/WD (k) (S (XD (k))-yd (k)) <=/WD (k) (F (XD (k))-yd (k))
---                               ---
K=1 k=1

To judge.

[Y, d] = Lsq_splin (XD, yd, x); Use equal weights

Plotting
ye = sin (xd);
ys = Interp (xd, x, y, D);
Xbasc wiped away the original image and prepared to draw a new picture.
XBASC ()
PLOT2D (Xd,[ye yd ys],style=[2-2 3], ...
leg= "Exact function@experimental measures (Gaussian perturbation) @fitted spline")
Xtitle ("A least square spline")
Xselect ()

Example 8: Data fitting
There are some data X (i), Y (i), and then select a function form to come to these data.
There are some parameters in the form of this function, Scilab provides methods to determine these parameters.

To determine the form of a function is y = a * (x-b) + c*x^2
A, B, C is the parameter that needs to be determined
Deff (' Y=ff (x) ', ' y=a* (x-b) +c*x.*x ')
X=[]; Y=[];
A=34;b=12;c=14;for X=0:.1:3, Y=[Y,FF (x) +100* (rand ()-.5)]; X=[x,x];end
Z=[y; X];
Error function G (p,z), different parameters, different errors, find the smallest parameters that make the error.
Deff (' e=g (p,z) ', ' a=p (1), b=p (2), C=p (3), y=z (1), X=z (2), E=Y-FF (x) ')
3;5;10 is the initial value of three parameters
[P,err]=fit_dat (G,[3;5;10],z)
Xset (' window ', 0)
XBASC ();
PLOT2D (X ', Y ',-1)
PLOT2D (x ', FF (x) ', 5, ' 002 ')
A=p (1), b=p (2), C=p (3);p lot2d (x ', FF (x) ', 12, ' 002 ')

a=34;b=12;c=14;
You can also use the information of the derivative of the error function.
Deff (' S=DG (p,z) ', ' y=z (1), X=z (2), S=-[x-p (2),-P (1), x*x] ')
[P,err]=fit_dat (G,[3;5;10],Z,DG)
Xset (' window ', 1)
XBASC ();
PLOT2D (X ', Y ',-1)
PLOT2D (x ', FF (x) ', 5, ' 002 ')
A=p (1), b=p (2), C=p (3);p lot2d (x ', FF (x) ', 12, ' 002 ')

Example 9: Data fitting improved version of Fit_data
Generate the data
function Y=ff (x,p), y=p (1) * (X-p (2)) +p (3) *x.*x,endfunction
X=[]; Y=[];
PG=[34;12;14]//parameter used to generate data
For X=0:.1:3, Y=[y,ff (X,PG) +100* (rand ()-.5)]; X=[x,x];end
Z=[y; X];


The criterion function
function E=g (p,z),
Y=z (1), x=z (2);
E=y-ff (X,p),
Endfunction

Solve the problem
P0=[3;5;10]
[P,err]=datafit (G,Z,P0);

The following line is for drawing, the first one says with window (0), and the second is to erase the original image.
SCF (0); CLF ()
PLOT2D (X,ff (X,PG), 5)//the curve without noise
PLOT2D (x,y,-1)//The Noisy data
PLOT2D (X,ff (x,p),//the solution


The gradient of the criterion function
function S=dg (p,z),
A=p (1), b=p (2), C=p (3), y=z (1), X=z (2),
S=-[X-B,-A,X*X]
Endfunction

[P,err]=datafit (G,DG,Z,P0);
SCF (1); CLF ()
PLOT2D (X,ff (X,PG), 5)//the curve without noise
PLOT2D (x,y,-1)//The Noisy data
PLOT2D (X,ff (x,p),//the solution

The main difference between Scilab and Matlab (Chinese characters are part of the function I used)

Functions in Scilab are is not Matlab m-files but variables. One or several functions can is defined in a single file (say Myfile.sci). The name of the file is not necessarily related to the name of the functions. The name of the function (s) is given by function [Y]=fct1 (x)
...
function [Y]=fct2 (x)
...

The function (s) are not automatically loaded into Scilab. Usually you have to execute the command getf ("Myfile.sci") before using it.

Functions can also be defined on-line (or inside functions) by the command Deff.

Command line composition of the script file, Matlab as long as the input file name can be, Scilab execution mode is input exec ("filename")

Comment lines

Scilab comments begins with://

Matlab comments begins with:% Variables

Predefined variables usually have the% prefix in Scilab (%i,%inf, ...). They are write protected. Strings

Strings are considered as 1 by 1 matrices of Strings in Scilab. Each entry of the A string matrix has its own length. Boolean variables

Boolean variables are%T,%F in Scilab and 0, 1 in Matlab. Indexing with Boolean variables could not produce same result. Example x=[1,2];x ([1,1]) [which is isn't X ([%t,%t])] returns [1,1] in Scilab and [1,2] in Matlab. Also if x is a matrix X (1:n,1) =[] or X (:) =[] is not valid in Matlab.

Polynomials

Polynomials and polynomial matrices are defined by the function poly in Scilab (built-in variables). They are considered as vectors of coefficients in Matlab. Empty matrices

[]+1 returns 1 in Scilab and [] in Matlab. Plotting

Except for the "simple" plot and mesh (Plot3d) functions, Scilab and Matlab are not compatible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.