Open the Scilab package: This opens the Scilab command window,. You can
type or paste directly into the command window: eg 2+3 and enter, gives 5.
Lab100 Week 10: First Steps in Scilab Scilab is a computer package specialised for matrices. You will quickly become familiar with the style of working used in Scilab. You can download Scilab: see the download page under maths.lancs.ac.uk. Click on DeptInfo/ InternalInfo/ Computing/ Software () We begin with a gentle introduction to the Scilab commands for standard arithmetic operations. By the end of this session you should be able to: − run some simple Scilab code, − check that Scilab operates as a calculator, − see how sequences and arrays are handled, − see how functions and variables are handled, − note that Scilab deals with complex numbers. Scilab commands // ; sin cos tan sqrt exp real imag conj abs disp Getting Started with Scilab Login in to the Departmental Windows Server. Open the Scilab package: This opens the Scilab command window, You can type or paste directly into the command window: eg 2+3 and enter, gives 5. In general we use the command window to interrogate, print etc. Using the Scilab editor to run your code In the top tool bar click on Applications then click on Editor; this opens an editor window. In the untitled editor window that appears enter: 4+5 click on File/SaveAs, choose your Lab100 folder, and give the file a name: eg labwk11, then click Save, From the Execute menu choose Load into Scilab, or use the shortcut Ctrl+l. The answer, 9, appears in the Command window. Type 6+7 in your editor. Select [highlight] the line. Now try using the shortcut Ctrl+l. The answer, 13, appears in the command window. Notes: Top Editor menus have the usual New, Open, Save. In the console the Enter key executes the command. In the console previous commands can be retrieved using UpArrow. [An alternative working style is to use a different editor (e.g. notepad) to create the commands and then to paste these into the command window.]
1
Quit: Save the final version of your session. Click on File/Exit from the Scilab main menu (on the command window). Pasting: Pasting is more efficient than direct typing. Pasting saves re-typing the input and avoids errors. The commands are highlighted in the worksheet pdf file. The pdf file of these notes is available on the Lab100 web page. Open in Firefox and click on the file. Use the Select tool to highlight, followed by Ctrl-C to copy the code. Use Ctrl-V to paste the code into the Scilab command window.
Scilab symbols and punctuation. >> is the Scilab prompt. Inf means Infinity. NaN means Not a Number. %pi in Scilab is pi. The semi colon ‘;’ suppresses display. ‘//’ is a comment in Scilab, and tells Scilab to ignore command Q 10.1 Scilab operates as a calculator. Find the answers to
Make a note of your answers.
3+5 // this is a comment 3-5 3-5 ; // this suppresses display 3/5 3*5 3/5 , 3*5 // the , separates the two commands 3+(4*5) , 3+4*5 (3+4)*5 2^5 2^34 // note the D instead of the usual E // read as ‘times ten to the’ 3/0 %pi // % is used before a pre-defined variable %i 4-2*%i Try some of your own sums until you feel comfortable with the Scilab commands. 2
Q 10.2 Sequences and arrays. Note the effect of entering the commands (hitting the return key where necessary). [ 1 2 3 4 ] [ 1 ; 2 ; 3 ; 4 ] [ 1 2 ; 3 4 ] 1:4 1:2:9 sum(1:3) sum(1:10) x = 1:3 sum(x) x = 1:3 ; sum(x) x = 5; x // x = 4 x x^2 Q 10.3 Scilab has built in functions and variables. Before you PASTE in these commands, write down the values a mathematician should expect. %pi sin(%pi/2) cos(%pi/2) tan(%pi/4) sqrt(4) 4^(1/2) sin( sqrt(4)* %pi ) %e = exp(1) %e y = 2 x = exp(0) z = x+y To see which variables are held in Scilab try A = 2 a = 3 whos Q 10.4 Error messages. These are often a bit confusing. Here is an example. Type in (3+4 3
You get the error message: !–error 3 waiting for right parenthesis. Create another example. Q 10.5 Complex numbers. Scilab supports complex arithmetic. We use display to compress the output. z = 2 + 2*%i w = 1 - %i z+w z-w disp([ z+w, z-w ]) disp([ z*w , z/w ]) disp([ real(z), imag(z), conj(z), abs(z)]) Q 10.6 An exercise. Find the numerical value of y when x = 4 and y = 3 exp{sin(2πx)} + 2. Find the numerical value of z = exp{2πi}. You will have noticed there are several differences to Maxima. The assign in Maxima is ’:’. In Scilab it is ’=’. Scilab is essentially numeric while Maxima is symbolic.
ASSESSED WORK by Sunday: select quizwk10 from your MyModules page.
Remember to EXIT from Scilab before logging out.
4