Utility Software II

lab 5

Jacek Wiślicki, jacenty@kis.p.lodz.pl
source: http://wwwmaths.anu.edu.au/comptlsci/

Scilab is an engineering software enabling advanced calculations (e.g. solving equations, matrix operations), signal analysis and processing, process simulations, etc. (range of applications is similar to the commercial Matlab package). It is distributed under free license (available at http://scilabsoft.inria.fr/legal/license.html) and its binaries (such as installers) and the source codes for different operating systems and platforms can be downloaded fromhttp://scilabsoft.inria.fr/download/index_download.php?page=release.html. The Scilab tutorial is available at http://www.phy.hw.ac.uk/~peckham/programming/scilab/intro-html.html, which will be very helpful as an extension of Utility Software II course.

The Scilab command window acts as a console and enables entering basic commands, eg. pwd would display the current working directory, where the Scilab files meant for edition or execution will be searched.

Changing the current directory is available by chdir command. The command window enables for example defining a variable, e.g.
A = [1 2 3; 4 5 6; 7 8 9] defines a matrix A in a form:

1 2 3 A= 4 5 6 7 8 9 Accept the matrix by pressing Enter. Then type A and press Enter – the matrix is displayed:
-->A A =

[ ]
1. 4. 7. 2. 5. 8.

Enter the matrix using the editor window (SciPad) and save it to a file with “.sce” extension, e.g. test_01.sce (by default in the current working directory). Check if the file is in your working directory and check it contents (it is a plain text file). Now you can use (e.g. execute) the file by the following command: which again displays the matrix. Notice: the Scilab console offers up/down arrow command repeating.
exec test_01.sce

3. 6. 9.

Basic commands
Enter in the console window a vector: x =[ 0 2 5 ] and a matrix:
page 1 of 5

Switching to the full output can be obtained by: and back to short: format(20. plot2d(w.1:7 Now we have two vectors w and z of the same length. Note that a semicolon at the end of the line would be helpful here.Utility Software II lab 5 Jacek Wiślicki.edu. the result is stored in y variable. jacenty@kis.g.au/comptlsci/ 1 3 4 A= − 1 2 5 4 −3 5 [ ] the exact function name. however the output is present in the short format. e. try help command. However. Try multiplying the vector x and the matrix A: and A*x A*A All the calculations are performed in the double precision format. rank(). Try: A should be produced. y y = A*x. z) plot of w versus z page 2 of 5 .anu.’v’) Placing a semicolon at the end of line will stop Scilab output after execution of the command (useful in case of large vectors or matrices).1 until 7 is reached. Try: This will produce a vector starting at 0 and increasing by 0.pl source: http://wwwmaths.: help spec or apropos when you are not sure about apropos random will display help on the rand function. You can save the graph to a file by using the File → Export menu item on the graphics window. We can plot the values of w versus the values of z.’e’) format(10.p. e. We can take functions of vectors (or matrices).g. For instance try: z = sin(w) w = 0:0.g.lodz. All the built-in functions are named with lower-case letters. Notice: Scilab is case-sensitive. spec(). Try: which displays nothing on the screen. e. If you are not sure about the function name. Try executing: The operation's result is presented on the Scilab output. Plotting Let’s make some larger vectors.: Notice: in the matrix notation semicolons can be replaced by EOL characters. It is easy to produce vectors that have components which increment nicely.

n = n + 1. You will be prompted for a file name. Consider the example program for calculating ex. n = 1.lodz. Go back to the Scilab window and test your program “ex”.0. Use the inv command to solve the matrix equation Ay = x. term = 1. while(y + term ~= y) y = y + term. Redefine A to be a random 5 × 5 matrix and x a random 5 × 1 vector. Loops and conditionals Scilab provides for. end ans Here ~= means “not equal to”. term = term*x/n. For instance try to let Scilab run your program with x = 1.anu. term = 1. Return to the editing window and change the program as follows: function y = ex(x) //EX A simple function to calculate exp(x) y = 0. In fact under-determined systems can also be accommodated. We can avoid retyping by saving the program in a file.sci” is a standard extension for Scilab function files. n = 1. m) produces a random n × m matrix. Use the name ex. the “. Save to a file using the file menu.sci’).0 exec(’ex. Try: y = A \ x The vector y should solve the linear equation x = A*y (check this). ans = 0.sci.pl source: http://wwwmaths. We can make our program into a function. Open up a text editor.edu. Type in the program. term = term*x/n. end endfunction x = 1. while and if statements to control flow within a program. The Scilab command rand(n. while(ans + term ~= ans) ans = ans + term.au/comptlsci/ Linear algebra We can also solve matrix problems.sci’) page 3 of 5 . The inverse of a matrix can also be calculated using the inv command. Type in a value for x and then issue the command exec(’ex. n = n + 1. The \ command is more general and can be used to solve over-determined systems (systems with more equations than unknowns) by finding a “least squares solution”.p. jacenty@kis.Utility Software II lab 5 Jacek Wiślicki.

The function should display a page 4 of 5 . 2) function [root1.Utility Software II lab 5 Jacek Wiślicki. Scilab has a for statement of the form for i = v //Scilab statements end Here i is a dummy variable to be used in the loop. that the dummy variable will iterate through. and v is a vector. root2] = quadroots(a. Use your function ex and the if statement to produce a new function newex which produces reasonable approximations of ex for x positive or negative.0) ex(x) if expression then //Scilab statements else //Scilab statements end By the way. jacenty@kis. Try This algorithm is useless for large negative values of x.p. and pay particular attention to the last example. checking the results in each case. Exercises (10 points) 1. First we need to become familiar with the if statement. but you will have to use exec again to load in the new function newex.sci’) ex(1. a) x2 + 1 = 0 b) 2x + 1 = 0 c) x2 + 3x + 2 = 0 d) 4x2 + 24x + 36 = 0 e) 1018x2 − x − 1 = 0 2.pl source: http://wwwmaths. since Scilab can compute and print complex values. Save your changes. Write a Scilab program quadroots to compute and print the roots of a quadratic equation 2 ax  bx c=0 using the quadratic formula: −b ± b2− 4ac x= 2a It should run with a command like for x2 + 3x + 2 = 0. This program can be made to return both roots in a vector by calling them. 3. Write a function fact calculating a factorial of a natural number. 3. You can use the same file to define both the ex and the newex functions. printing an appropriate message in that case. Test quadroots on the following examples. c) These can then be stored into two variables such as r1 and r2 by running the function with the command [r1 r2] = quadroots(1. The program should avoid division by zero. b. for example. So: for j=-4:2:6 disp(j**2) end will print out the squares of the even numbers from -4 to 6. The Scilab if statement has the simple form exec(’ex. usually a range of numbers defined 1:20. 2).lodz.au/comptlsci/ The lines starting with // are comment lines.edu.anu. root1 and root2 in the function file and starting the function definition with the line quadroots(1. Make your own decision about what to do in the case of complex roots. Now you can use the command But you must load-in your changes.

au/comptlsci/ message if its argument is incorrect.p.5cos(0. step) plotting the values of the function f(x) for x's in the range from x_min to x_max with a step equal step. Write a function trigo realizing a trigonometric function: f(x) = 3sin(2x) – 0.lodz. 0. 3. 1) page 5 of 5 .anu. 10. For example. calling the function  should display the following plot: trigoplot(-10.1) while would result in: trigoplot(-10.Utility Software II lab 5 Jacek Wiślicki. jacenty@kis.5x) Use this function in another function trigoplot(x_min. x_max. Write a function calculating Newton's binomial: n! n = k !  n −k  ! k using fact function from the previous exercise.pl source: http://wwwmaths.edu. 4. 10.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.