Labels

Showing posts with label Scilab. Show all posts
Showing posts with label Scilab. Show all posts

Tuesday, August 14, 2012

Scilab Graphic Editor Improvement

Hey guys, I've utilized my little TCL-TK programming experience and done many improvements to the graphic editor of Scilab. The major improvements are:

1. Converting most of the widgets (buttons, check buttons, radio buttons, scroll bars, combo boxes, labels, frames, entries, label frames, notebooks) to themed widgets, which means the graphic editor will obtain the same look and feel of your operating system (windows, linux) or you can apply the themes available by Ttk. I didn't change the scales used for setting colors and sizes, and the object browser tree.

2. The original code contained a lot of unnecessary frames and paned windows which made the heirarchy of the GUI difficult to understand and resource-consuming

3. Removed some code lines that were unimportant

4. Add some features for colormap selection, plot rotation angles, arc entities

5. A lot of useful comments added to the original files to help in future improvement and development

6. Changing the annoying icon of Tk with a Scilab-representive icon

7. Adding all supported fonts in the operating system in GED which makes graph labels and text easily-customizable. These fonts are listed in a combo box with auto complete feature to pick up quickly your favorite font.

8. Interacitve entries, most of the entries can be scrolled by mouse wheel to decrease or increase values inside with the proper increment.

9. Scrollable scales. By just hovering over the scale and scrolling up or down the scale is scrolled.

11. Axis label position was represented by one entry in which you should write a row vector inside. Now, the position of axis lable is represented by two scrollable entries for interactive horizontal and vertical movement.

12. It was not possible to set negative rotation angle for axis label. Now, you can set the rotation angle with negative values.

13. Setting colormap of the figure is made easy buy replacing the old entry used as a command line by a combo box containing list of colormaps and an entry to set number of colors in the colormap.

The following capture shows how the graphic editor looks like after the modifications. I hope that Scilab developers will embed my modifications to Scilab in the next version. If you need the modified files and the installation method, just contact with me via comments.




And the following capture is taken when I tested the new graphic editor after modifications in windows xp (as you can see, the GUI is xp looking).


Sunday, August 12, 2012

Scilab console keyboard shorcuts

The following table shows some keyboard shortcuts used in Scilab as mentioned in Scilab help:


UP or Ctrl+P
Recall previous line in command history list
DOWN or Ctrl+N
Recall next line in command history list
F1
Call Scilab help.
F2
Clear console.
F12
Open console box only on Windows.
Ctrl+Space or TAB
Completion: Scilab displays a list of all names that start with some characters.
Ctrl + A or HOME
Move to beginning of current line. This is useful for long code lines.
Ctrl + B or LEFT
Moves the cursor one character to the left.
Ctrl + C
Interrupts Scilab if nothing selected in the console, else text selected is sent to clipboard.
Ctrl + D or DELETE
Deletes the current character.
Ctrl + E or END
Moves the cursor to the end of command line.
Ctrl + F or RIGHT
Moves the cursor one character to the right.
Ctrl + H or BACKSPACE
Deletes the previous character.
Ctrl + K
Kills command line from cursor to the end.
Ctrl + S
Select all.
Ctrl + U
Delete the whole command line.
Ctrl + V
Do a paste from clipboard.
Ctrl + W
Delete the last word of the command line.
Ctrl + X
Interrupt Scilab
Ctrl + LEFT
Move left one word.
Ctrl + RIGHT
Move right one word.
Shift + HOME
Select from cursor to beginning of statement.
Shift + END
Select from cursor to end of statement.
Double-click
Select current word.

In fact, most of these shortcuts is rarely used, but there is a great shortcut that a lot of Scilabers don't know about Scilab, this shourtcut is "TAB" or "Ctrl+Space" which is used for auto-completion of Scilab Syntax. The list of auto-completion displays all the functions, variables, macros, files, and graphic properties that start with the written characters. When there is no suggestions for the written characters then the shortcut will return nothing, and when there is only one suggestion then Scilab will auto-complete using this suggestion. This shortcut also works in the Scipad. The following caption shows how the auto-completion popup list looks like.

I don't know what was the first version of Scilab introducing this auto-completion popup list, but I tried it in Scilab 5.3.3 (0).

Tuesday, July 31, 2012

Some remarkable difference between matrix and list in Scilab

