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 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

Saturday, July 28, 2012

Where to find magnets in our daily life

In fact this post should not be a post since it is not that important, but it will. In some developing countries like my lovely country (Egypt) there are no sellers or manufacturer for magnets or there are some but difficult to find or reach. Many times I needed magnets to do some experiments or models, but I didn't find someone who sells magnets. Therefore, I decided to use magnets found in some available products around us. The following list is some of the products that you may extract magnets from them if they are scrapped:

§  Speakers of computer, telephone, cell phone, TV boxex, and headphones

§  Fridge door seal

§  Cabinet magnetic catch (bathroom cabinet, kitchen cabinet, closet, ...)

§  Magnetic door catch (wall-mounted)

§  Magnetic door catch (ceil-mounted)

§  Door magnetic switch/sensor (used to switch on bells or lights when door is opened)

§  Small toys' DC motors (flexible/rubber magnet)

§  DC motors

§  Hard disk (Neodymium magnet)

§  Laptop standby locking switch (not all laptops)

§  Fridge magnets used to stick papers of "notes" or "to do list" on fridges

§  Soap magnetic holder

§  Magnetic-base antenna mounted on cars

§  Small chess with magnetic base

§  Magnetic singing eggs: are one of the most popular toys in the world. Made from highly polished hematite magnet material, they have a deep black color and ultra smooth finish. Normally they are elliptical and sphere shape.
These eggs are incredibly fascinating. Whenever they are separated, they will try to find each other. When they do, amazing thing happen. If you throw them to the sky, they will give out nice snake sound caused by strong magnetic attraction. So "snake eggs" is another cute nickname. It is also a healthy massage. When you feel tied, their magnetism will do good to your body.
§ Desk magnets used for collecting paper clips

§ Fishing toy

§ Magnetic white board eraser, and white board magnets

§ Magnetic pin holder used by tailors

§ Magnetic picture frames

If you have some suggestions on where to find magnets in products all around us, leave a comment.

Thursday, July 26, 2012

Copying from microsoft office word to blogspot post editor

Writing your posts in Microsoft office word and copying and pasting them back in your post in blogspot is a great deal since the GUI of the post editor in blogspot lacks to some features that are already compatible with the site. The following are some examples of some items I copied from microsoft office 2010.
Superscripts          BlogSpot
Subscripts          BlogSpot
Underlining          Single line with only one style  
Text highlighting with specific colors         Highlighted text
Strike  through        Stroked through text
Variety of font sizes 30 28 26 24 22 20 18 16
Bullets with different styles
     Line 1
     Line 2
§  Line 3
Different fonts that does not appear in blogspot editor    BankGothic Lt BT    Algerian      Broadway
Table with different effects

