3
Modelagem e Simulação de Processos
UFMG
22
Modelagem e Simulação de Processos
UFMG
11
Modelagem e Simulação de Processos
UFMG
1
Modelagem e Simulação de Processos
UFMG
15
Modelagem e Simulação de Processos
UFMG
7
Modelagem e Simulação de Processos
UFMG
28
Modelagem e Simulação de Processos
UFMG
19
Modelagem e Simulação de Processos
UFMG
23
Modelagem e Simulação de Processos
UFMG
Texto de pré-visualização
Scilab for very beginners 1233 Chapter 2 Programming In the examples given in this document any line preceded by is a command the other lines are the returns from commands calculation results error messages Do not write in the editor They are introduced here to make the distinction between command lines and calculation results as it is displayed in the console after copyingpasting When presented in a table without and no calculation result the commands are depicted exactly as they should be typed in the editor Variables assignment and display Variables Scilab is not a computer algebra system It calculates only with numbers All calculations are done with matrices although this may go unnoticed Even if the concept of matrices is unknown vectors and sequences of numbers can explain it as they are in fact matrices of dimension 1 n or n 1 and a number is itself a matrix of dimension 1 1 Variables do not need to be declared in advance but any variable must have a value For example obtaining the value of a variable which has not been given a value produces an error a error 4 Undefined variable a If a value is assigned to the variable a there is no longer an error api4 a 07853982 a a 07853982 Variables may take any name that is not already defined by the system Piby2pi2 Piby2 15707963 Mention Like the Scilab functions a variable name must not have accents or special characters Scilab for very beginners 1333 The result of a calculation that is not assigned to a variable is automatically assigned to the variable called ans 342 ans 6 ans ans 6 Functions Functions are the easiest and most natural way to make computations from variables and for obtaining results from variables A function definition begins with function and ends with endfunction For example to convert euros e in dollars d with an exchange rate t the dollars function is defined The variables are e and t and the image is d function ddollarset det endfunction dollars20014 ans 280 Very usually numerical functions are functions of one real variable For example two functions f and g are defined using the following commands function yfx y368expx endfunction function ygx y4x94 endfunction The defined functions can then be used to calculate values f10 ans 44999745 g125 ans 95555556 Mention The variables x and y are dummy variables their names can be reused when defining other functions or in Scilab Scilab for very beginners 1433 Requesting the assignment of a variable Assignment is made easily using the operator Display Writing Typing the name of a variable displays its value except when there is at the end of the command line Brackets Matrices are defined using square brackets see page 23 As mentioned before matrix computation is the basis of calculations in Scilab A space or comma is used to separate columns and semicolons are used to separate rows To define a column vector and obtain a column display v325 v 3 2 5 To define a row vector and obtain a row display v325 v 3 2 5 To define a matrix and obtain a tabular display m1 2 34 5 67 8 9 m 1 2 3 4 5 6 7 8 9 Mention This command can also be typed under the form v3 2 5 Mention This command can also be typed under the form m123456789 Scilab for very beginners 1533 disp function disp must always be used with parentheses With the vector v previously defined v2 ans 2 dispv2 2 To display a string usually a sentence put it between quotes dispBob won Bob won To display a combination of words and values use the string command which converts values to character strings using a between the different parts d500 dispBob won stringd dollars Bob won 500 dollars If the sentence contains an single quote the latter needs to be doubled in the string to be displayed properly dispIts fair Its fair Loops Incrementation The operator allows to define vectors of numbers whose coordinates are in arithmetic sequence We give beginning value step ending value It is possible that ending value is not reached If the step is not mentioned its default value is 1 For example to define a row vector of integers which increments in steps of 1 from 3 and 10 310 ans 3 4 5 6 7 8 9 10 or which increments in steps of 2 from 1 to 10 1210 ans 1 3 5 7 9 Scilab for very beginners 1633 or which decreases in steps of 4 from 20 to 2 u2042 u 20 16 12 8 4 For The easiest loop structure for a fixed number of iterations is written with for end Example Calculation of 20 terms of a sequence defined by recurrence by 𝑢 4 𝑢 𝑢 2𝑛 3 Algorithm Scilab Editor Put 4 in u1 For n from 1 to 20 un1 takes the value un2n3 and un Display n and un End for u14 for n120 un1 un2n3 dispnun end While To stop a loop when a given goal is reached while end is used I planted a Christmas tree in 2005 measuring 120 m It grows by 30 cm per year I decided to cut it when it exceeds 7 m In what year will I cut the tree Algorithm Scilab Editor Put 12 in h h tree height Put 2005 in y y year While h7 h takes the value h03 the tree grows y takes the value y1 one year passes End while Display y the final year h12 y2005 while h7 hh03 yy1 end dispI will cut the tree in stringy Mention When a command is too long to be written in one line the editor will wrap lines One can also wrap lines using two dots before going to the next line Scilab for very beginners 1733 Tests Comparison operators Useful tests include comparing numbers or determining whether a statement is true or false Below the corresponding commands Equal Different Less Greater Less or equal Greater or equal True False And Or No T F To compare two vectors or two matrices the tests and will compare term by term For example X125 Y535 XY ans F F T To test if two vectors are equal isequal is used and isequal is used if they are different isequalXY ans F isequalXY ans T Ifthen The basic conditional statements are the following if then else end if then elseif then else end if then must be written on the same line and likewise with elseif then Mention Be cautious with calculation accuracy Calculations are approached and will sometimes give wrong results see Calculation accuracy page 30 Scilab for very beginners 1833 Example Alice throws three dice If she gets three 6s she wins 20 If she gets three identical numbers different from 6 she wins 10 If she gets two identical numbers she wins 5 Otherwise she wins nothing Simulate a trial and calculate Alices winnings using the functions grand see page 22 uniqueDwhich keeps only once the values that appears several times in D lengthuniqueD which returns the size of the obtained vector that is to say 1 if three dice are equal and 2 if two dice are equal Algorithm Scilab Editor Put the three values in D If Alice gets three 6 then Alice wins 20 dollars Else if she receives three identical values then Alice wins 10 dollars Else if she receives two identical values then Alice wins 5 dollars Otherwise Alice wins nothing End if Display Alices winnings Dgrand13uin16 if D666 then W20 elseif lengthuniqueD1 then W10 elseif length uniqueD2 then W5 else W0 end dispAlice wins stringW dollars 2D and 3D plots plot command is used to create plots in the plane Color and appearance can be specified by putting indications of color and points style within quotes Colors b blue by default k black r red g green c cyan m magenta y yellow w white Point styles Joined by default or o x Other options are available with s d v and Scilab for very beginners 1933 Basic plots To plot a point Plot the point A1 2 with a red point Scilab Editor Graphics Window plot12r To plot a segment Plot the segment AB in blue by default with A1 2 and B3 5 Scilab Editor Graphics Window plot1325 Plots of plane curves defined by functions yfx For a function 𝑥 𝑓𝑥 the values of x are specified using the linspace command by writing xlinspaceabn in which a is the smallest value of the variable 𝑥 b the highest value of 𝑥 and n the number of values calculated between a and b Do not forget the sympbol otherwise all n values of 𝑥 will be displayed For example consider two functions 𝑓 and 𝑔 defined over the interval 2 5 by 𝑓 𝑥 𝑥 2𝑥𝑒 and 𝑔 𝑥 sin Scilab for very beginners 2033 With the program below we plot the curve defined by𝑓 in blue by default Scilab Editor Graphics Window function yfx yx22xexpx endfunction xlinspace2550 plotxf By adding the program below we obtain the plot of two curves that of f in red and that of g in green The previous plot was cleared through the clf command clear figure Scilab Editor Graphics Window function ygx ysinx2 endfunction xlinspace2550 clf plotxfrxgg Plots of sequences of points Terms of a sequence The most common case is to plot the sequences of points 𝑀𝑛 𝑢 𝑛 after calculating the coordinates 𝑢𝑛 of a vector 𝑢 plotur specifies the style and color of the points in quotes red and nonconnected stars By default points are plotted in blue and are connected Scilab Editor Graphics Window for n150 un08n end clf plotur Scilab for very beginners 2133 Bivariate statistical data Bivariate statistical data are given in the form of two vectors lets call them X and Y plotXY will plot a scatter plot of 𝑀𝑋 𝑌 with blue triangles Scilab Editor Graphics Window X13377910 Y8755422 clf plotXY Plots in 3 dimensions Scilab can be used to plot surfaces and curves in space with many options for the treatment of hidden faces face colors points of view etc The following examples will illustrate 3D plots The surf function can be used for plotting surfaces This function has three input variables x y and z x and y are respectively vectors of size 𝑚 and 𝑛 corresponding to points on the axes 𝑂𝑥 and 𝑂𝑦 z is a matrix of dimension 𝑛𝑚 with element 𝑧 corresponding to the height of the point with Xcoordinate 𝑥 and Ycoordinate 𝑦 To plot the surface defined by a function of the form 𝑧 𝑓𝑥 𝑦 it is necessary to Define function 𝑓 Calculate zfevalxyf fevalxyf returns the 𝑚𝑛 matrix whose 𝑖𝑗 element is 𝑓𝑥 𝑦 which will be transposed by using the single quote symbol Execute surfxyz To plot the surface 𝑧 2𝑥 𝑦 elliptic paraboloid Scilab Editor Graphics Window function zfxy z2x2y2 endfunction xlinspace11100 ylinspace22200 zfevalxyf clf surfxyz Scilab for very beginners 2233 Curves in space may be plotted using the param3d function param3d has three arguments x y and z each vectors has the same dimension and corresponds to the points 𝑥 𝑦 𝑧 on the curve To plot the helix defined by 𝑥 cos 𝑡 𝑦 sin 𝑡 𝑧 𝑡 Scilab Editor Graphics Window tlinspace04pi100 param3dcostsintt Simulations and statistics Several functions are available in Scilab to perform simulations quickly and efficiently Random sequences grand1puinmn returns a vector of 𝑝 random integer sequences between 𝑚 and 𝑛 with 𝑝 positive integer 𝑚 and 𝑛 integers and 𝑚 𝑛 t grand14uin16 t 3 1 3 6 grand1punfab returns a vector of 𝑝 random real sequences between 𝑎 and 𝑏 with 𝑝 positive integer 𝑎 and 𝑏 real and 𝑎 𝑏 tr grand12unf11 tr 07460264 09377355 Scilab for very beginners 2333 Statistics All the standard statistical functions are listed on page 32 Keep particularly in mind The function barxncolor which draws bar graphs Scilab Editor Graphics Window x110 n8613106416785 clf barxn For a bar graph representing two sets side by side consider the series of values X and the two series of numbers n1 and n2 For plotting n1 and n2 must be column vectors that is why transposes are used in the example below Scilab Editor Graphics Window X125n15105n2687 barXn1n2 The color optional argument defines the color as in the plot function Additional information on matrices and vectors Accessing elements Square braces are used to define matrices A space or a comma is used to switch from one column to another and semicolons are used to switch from one line to another m1 2 34 5 6 m 1 2 3 4 5 6 Parentheses are used to access elements or to modify them Mention This command can also be typed under the form m123456 Scilab for very beginners 2433 m23 ans 6 m2323 m 1 2 3 4 5 23 The operator is used to designate all the rows or all columns of a matrix To view the second row of the matrix m type m2 ans 4 5 23 And the third column m3 ans 3 23 To obtain the transpose of a matrix or a vector use the single quote symbol m ans 1 4 2 5 3 23 Operations The operations are matrix operations To make element wise operations we need to put a dot before the operating sign A123456 A 1 2 3 4 5 6 B112 Scilab for very beginners 2533 B 1 1 2 AB ans 9 21 Matrix multiplication AA error 10 Inconsistent multiplication Dimensions are not consistent AA ans 1 4 9 16 25 36 Element wise multiplication 2A2 ans 6 8 10 12 14 16 The operation is performed on each element because 2 is a number AA ans 1 1518D16 3795D15 1 Gives the matrix X for which XA A The exact result is 1 0 0 1 For reasons of calculation accuracy the result can be slightly different depending on your version of Scilab and your operating system see calculation accuracy page 29 AA ans 1 1 1 1 1 1 Gives the matrix divided element wise In the case of vectors C14 C 1 2 3 4 CC error 10 Inconsistent multiplication Dimensions are not consistent Scilab for very beginners 2633 CC ans 1 4 9 16 It is also possible to write C2 because for vectors exponents are interpreted as an operation element wise This is not the case with matrices 1C ans 00333333 00666667 01 01333333 In this particular case with vectors we find the vector X for which CX 1 1C ans 1 05 03333333 025 Reverse element wise As previously C1 is possible The parentheses around 1 are necessary so that the point is not considered as a comma as part of the number 1 It is also possible to write 1 C with a space between 1 and Solving linear systems To solve the linear system AX Y in which A is a square matrix use the backslash X A Y Be cautious the operation Y A will give only if the dimensions are correct another result that is to say the matrix Z for which Z A Y To solve the system 1 2 3 4 5 6 𝑋 1 1 A1 2 34 5 6 Y11 XAY X 05 0 05 AX ans 1 1 Scilab for very beginners 2733 Some useful functions Sort The gsort function is used to sort the elements of a vector in ascending or descending order v2696402 v 2 6 9 6 4 0 2 gsortvgi ans 4 0 2 2 6 6 9 gsortv ans 9 6 6 2 2 0 4 Length The length function returns the number of coordinates for a vector The size function returns the dimensions rows columns for a matrix U110 U 1 2 3 4 5 6 7 8 9 10 lengthU ans 10 m1 2 34 5 6 sizeU ans 2 3 Scilab for very beginners 2833 Sum and product The sum and prod functions respectively calculate the sum and the product of their argument elements U110 sumU ans 55 prodU ans 3628800 Unique The unique function keeps only once the elements of a vector even if they are repeated several times and sorts them in ascending order v2696402 v 2 6 9 6 4 0 2 uniquev ans 4 0 2 6 9 Find The find function searches for elements in a vector or a matrix and returns a vector containing the corresponding indices To find all the elements of the vector 𝑤 smaller than 5 w153814732126 findw5 ans 1 3 7 8 The resulting vector 1378 indicates that elements 𝑤 𝑤 𝑤 and 𝑤 are smaller than 5 Scilab for very beginners 2933 To find all the elements of the vector 𝑤 equal to 3 w153814732126 findw3 ans 3 7 The resulting vector 37 indicates that elements 𝑤 and 𝑤 are equal to 3 Accuracy computation Computation Numbers have an absolute value between about 2210308 and 1810308 The number eps equals to 2220446049D16 gives the smallest relative precision that can be obtained in computations which therefore gives about 16 decimal digits Example 1 Computation of sinπ sinpi ans 1225D16 The value of 𝑠𝑖𝑛 𝜋 above is not 0 but it is considered as zero Indeed compared to the maximum value of the sine function ie 1 it is equal to 0 with a value less than eps Example 2 Testing if the triangle with sides 𝟑 1 et 2 is a rightangled triangle asqrt3 a 17320508 b1 b 1 c2 c 2 a2b2c2 ans F The program responds false because the value of a2 b 2 is approximate Scilab for very beginners 3033 absa2b2c2eps ans F The program responds false because absolute precision is called for absa2b2c2c2eps ans T The program responds true because relative precision is called for Display Results are displayed by default with 10 characters including the decimal point and the sign The format function is used to display more digits To display 20 digits type format20 Reconsidering 𝑎 3 a2 ans 3 Here there are 7 decimal places and we do not see the error format20 a2 ans 299999999999999956 Here there are 17 decimal places and we can see the error Solving differential equations To find the solutions of an explicit system of differential equations simply reduce to differential equations of order 1 𝑦 𝑡 𝑓 𝑡 𝑦 𝑡 𝑦 𝑡 𝑦 in which 𝑡 and 𝑡 represent time and 𝑦 and 𝑦𝑡 are vectors then apply the ode function yodey0t0tf with y0 initial condition vector of dimension 𝑛 t0 initial time t vector of dimension T corresponding to the times in which the solution is computed This vector must begin with t0 f function defining the system under the form function yprimfty yprim1 yprim2 yprimn endfunction
3
Modelagem e Simulação de Processos
UFMG
22
Modelagem e Simulação de Processos
UFMG
11
Modelagem e Simulação de Processos
UFMG
1
Modelagem e Simulação de Processos
UFMG
15
Modelagem e Simulação de Processos
UFMG
7
Modelagem e Simulação de Processos
UFMG
28
Modelagem e Simulação de Processos
UFMG
19
Modelagem e Simulação de Processos
UFMG
23
Modelagem e Simulação de Processos
UFMG
Texto de pré-visualização
Scilab for very beginners 1233 Chapter 2 Programming In the examples given in this document any line preceded by is a command the other lines are the returns from commands calculation results error messages Do not write in the editor They are introduced here to make the distinction between command lines and calculation results as it is displayed in the console after copyingpasting When presented in a table without and no calculation result the commands are depicted exactly as they should be typed in the editor Variables assignment and display Variables Scilab is not a computer algebra system It calculates only with numbers All calculations are done with matrices although this may go unnoticed Even if the concept of matrices is unknown vectors and sequences of numbers can explain it as they are in fact matrices of dimension 1 n or n 1 and a number is itself a matrix of dimension 1 1 Variables do not need to be declared in advance but any variable must have a value For example obtaining the value of a variable which has not been given a value produces an error a error 4 Undefined variable a If a value is assigned to the variable a there is no longer an error api4 a 07853982 a a 07853982 Variables may take any name that is not already defined by the system Piby2pi2 Piby2 15707963 Mention Like the Scilab functions a variable name must not have accents or special characters Scilab for very beginners 1333 The result of a calculation that is not assigned to a variable is automatically assigned to the variable called ans 342 ans 6 ans ans 6 Functions Functions are the easiest and most natural way to make computations from variables and for obtaining results from variables A function definition begins with function and ends with endfunction For example to convert euros e in dollars d with an exchange rate t the dollars function is defined The variables are e and t and the image is d function ddollarset det endfunction dollars20014 ans 280 Very usually numerical functions are functions of one real variable For example two functions f and g are defined using the following commands function yfx y368expx endfunction function ygx y4x94 endfunction The defined functions can then be used to calculate values f10 ans 44999745 g125 ans 95555556 Mention The variables x and y are dummy variables their names can be reused when defining other functions or in Scilab Scilab for very beginners 1433 Requesting the assignment of a variable Assignment is made easily using the operator Display Writing Typing the name of a variable displays its value except when there is at the end of the command line Brackets Matrices are defined using square brackets see page 23 As mentioned before matrix computation is the basis of calculations in Scilab A space or comma is used to separate columns and semicolons are used to separate rows To define a column vector and obtain a column display v325 v 3 2 5 To define a row vector and obtain a row display v325 v 3 2 5 To define a matrix and obtain a tabular display m1 2 34 5 67 8 9 m 1 2 3 4 5 6 7 8 9 Mention This command can also be typed under the form v3 2 5 Mention This command can also be typed under the form m123456789 Scilab for very beginners 1533 disp function disp must always be used with parentheses With the vector v previously defined v2 ans 2 dispv2 2 To display a string usually a sentence put it between quotes dispBob won Bob won To display a combination of words and values use the string command which converts values to character strings using a between the different parts d500 dispBob won stringd dollars Bob won 500 dollars If the sentence contains an single quote the latter needs to be doubled in the string to be displayed properly dispIts fair Its fair Loops Incrementation The operator allows to define vectors of numbers whose coordinates are in arithmetic sequence We give beginning value step ending value It is possible that ending value is not reached If the step is not mentioned its default value is 1 For example to define a row vector of integers which increments in steps of 1 from 3 and 10 310 ans 3 4 5 6 7 8 9 10 or which increments in steps of 2 from 1 to 10 1210 ans 1 3 5 7 9 Scilab for very beginners 1633 or which decreases in steps of 4 from 20 to 2 u2042 u 20 16 12 8 4 For The easiest loop structure for a fixed number of iterations is written with for end Example Calculation of 20 terms of a sequence defined by recurrence by 𝑢 4 𝑢 𝑢 2𝑛 3 Algorithm Scilab Editor Put 4 in u1 For n from 1 to 20 un1 takes the value un2n3 and un Display n and un End for u14 for n120 un1 un2n3 dispnun end While To stop a loop when a given goal is reached while end is used I planted a Christmas tree in 2005 measuring 120 m It grows by 30 cm per year I decided to cut it when it exceeds 7 m In what year will I cut the tree Algorithm Scilab Editor Put 12 in h h tree height Put 2005 in y y year While h7 h takes the value h03 the tree grows y takes the value y1 one year passes End while Display y the final year h12 y2005 while h7 hh03 yy1 end dispI will cut the tree in stringy Mention When a command is too long to be written in one line the editor will wrap lines One can also wrap lines using two dots before going to the next line Scilab for very beginners 1733 Tests Comparison operators Useful tests include comparing numbers or determining whether a statement is true or false Below the corresponding commands Equal Different Less Greater Less or equal Greater or equal True False And Or No T F To compare two vectors or two matrices the tests and will compare term by term For example X125 Y535 XY ans F F T To test if two vectors are equal isequal is used and isequal is used if they are different isequalXY ans F isequalXY ans T Ifthen The basic conditional statements are the following if then else end if then elseif then else end if then must be written on the same line and likewise with elseif then Mention Be cautious with calculation accuracy Calculations are approached and will sometimes give wrong results see Calculation accuracy page 30 Scilab for very beginners 1833 Example Alice throws three dice If she gets three 6s she wins 20 If she gets three identical numbers different from 6 she wins 10 If she gets two identical numbers she wins 5 Otherwise she wins nothing Simulate a trial and calculate Alices winnings using the functions grand see page 22 uniqueDwhich keeps only once the values that appears several times in D lengthuniqueD which returns the size of the obtained vector that is to say 1 if three dice are equal and 2 if two dice are equal Algorithm Scilab Editor Put the three values in D If Alice gets three 6 then Alice wins 20 dollars Else if she receives three identical values then Alice wins 10 dollars Else if she receives two identical values then Alice wins 5 dollars Otherwise Alice wins nothing End if Display Alices winnings Dgrand13uin16 if D666 then W20 elseif lengthuniqueD1 then W10 elseif length uniqueD2 then W5 else W0 end dispAlice wins stringW dollars 2D and 3D plots plot command is used to create plots in the plane Color and appearance can be specified by putting indications of color and points style within quotes Colors b blue by default k black r red g green c cyan m magenta y yellow w white Point styles Joined by default or o x Other options are available with s d v and Scilab for very beginners 1933 Basic plots To plot a point Plot the point A1 2 with a red point Scilab Editor Graphics Window plot12r To plot a segment Plot the segment AB in blue by default with A1 2 and B3 5 Scilab Editor Graphics Window plot1325 Plots of plane curves defined by functions yfx For a function 𝑥 𝑓𝑥 the values of x are specified using the linspace command by writing xlinspaceabn in which a is the smallest value of the variable 𝑥 b the highest value of 𝑥 and n the number of values calculated between a and b Do not forget the sympbol otherwise all n values of 𝑥 will be displayed For example consider two functions 𝑓 and 𝑔 defined over the interval 2 5 by 𝑓 𝑥 𝑥 2𝑥𝑒 and 𝑔 𝑥 sin Scilab for very beginners 2033 With the program below we plot the curve defined by𝑓 in blue by default Scilab Editor Graphics Window function yfx yx22xexpx endfunction xlinspace2550 plotxf By adding the program below we obtain the plot of two curves that of f in red and that of g in green The previous plot was cleared through the clf command clear figure Scilab Editor Graphics Window function ygx ysinx2 endfunction xlinspace2550 clf plotxfrxgg Plots of sequences of points Terms of a sequence The most common case is to plot the sequences of points 𝑀𝑛 𝑢 𝑛 after calculating the coordinates 𝑢𝑛 of a vector 𝑢 plotur specifies the style and color of the points in quotes red and nonconnected stars By default points are plotted in blue and are connected Scilab Editor Graphics Window for n150 un08n end clf plotur Scilab for very beginners 2133 Bivariate statistical data Bivariate statistical data are given in the form of two vectors lets call them X and Y plotXY will plot a scatter plot of 𝑀𝑋 𝑌 with blue triangles Scilab Editor Graphics Window X13377910 Y8755422 clf plotXY Plots in 3 dimensions Scilab can be used to plot surfaces and curves in space with many options for the treatment of hidden faces face colors points of view etc The following examples will illustrate 3D plots The surf function can be used for plotting surfaces This function has three input variables x y and z x and y are respectively vectors of size 𝑚 and 𝑛 corresponding to points on the axes 𝑂𝑥 and 𝑂𝑦 z is a matrix of dimension 𝑛𝑚 with element 𝑧 corresponding to the height of the point with Xcoordinate 𝑥 and Ycoordinate 𝑦 To plot the surface defined by a function of the form 𝑧 𝑓𝑥 𝑦 it is necessary to Define function 𝑓 Calculate zfevalxyf fevalxyf returns the 𝑚𝑛 matrix whose 𝑖𝑗 element is 𝑓𝑥 𝑦 which will be transposed by using the single quote symbol Execute surfxyz To plot the surface 𝑧 2𝑥 𝑦 elliptic paraboloid Scilab Editor Graphics Window function zfxy z2x2y2 endfunction xlinspace11100 ylinspace22200 zfevalxyf clf surfxyz Scilab for very beginners 2233 Curves in space may be plotted using the param3d function param3d has three arguments x y and z each vectors has the same dimension and corresponds to the points 𝑥 𝑦 𝑧 on the curve To plot the helix defined by 𝑥 cos 𝑡 𝑦 sin 𝑡 𝑧 𝑡 Scilab Editor Graphics Window tlinspace04pi100 param3dcostsintt Simulations and statistics Several functions are available in Scilab to perform simulations quickly and efficiently Random sequences grand1puinmn returns a vector of 𝑝 random integer sequences between 𝑚 and 𝑛 with 𝑝 positive integer 𝑚 and 𝑛 integers and 𝑚 𝑛 t grand14uin16 t 3 1 3 6 grand1punfab returns a vector of 𝑝 random real sequences between 𝑎 and 𝑏 with 𝑝 positive integer 𝑎 and 𝑏 real and 𝑎 𝑏 tr grand12unf11 tr 07460264 09377355 Scilab for very beginners 2333 Statistics All the standard statistical functions are listed on page 32 Keep particularly in mind The function barxncolor which draws bar graphs Scilab Editor Graphics Window x110 n8613106416785 clf barxn For a bar graph representing two sets side by side consider the series of values X and the two series of numbers n1 and n2 For plotting n1 and n2 must be column vectors that is why transposes are used in the example below Scilab Editor Graphics Window X125n15105n2687 barXn1n2 The color optional argument defines the color as in the plot function Additional information on matrices and vectors Accessing elements Square braces are used to define matrices A space or a comma is used to switch from one column to another and semicolons are used to switch from one line to another m1 2 34 5 6 m 1 2 3 4 5 6 Parentheses are used to access elements or to modify them Mention This command can also be typed under the form m123456 Scilab for very beginners 2433 m23 ans 6 m2323 m 1 2 3 4 5 23 The operator is used to designate all the rows or all columns of a matrix To view the second row of the matrix m type m2 ans 4 5 23 And the third column m3 ans 3 23 To obtain the transpose of a matrix or a vector use the single quote symbol m ans 1 4 2 5 3 23 Operations The operations are matrix operations To make element wise operations we need to put a dot before the operating sign A123456 A 1 2 3 4 5 6 B112 Scilab for very beginners 2533 B 1 1 2 AB ans 9 21 Matrix multiplication AA error 10 Inconsistent multiplication Dimensions are not consistent AA ans 1 4 9 16 25 36 Element wise multiplication 2A2 ans 6 8 10 12 14 16 The operation is performed on each element because 2 is a number AA ans 1 1518D16 3795D15 1 Gives the matrix X for which XA A The exact result is 1 0 0 1 For reasons of calculation accuracy the result can be slightly different depending on your version of Scilab and your operating system see calculation accuracy page 29 AA ans 1 1 1 1 1 1 Gives the matrix divided element wise In the case of vectors C14 C 1 2 3 4 CC error 10 Inconsistent multiplication Dimensions are not consistent Scilab for very beginners 2633 CC ans 1 4 9 16 It is also possible to write C2 because for vectors exponents are interpreted as an operation element wise This is not the case with matrices 1C ans 00333333 00666667 01 01333333 In this particular case with vectors we find the vector X for which CX 1 1C ans 1 05 03333333 025 Reverse element wise As previously C1 is possible The parentheses around 1 are necessary so that the point is not considered as a comma as part of the number 1 It is also possible to write 1 C with a space between 1 and Solving linear systems To solve the linear system AX Y in which A is a square matrix use the backslash X A Y Be cautious the operation Y A will give only if the dimensions are correct another result that is to say the matrix Z for which Z A Y To solve the system 1 2 3 4 5 6 𝑋 1 1 A1 2 34 5 6 Y11 XAY X 05 0 05 AX ans 1 1 Scilab for very beginners 2733 Some useful functions Sort The gsort function is used to sort the elements of a vector in ascending or descending order v2696402 v 2 6 9 6 4 0 2 gsortvgi ans 4 0 2 2 6 6 9 gsortv ans 9 6 6 2 2 0 4 Length The length function returns the number of coordinates for a vector The size function returns the dimensions rows columns for a matrix U110 U 1 2 3 4 5 6 7 8 9 10 lengthU ans 10 m1 2 34 5 6 sizeU ans 2 3 Scilab for very beginners 2833 Sum and product The sum and prod functions respectively calculate the sum and the product of their argument elements U110 sumU ans 55 prodU ans 3628800 Unique The unique function keeps only once the elements of a vector even if they are repeated several times and sorts them in ascending order v2696402 v 2 6 9 6 4 0 2 uniquev ans 4 0 2 6 9 Find The find function searches for elements in a vector or a matrix and returns a vector containing the corresponding indices To find all the elements of the vector 𝑤 smaller than 5 w153814732126 findw5 ans 1 3 7 8 The resulting vector 1378 indicates that elements 𝑤 𝑤 𝑤 and 𝑤 are smaller than 5 Scilab for very beginners 2933 To find all the elements of the vector 𝑤 equal to 3 w153814732126 findw3 ans 3 7 The resulting vector 37 indicates that elements 𝑤 and 𝑤 are equal to 3 Accuracy computation Computation Numbers have an absolute value between about 2210308 and 1810308 The number eps equals to 2220446049D16 gives the smallest relative precision that can be obtained in computations which therefore gives about 16 decimal digits Example 1 Computation of sinπ sinpi ans 1225D16 The value of 𝑠𝑖𝑛 𝜋 above is not 0 but it is considered as zero Indeed compared to the maximum value of the sine function ie 1 it is equal to 0 with a value less than eps Example 2 Testing if the triangle with sides 𝟑 1 et 2 is a rightangled triangle asqrt3 a 17320508 b1 b 1 c2 c 2 a2b2c2 ans F The program responds false because the value of a2 b 2 is approximate Scilab for very beginners 3033 absa2b2c2eps ans F The program responds false because absolute precision is called for absa2b2c2c2eps ans T The program responds true because relative precision is called for Display Results are displayed by default with 10 characters including the decimal point and the sign The format function is used to display more digits To display 20 digits type format20 Reconsidering 𝑎 3 a2 ans 3 Here there are 7 decimal places and we do not see the error format20 a2 ans 299999999999999956 Here there are 17 decimal places and we can see the error Solving differential equations To find the solutions of an explicit system of differential equations simply reduce to differential equations of order 1 𝑦 𝑡 𝑓 𝑡 𝑦 𝑡 𝑦 𝑡 𝑦 in which 𝑡 and 𝑡 represent time and 𝑦 and 𝑦𝑡 are vectors then apply the ode function yodey0t0tf with y0 initial condition vector of dimension 𝑛 t0 initial time t vector of dimension T corresponding to the times in which the solution is computed This vector must begin with t0 f function defining the system under the form function yprimfty yprim1 yprim2 yprimn endfunction