When comparing Scilab with C++, we will find that matrices in Scilab are equivalent to arrays in C++ and lists in Scilab are equivalent to structures in C++. Lists (data structures) are preferable when creating a database or when a set of data has no specified layout. The following points are some differences between matrices and lists in Scilab:
§  Matrix elements must be all of the same type (.i.e. matrix of strings, sparse matrix, polynomial matrix, real matrix, ... ) , while the list can store different types of elements like numbers, strings, matrices, sparse matrices, polynomial, functions, ... and for this reason lists (typed lists) are implemented in graphic window properties, axes properties, entity properties, and ... etc. in Scilab as it contains mixed types of data, like the following:

Figure property
Type
Type No.
Children Matrix of graphic handles 9
Figure position Real row vector of size  1 x 2 1
Figure size Real row vector of size  1 x 2 1
Axes size Real row vector of size  1 x 2 1
Auto resize Matrix of character strings 10
View port Real row vector of size  1 x 2 1
Figure name Matrix of character strings 10
Figure ID Real matrix 1
Info message Matrix of character strings 10
Color map Matrix of size M x 3, where M is number of colors and 3 is the RGB channels 1
Pixel map Matrix of character strings 10
Pixel drawing mode Matrix of character strings 10
.
.
.
.
.
.
.
.
.

For example, you can use lists to create a data base for employees and this data base contains the following data:
     Employee name (string)
     Employee age (real matrix)
     Employee salary (real matrix)
     Employee is married or not (Boolean)
     Employee notes (empty in case there are no notes)


§  Number of elements in each row in a matrix is the same, while in lists each row (entry) can has different number of elements

§  Matrix can't store an empty row while list can do

Monday, July 23, 2012

"Set theory" operations in Scilab: union, intersection, and difference

In Scilab, there are 4 commands for "set theory":

1. Union operation of two data sets
2. Intersection of two data sets
3. Difference of two data sets
4. Unique elements of data set



Union operation is used in Scilab as the following (as Matlab)



The intersection of two data sets is computed like the following


The difference between two data sets  is computed like the following. You should remeber that difference is not commutative.


Finally, the unique command is used to get the unique elements of a data set with no repetition.

A thing to consider that all the results of set operations are row vectors. The inputs of set operations in Scilab can be matrices or vectors and they are automatically and internally converted to vectors to deal with.

Saturday, July 21, 2012

Function definition in Scilab