Symbols         © ® ∞ [ S

Edge flipping in Delaunay triangulation

Edge flipping is applied on internal edges (edge shared with two triangles) if they are illegal and can’t be applied on boundary edges. In triangulation (and meshing in general), all triangles (cells) should have the same notation direction (orientation) as this is useful in edge-based data structures used in rendering and simulation. The main trick in edge flipping is the notation of the new triangles. That means: if the original triangles are written in clockwise/counter clockwise orientation (order), then the new triangles should have the same orientation.

Consider that we have two adjacent triangles like the following (written in Scilab/Matlab syntax):

OldTri1=[A, C, B];     OldTri2=[D, B, C];

Where A, B, C, D are integers representing the indices of the vertices, and in our case the notation orientation is clockwise.

Steps of flipping:

§  Get the vertices of the shared edge the shared edge is [B, C] or [C, B]

§  Get the edges of the first triangle edges=[A, C;C, B;B, A]

§  Remove the shared edge from the edges of the first triangle edges=[A, C;B, A]

§  We have two triangles with total of four vertices. Now, get the vertex that we can call far vertex. This vertex is simply the fourth vertex that does not belong to the first triangle, in our case the far vertex is D

§  Now, the new triangles after flipping are

NewTri1=[(the first edge/row in edges) (far vertex)]=[A, C, D];
NewTri2=[(the second edge/row in edges) (far vertex)]=[B, A, D];

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.

Sunday, July 22, 2012

Lawson triangulation method

The following flow chart summarize the algorithm of Lawson triangulation method

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.

The idea beyond fiber construction

There are many applications in our lives that use the fiber construction of materials like clothes, nylon ropes, steel ropes, fiber-glass, etc. In this post I’m going to consider the idea beyond using fiber construction in mechanical applications. Consider the figure below which represents two samples of the same material: one with continuous layout and the other is fiber-constructed and admire it.

Assume that those two samples are going to carry the same load with the same safety factor in the shown direction (direction is parallel to fibers in this case). Also, consider the presence of a crack (which may take place in our practical life) to the right of both samples. If the load is still applied on both samples, the crack will propagate till it reaches the other end of the sample as shown (end of crack propagation). On the other hand, the crack in the fiber-constructed material will terminates at the end of the fiber and the remainder of the fibers can carry the load, but with lower margin of safety.

So, and for this reason, the fiber construction is more preferable for being used in high safety applications like airplane (body is made of aluminum-carbon fibers), hoisting systems (cranes, lifts, …), three-bladed conventional wind turbines, and all other applications.

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 
else f=f_new; 




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

Heating element placement in electric water heater

If you are going to place an electric heating element in a tank to make an electric water heater, then you have two proposed layouts: vertical and horizontal layouts.

In fact, the vertical layout of the heating element is going to be affected by changing water level due to consumption leading to overheating of some portion of the heating element which may result in burn and failure of the heating element. Also, the vertical layout in most cases is just wasting energy and inefficient. Furthermore, in the vertical layout, the natural buoyancy potential is decreasing when moving up along the heating element.  

On the contrary, in the horizontal layout, the heating element is always immersed (fully covered) under water and so it is protected from overheating and efficient.

Iteration termination (limit) and convergence in Newton-Raphson iteration

In any iterative solver, there are many stopping (termination) methods to stop iteration loop because the solver is not going to iterate forever.

Iteration loop will stop in the following cases:

§  Solution is obtained: It is a waste of time and resources to iterate further after the solution is known.

§  Maximum number of iterations reached with no solution obtained: you tell the solver previously to iterate for 1000 times – for example- that if the solution is not obtained within those iterations then stop iteration loop and output a message  “Convergence is not obtained” or “ No solution found”

§  Maximum iteration time elapsed with no solution obtained: in this case you tell the solver to stop iteration after a certain time is elapsed (say 1 minute) if the solution is not obtained. In this case also, messages “Convergence is not obtained” or “No solution found” can be displayed.

So …, how to check if the current value of the variable x is the solution or not? There are two methods I’ve implemented (maybe there are another methods, I don’t know):

§  Absolute residual error: you tell the solver if the error of the residual is less than or equal certain value (say 0.001, which is the reasonable error for you), then the current value of the variable x is the solution.

§  Variable x convergence criteria: it may be used when you set an absolute error value that can not be obtained in iteration due to round off error. So, you tell the solver if the value of x is obtained for many times successively (say 100 times), then consider this value as a solution.

Wednesday, July 4, 2012

Critical (economic) radius (thickness) of insulation

Consider a very thin pipe with negligible conduction resitance with a hot fluid inside and cold ambient temperature. Also, consider an insulation material with known thermal conductivity. Our aim is to insulate the pipe with this insulation material with no option to choose another insulation material.

A little thing to remember is the relation to calculate the crtical radius of insulation
Note: In the following paragraphs, bare pipe means a pipe without insulation.

Consider the plot drawn below which shows the thermal resistance of the system versus the outer radius of insulation. The outer radius of the pipe (which is the inner radius of the insulation) is at point (1), and the critical radius of insulation -calculated using the previous formula- for the insulation material and the ambient fluid is at point (2).

At point (1) the thickness of insulation is zero (because radius of insulation equals the outer radius of the pipe) and so the thermal resistance of the system is only convection.

Note that by adding insulation (increasing radius of insulation) starting from point (1) to point (2), the thermal resistance of the system decreases that it becomes lower than the resistance of the bare pipe. That means the insulation in this case improves heat transfer rather that insulation.

By adding extra insulation starting from point (2) to point (3), the thermal resistance of the sytem increases, but it is still lower than the thermal resistance of the bare pipe.

Adding more insulation starting from point (3) is going to increase the thermal resistance of the system that it will be higher than the thermal resistance of the bare pipe. So, insulation will actually take place starting from point (3) and therefore, the insulation material between points (1) and (3) is just a wasted material.

So, Based on the previous example, it is economically-recommended to select an insulator material that has a critical radius of insulation smaller than the pipe radius so that insulation will take place whatever the insulation thickness.