What is Scilab
Quick guide for Scilab
Important math functions
Draw a function
Define your own functions
Addendum control engineering: state space methods
After starting Scilab, the command window appears (see picture). After the arrow-shaped prompt -> you can now enter Scilab commands. With the Cursor keys up / down by the way, you can scroll through commands that have already been entered. With the Cursor keys left / right you can step through the entered command line and change the text entered (to delete characters, you can use both the DEL as well as the BACKSPACEUse the button in the usual way). The entry can be canceled with the ESC key. An entered command is activated after pressing the key ENTER executed. To continue a command on a new line, end the current line with ... (three dots) and press ENTER. Enter the rest of the command on the new line and press ENTER to execute the command.
You can get help in the program under the menu entry Help-> Help Dialog. If you know a command name and want more detailed information, you can too help followed by the command name you know, type in. Under the menu item Demos you can look at some examples.
Basic arithmetic operations (content)
+, -, *, ^ are the commands for addition, subtraction, product and power.
- Evaluation sequence: 1+2*3-3^4*(2+5), Scilab calculates this term, for example, in the mathematically correct order from left to right, taking into account the priorities of powers (^), products (*), expressions in brackets etc. and reports: ans = -560
- Standard output variable: ans, is the standard variable in which Scilab saves all results if you do not specify a variable.
- Assign variable: If you have the result e.g. in the variable Result want to save, you can specify them explicitly by using an equal sign (=):
Result = 1 + 2 * 3-3 ^ 4 * (2 + 5)
Scilab then reports:
Result = -560.
You can now continue to use the Result variable for further calculations (e.g. 2 * result-50).
- Suppress output: If you do not want Scilab to report anything back to you, end the command with a semicolon:
Result = 1 + 2 * 3-3 ^ 4 * (2 + 5);
Scilab will then assign the result to the variable Result without informing you.
Predefined constants (content)
Scilab provides you with a number of predefined constants. All predefined constants begin with a percent sign.
- % e, natural number e = 2.7182818 ...
- %pi, Circle number Pi = 3.1415927 ...
- % i, Imaginary number i ^ 2 = -1
- % inf, Infinity
- % nan, not a number, e.g. 1/0
- % T, True, truth value in Boolean logic
- % F, False, truth value in Boolean logic
Important mathematical functions (content)
The argument of the function is always passed to the function within a bracket that immediately follows the function name. The argument "x" used in the following examples stands for any number values, variables you have previously defined, or any type of expression or Scilab command that can be evaluated to a number value. For example, you can also pass a function as an argument of a function.
- sqrt (x), computes the square root of x. Alternatively: x ^ 0.5
- exp (x), computes the x. Power of the natural number e. Alternatively: % e ^ x
- sin (x), asin (x), sinh (x) calculates the sine of x (radians), or the inversion of the sine, or the hyperbolic sine
- cos (x), acos (x), cosh (x), Cosine, inverse cosine, hyperbolic cosine
- tan (x), atan (x), tanh (x), Tangent (alternatively: sin (x) / cos (x)), inverse tangent, hyperbolic tangent.
- log (x), log10 (x) natural logarithm (base e), logarithm to base 10
Matrix operations (content)
- Definition of a matrix: A = [1 2 3; 4 5 6; 10 11 12], defines a matrix with 3 rows and 3 columns that is filled with the specified values. Matrices are defined within square brackets. Line values are separated from one another with spaces or commas. A new line is started with a semicolon. In this example, Scilab reports:
1. 2. 3.
4. 5. 6.
10. 11. 12.
- inv (A), computes the inverse of matrix A (A must be square).
- A + B, (element-wise) addition of two matrices (matrices A and B must have the same dimension), or addition of a scalar with a matrix, e.g. 5 + A (5 is added to each element of the matrix).
- FROM, see above, but subtraction.
- A ', Transpose of matrix A (rows of the matrix become columns and vice versa). If A is a row vector, it becomes a column vector and vice versa.
- FROM, Matrix product (number of columns in matrix A must be equal to the number of rows in matrix B), or multiplication of a scalar by a matrix, e.g. 5 * A (each element of A is multiplied by 5)
- FROM, element-wise multiplication of two matrices (must have the same dimension).
- FROM , is short for A * inv (B), since there is no matrix division. If A or B is a scalar, each element of the matrix is divided by the scalar.
- FROM, element-wise division of two matrices (must have the same dimension).
- A ^ n, Abbreviation for multiple matrix multiplication of A by itself (A must be square). For example is A ^ 3 identical to the command A * A * A.
- A. ^ n, exponentiation of a matrix element by element (A can have any number of rows and columns). For example results [1 2 3].^2 the vector [1 4 9]
- det (A), calculates the determinant of A.
- Colon operator: You can use the colon to build larger vectors. The command x = [0: 0.1: 100] For example, creates a line vector that contains the numerical values 0, 0.1, 0.2 ... 100. As you can see, the start value, step size and end value are specified - separated by a colon. If you omit the step size, Scilab assumes a step size of 1. Example: x = [0: 5] gives the vector x = [0 1 2 3 4 5]
- zeros (x, y) creates a zero-filled matrix with x rows and y columns.
- ones (x, y), creates a matrix filled with ones with x rows and y columns
- eye (x, y), creates a matrix with x rows and y columns, whose main diagonal elements are 1 and the remaining elements are 0. If x = y then there is a square unit matrix I.
- You receive individual values of a vector x with the command x (i), where i = 1 is the first value in the vector. You get the index of the last value with the command size (x). You can also display areas of the vector by using the Colon operator (:). With x (5:10) For example, you get a vector that contains the values of the vector x at positions 5 to 10.
- You get individual values of a matrix A with A (i, j)where i is the row number and j is the column number of the element. Here, too, you can display areas (sub-matrices). A (1: 3.6: 9) yields e.g. a matrix that contains the elements of the first 3 rows and columns 6 to 9 of A. If you want to extract a complete, e.g. the second, column from A, you can use the following notation: A (:, 2). Inquire about the size of the matrix A with size (A). You get a vector, the first element of which contains the number of rows and the second element the number of columns of A.
Draw a function (content)
- plot2d (x, F), opens a graphics window and plots the values from vector F over the values from vector x. For example, to draw a square parabola for numerical values from 0 to 100, enter:
x = [0: 0.01: 100]; F = x. ^ 2; plot2d (x, F)
The plot command has many parameters, e.g. to determine the line type, color, axis scaling, etc., there is also the plot3d variant for 3D plots. For further details it is recommended to use help plot2d call the online help for this command.
- clf, clears the last plot. If you execute the plot command again without first deleting, the new curve will be added to the existing plot.
- xgrid (), turns on grid lines in the plot.
- replot ([xmin, ymin, xmax, ymax]), redraws a plot and sets the axes to the maximum and minimum values xmin, ymin, xmax, ymax you want.
- square (), makes the graphics window square.
You can work with polynomials at Scilab. Polynomials must first start with the command poly can be defined, after which it is very easy to define transfer functions, e.g. for control engineering calculations. Polynomials can even be used in matrices, which is very useful for state space methods.
- P1 = poly ([1 2 3 4], 's'), This command creates a polynomial with the zeros 1, 2, 3 and 4 and defines the letter s as the variable of the polynomial. The polynomial is obtained: 24-50s + 25s2-10s3+ s4.
- P2 = poly ([1 2 3 4], 's', 'coeff'), generates a polynomial with the coefficients 1, 2, 3 and 4. One gets: 1 + 2s + 3s2+ 4s3
- s = poly (0, 's'), defines the variable s as a polynomial variable. Once this has been determined, you can simply type in the polynomial (also fractions), e.g .: P3 = (s + 1) / (s ^ 2 + 3 * s + 4)
- roots (P), calculates the zeros of the polynomial P.
- P1 * P2, P1 / P2, P1 + P2, P1-P2, Polynomials can of course be multiplied, divided, added and subtracted. E.g. P1 * P2 with the above example values for P1 and P2: 24-2s + 7s2+ 6s3-114s4+ 112s5-37s6+ 4s7
- horner (P, x), calculates the numerical value of the polynomial P for a numerical value x, which is used instead of the polynomial variable (in the previous examples 's') (x can also be a vector, then returns horns a vector as a result). E.g .: horner (P2, [1 2]) results [10 49].
Control engineering (content)
Scilab knows a few commands for analyzing dynamic systems. This section is limited to systems with one input and one output (SISO). For systems with several inputs and outputs, the state space representation, which is also supported by Scilab, is recommended. For more information, see the Supplement Control Engineering: State Space Methods.
- G = syslin ('c', PZ, PN), or G = syslin ('c', PZN), define a (continuous) linear system with the numerator polynomial PZ and the denominator polynomial PN, or the fraction of polynomials PZN. The polynomials must be defined beforehand as explained in the previous section. A P-T1 system with T1 = 1 could be defined as follows:
s = poly (0, 's'); P1 = 1; P2 = s + 1; G = syslin ('c', P1, P2), or:
s = poly (0, 's'); P = 1 / (s + 1); G = syslin ('c', P)
The polynomial can also be defined within the syslin command: s = poly (0, 's'); G = syslin ('c', 1 / (s + 1))
The definition s = poly (0, 's') only has to be done once during a Scilab session. Instead of the polynomials, the system matrices can also be specified; you can find out more with the command help syslin
- nyquist (G, freqmin, freqmax), draws the frequency response locus for frequencies from freqmin to freqmax for the system G previously defined with syslin.
- evans (G, Kmax), draws the root locus of the system G previously defined with syslin for controller gains from 0 to Kmax.
- y = csim ('step', t, G), calculates the step response of the system G previously defined with syslin for the times specified in the vector t (e.g. with t = [0: 0.01: 50] define beforehand) and save it in y. The result can then e.g. with plot2d (t, y) being represented. Instead of 'step' can also 'impulses' can be specified to obtain the impulse response. Other functions or specification of support points of any input signal is also possible. For a sinusoidal input signal, you can y = csim (sin (t), t, G) enter.
- bode (G, freqmin, freqmax), draws the Bode diagram of the system G previously defined with syslin for a frequency range from freqmin to freqmax. Notice that the command creates a Bode diagram with the x-axis representing the frequency in Hertz and not the angular frequency. So if you want to read off corner frequencies and convert them into parameters of the system, you must first convert the frequencies into the angular frequency with the relationship omega = 2 * Pi * f! Alternatively, you can also go the following way:
- [frq, repf] = repfreq (G, freqmin, freqmax), calculates the frequency response of system G for the frequency range from freqmin to freqmax. The result of the function is the vector repf, which contains the frequency response in the form of complex numbers, and the vector frq with the associated frequencies.
- [phi, amp] = phasemag (repf), With this command you now get from the vector repf a vector amp which contains the amplitude in db and a vector phi which contains the phase shift in degrees.
- plot2d ('ln', frq * 2 *% pi, amp), now plots the amplitude response in the usual form over the angular frequency (the frequency values frq are simply multiplied by 2 * Pi in the representation). The parameter 'ln' indicates that the x-axis is to be scaled logarithmically ('l') and the y-axis is to be scaled normally ('n').
- plot2d ('ln', frq * 2 *% pi, phi), draws the phase response in the usual form over the angular frequency.
Don't forget to include the amplitude response clf before you draw the phase response. You will receive grid lines with the order xgrid ()
- xcos (), starts the block diagram editor and enables graphic input (block diagram) and simulation of dynamic systems (continuous and discrete).
Define your own function (content)
If, for example, the procedure described above for displaying the Bode diagram is too complex for you, you have the option of writing a function that takes on this task. Own functions must be defined with the command function started and with the command end function be terminated. Right after the command function the sizes that the function returns, the function name and the parameters that are transferred to the function when it is called are specified. It looks like this, for example: [a, b, c] = myfunction (x, y). The function is called myfunction, takes the parameters x and y and returns the content of the variables a, b and c after all calculations have been completed.
- Short functions can be entered directly like a command at the prompt in the command window:
function y = sum (x1, x2), y = x1 + x2, endfunction, by entering this line you get a (admittedly not very useful) function that adds two numbers. You can now, for example, add 2 and 3 and store them in the variable z, the command z = sum (2,3) enter.
- For more extensive functions, it is recommended to use them in the supplied editor SciNotes (open with the first button at the top left of the SciLab console) and save it (text file with the extension .sci). You can load such files into the working environment with the help of the file menu and are then available for execution.
In order to get the Bode diagram over the angular velocity comfortably with one command, enter e.g. the following lines in SciNotes (this function is contained in the example file):
function bodeomega (G, freqmin, freqmax, plot_was)
[frq, repf] = repfreq (G, freqmin, freqmax);
[phi, amp] = phasemag (repf);
if plot_was == 'p' then plot2d ('ln', frq * 2 *% pi, phi)
plot2d ('ln', frq * 2 *% pi, amp)
Save this file under the name bodeomega.sci. After you have this file with getf ('path\bodeomega.sci ') you can log in with the command bodeomega (G, fmin, fmax, 'a') the amplitude response and with bodeomega (G, fmin, fmax, 'p') display the phase response of system G in the frequency range fmin to fmax.
As you may have noticed with the bodeomega function above, you can use the if / else-Carry out case distinctions command. Scilab also masters other commands for controlling the program flow, e.g. the for loop. The syntax of these commands is now illustrated using examples:
- if a> 0 then y = 1, elseif a <0 then y = -1, else y = 0, end, is an example of a case distinction with an if / else structure, it corresponds to the Signum function sign (a). First of all, immediately after the if-Keyword, a comparison is made. If this comparison is true, the command after the thenKeyword executed, i.e. the value 1 is assigned to the variable y and the processing of the if / else structure is ended at this point. If the comparison is wrong, the comparison is behind the elseifKeyword checked, if this is true, the value -1 is assigned to y and the structure is terminated. Any number of other comparisons can be added here with the help of other elseif keywords. If the if-condition and all elseif-conditions are false, the command after the else-Key word executed. If there is no else, no command would be executed in this case. The commands do not have to be assignments as in this example, but any type of Scilab command is permitted. Please do not forget that every if / else structure must end with a end-Keyword to be terminated!
The following comparison operations are still permitted:
- Comparison for equality: a == b
- Greater than or equal to: a> = b
- Smaller or equal: a <= b
- Unequal: a ~ = b
As well as the logical links:
- Logical AND: &
- Logical OR: | (Vertical line, pipe)
- Logical NOT: ~ (Tilde)
- for i = 1: 2: 5, x (i) = i ^ 2, end, with a for loop, the area between the keywords for and end repeated several times, increasing a run variable by a certain value with each run until it has reached a specified maximum value. In this example, the variable i runs through the values 1, 3 and 5 and saves the values 1, 9 and 25 in a vector x at positions 1, 3 and 5, respectively Way, separated by a colon. Again, a must end-Keyword to complete the structure.
Data management (content)
- who, shows you the names of all variables that are currently defined.
- clear x, deletes the variable with the name x from memory.
- save ('test.dat', x), saves the content of the variable x in a binary file called test.dat in the current directory on the hard disk.
- save ('filename.dat'), saves the content all Variables that are currently defined are stored in a binary file called filename.dat in the current directory on the hard disk.
- load ('test.dat'), reloads the variables saved in the test.dat file into the working environment, and the variable names are also restored.
- chdir ('path'), Directory change, path becomes the new current directory.
- chdir ('..'), Directory change, the directory one level higher becomes the current directory
- pwd, shows the current directory.
Addendum control engineering: state space representation (content)
As already indicated in the section on control technology, you can also work with the methods of the state space at Scilab.
- Enter a system in state notation: A = [1 2; -3 -4], B = [1 0; 2 5], C = eye (2.2) (square identity matrix I with dimension 2), D = zeros (2.2) (square matrix with zeros, no penetration).
- The characteristic equation is now obtained with CGl = det (s * eye (2,2) -A) (results in: s2+ 3s + 2). Before doing this, you have to use the variable s s = poly (0, 's') as a polynomial variable. With roots (CGl) you now simply get the poles of the system (in this case -1 and -2, so the system is stable).
- You also calculate the matrix of the transfer functions (which links the outputs with the inputs of the system) G = C * inv (s * eye (2,2) -A) * B + D. You receive:
8 + s 10
2 + 3s + s 2 + 3s + s
- 5 + 2s - 5 + 5s
2 + 3s + s 2 + 3s + s
- You can now access each of these 4 transfer functions with G (1,1), G (1,2), G (2,1) and G (2,2) and use them for further investigations. E.g. you can determine the step response of G (1,1) with the following 3 steps:
- Define transfer function: G11 = syslin ('c', G (1,1))
- Define time vector: t = [0: 0.01: 10]
- Calculate and display the step response: y = csim ('step', t, G11); plot2d (t, y)
However, this example only supplies the portion of the first output variable (y1) caused by the first input variable (u1) (or it is assumed that u2 would be equal to zero). If you assume, for example, that both input quantities are identical jumps, you can calculate the complete outputs of the system as follows:
- Output variable 1: G11 = syslin ('c', G (1,1)); G12 = syslin ('c', G (1,2)); y1 = csim ('step', t, G11 + G12).
- Output variable 2: G21 = syslin ('c', G (2,1)); G22 = syslin ('c', G (2.2)); y2 = csim ('step', t, G21 + G22).
However, if the input variables have different courses, then it is advisable to proceed as follows:
- Calculate the transfer function directly from the matrices A, B, C and D with the command: GS = syslin ('c', A, B, C, D)
- Define a function, e.g. ut (t), which describes the values of the input variables as a function of the time vector t and returns them as a column vector. An example of this is shown below.
- Simulate the system and represent it: y = csim (ut, t, GS); plot2d (t, y '). Note that ut here is the function that was defined in the previous step. For y, csim results in a two-column matrix for this example, since the system has 2 outputs. The transpose y 'must be used for representation using plot2d, since t was defined as a line vector. plot2d shows the curves of y1 and y2 in different colors in the same window.
How do you define the function ut. Let us assume that at the beginning only the first input variable (u1) should jump from 0 to 1, u2 only jumps to 2 at time t = 10, then the function definition looks like this:
function u = ut (t), if t <10 then u = [1; 0], else u = [1; 2]; end; endfunction
If t is less than 10, the vector [1; 0], otherwise the vector [1; 2] is returned. csim expects the values of the input variables in a column vector, the number of rows of which corresponds to the number of input variables. When defining ut, you are of course not fixed in leaps and bounds, you can implement any function, e.g. a sine and a cosine function with decaying amplitude:
function u = ut (t), u (1,1) = 1 / (t + 1) * sin (t); u (2,1) = 1 / (t + 1) * cos (t); endfunction
- What is required for a strong relationship
- The interference law applies to programming
- Pandora polls are global
- Why is PPC sloping down
- How are tin cans recycled
- What is the exact concept of Bitcoin
- How do people recognize potential in sport
- Yahweh tempts people
- Presumptuous Why do people act like that
- What is OMR duplex printing
- How did table tennis come about
- Which artist sang the song Ms. Jackson
- How is Pinterest useful for SEO?
- Is macaroni and cheese pasta
- Is a logo design considered an art?
- What is numericality reduction
- What does the German word Geil mean?
- What color is a main sequence star
- How do I fix a locked computer
- What is protected agriculture
- Should I get technical in my SOP
- Is China happy with Russia?
- The medical field is growing or shrinking
- How do you publish an article yourself