So ..., quickly, let's remember what is a function ? A function is a set of commands and operations that are applied to inputs to give us certain outputs. A function is preferred in situations where a block of code is usually used and this -of course- saves programmer's (Scilaber's) time and effort and makes the code compact and small. Another power of functions in Scilab that all the internal variables used in any function are killed (erased) after executing the function.

The following method is used in defining a function



There are many things to note:

Outputs (output variables) should be written in one of the following formats according to the number of outputs


Also, inputs should be written in one of the following formats according to the number of inputs


Wednesday, July 18, 2012

using Scilab code in TCL-TK (tutorial)

The key to use Scilab code in TCL-TK interpreter is a command called ScilabEval. As the name implies, the command is responsible for evaluation of Scilab commands (syntax) inside TCL-TK. The following hints are to be considered when using this command:

1. Syntax

The command should be used in this manner


You can use this command many times in a single line like the following


2. Common mistakes

The Scilab code should start and end with double quotation mark (") not single quotation mark ('), otherwise an error will be returned by Scilab console.


 Also, the Scilab code should all be placed in one line not in multi lines, otherwise the first line only will be executed.



If you want to use strings in the Scilab code use single quotes (') instead of double quotes in order to prevent Scilab from confusing or misunderstanding of start and end quotes.


A simple explanation of this problem is that Scilab detects only the first and second occurrences of the double quotes and executes what is between them.



I think these are the common mistakes and problems that may make you pull out your hair if you don't know them.


Scilab and TCL-TK

TCL-Tk, TCL is an abbreviation for Tool Command Language and TK is an abbreviation for toolkit. In Matlab, graphical user interfaces GUIs are based on Java language (as I remember), while in Scilab, TCL-TK which is a powerful scripting language is used in Scilab. I admit that TCL-TK in Scilab is not well-documented, and this come from the fact that TCL-TK already has a wealthy documentation in its community site http://www.tcl.tk/ with a lot of examples and sample scripts. Another thing to know that Scilab has no GUI or toolbox to create GUIs using TCL-TK (like Matlab).

From my point of view, TCL-TK is a great scripting language and may be the easiest one to use for creating GUIs, it includes all types of GUI elements (widgets) like buttons, radio buttons, check boxes, scroll bars, scales, canvas (for drawing), labels, entries, combo boxes, and a lot more. It also supports string and math operations and functions. The following capture is just to show the power of TCL-TK GUI (I created this simple GUI using my GUI builder project which is not suitable for proffesional use. By a little help of you we can improve this project).




So, what's new with Scilab ? . The greate thing in Scilab that you can create GUIs in Scilab environment using TCL-TK and exchange data and variables between Scilab and TCL-TK so that you can make Scilab make calculations and plots by clicking a button or scrolling a scale and so on.

The aim of this post is not to show how to use TCL-TK in Scilab, but is to show you that there is a buried treasure inside Scilab. Really, Scilab is very precious.

Saturday, July 14, 2012

Solving Colebrook equation in Scilab

Colebrook equation is an implicit equation used to calculate the Darcy-Weisbach friction factor which is used to calculate the pressure drop in pipes.

So, one of the methods to solve this equation is Newton-Raphson method. Colebrook equation can be re-written in residual form like the following

The following Scilab function is used to calculate Darcy-Weisbach friction factor for a fully-developed turbulent flow.

function [f, #of_iterations]=Colebrook_f(epslon, d, Re, NMax) 

R='1/sqrt(f)+2*log10((epslon/d)/3.7+2.51/(Re*sqrt(f)))'; // Residual expression 

D='-0.5*f**(-1.5)*(1+(5.02/(log(10)*Re))/((epslon/d)/3.7+2.51/(Re*sqrt(f))))'; // derivative expression: partial ... derivative of residual with respect to friction factor
 
 
f=0.02; // Initial guess of Darcy-Weisbach friction factor 

for i=1:NMax, // loop stops after reaching stop criteria 

RV=eval(R); // Residual value evaluation 
DV=eval(D); // Derivative value evaluation 
f_new=f-RV/DV; // computing the new value of f using Newton-Raphson method

if RV <=0.0001 then // If residual is less than the accepted error 
break 
else f=f_new; 
end 

end 

#of_iterations=i; 

endfunction

The following capture shows the result obtained by the function used in Scilab

and the following capture is the result obtained by the online calculator in this link http://www.engineeringtoolbox.com/colebrook-equation-d_1031.html

Saturday, April 21, 2012

Cont. Basic matrix commands and operations in Scilab

7. Matrix inverse
Basically, in Scilab there are two methods to compute the inverse matrix. The first one is using the command "inv(mat)" and the other method is raising the matrix to power equals (-1). Indeed, we all know that the inverse is computed for a square matrix. So.. consider the following square matrix of size (2 x 2)

the first method is like the following

and the second is 
and both methods will return the same result as shown in below

8. Matrix transpose
Matrix transpose is simply rotating each row 90 degrees in clockwise direction about row pivot in order to be column. The transpose matrix is computed simply by adding the quotation mark (') after the matrix like shown below
Note: this note actually is for people who write codes in word documents. If you are writing Scilab codes in MS Word (or even Word Pad) using any font, you should note that the quotation mark in word file differs from the quotation mark typed in Scilab. The latter will return the matrix transpose, but the quotation mark copied from word document will return an error message "Invalid factor". The following figure shows different quotation marks for different fonts.

The quotation mark used in word documents has ASCII number [226    128   152] while that used in Scilab has ASCII number 39.

Computing the transpose matrix for the matrix used in section 7 will give the following answer
9. Matrix multiplication
We can easily multiply two matrices in Scilab (or Matlab) using the asterisk (*). When multiplying two matrices, number of columns of the first matrix must equal number of rows of the second matrix. Consider the following two matrices
to multiply these two matrices we will use the following command

The answer of matrix multiplication is


10 . Element wise matrix operations
Conceptually, in matrix addition and subtraction we add/subtract each element in the first matrix with/from the corresponding element in the second matrix. So, what should we do if we want to multiply/divide each element in the first matrix by the corresponding one in the second matrix?. The answer is element-wise matrix multiplication or division. All what you have to do is adding dot (.) before the multiplication mark (*) or the division mark (/). See the command below for element-wise matrix multiplication

Consider the following two matrices.


element-wise matrix multiplication will return the following answer
Notes:
  •  Element wise matrix multiplication (and the same for division) is commutative which means
  • Element-wise operations require two matrices of the same size