Saturday, November 26, 2011

A Basic Android Example

A Basic Android Example
Android according to the definition provided at developer.android.com is 
 a software stack for mobile devices that includes an operating system, middleware and key applications. The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.

It provides the necessary tools for developing software for the Android OS. Important parts are :-
1) SQLLite for Data Storage.
2) Optimized Graphics 
3) Media support for common audio/video formats & still image formats.
4) GSM
5) Bluetooth
6) Camera, GPS , etc,

For developing Android applications we need to download the Android SDK, and also setup Eclipse on our computer. Both are available as free downloads. It is also possible to develop using only the Android SDK from the commandline.

Many setup guides are available on the internet, and setting up the environment should be a cinch.

A Sample Application
To begin, let us develop a simple application that shall read two numbers from two textfields and display their sum in the third.

First of all start Eclipse, New Project  and select Android Project.
Since we are developing for the Samsung Galaxy, select Samsung Tab.

Android programming is done using the Java Programming Language, and user interface design is done using XML. 
To create the user interface .
After creating the project.

Open main.xml
This will bring up the User Interface designer. It has two views. The Graphical Layout & the XML view
Now we shall drag and drop three EditText boxes and a Button onto the mobile.
This is the user interface now.

This is the main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberSigned" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberSigned" />


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberSigned" />

</LinearLayout>


We shall change the caption of the button to add.
This is done by adding a new String to
strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Dedicated to Hypatia of Alexandria</string>
    <string name="app_name">HypatiaBasicAndroid</string>
    <string name="buttoncaption">Add</string>

</resources>




 HypatiaBasicAndroidActivity.java
package hypatia.basic;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class HypatiaBasicAndroidActivity extends Activity {
    /** Called when the activity is first created. */
    EditText t1,t2,t3;
    Button b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b=(Button)findViewById(R.id.button1);
        t1=(EditText)findViewById(R.id.editText1);
        t2=(EditText)findViewById(R.id.editText2);
        t3=(EditText)findViewById(R.id.editText3);
       
        b.setOnClickListener(new ButtonHandler());
       
       
    }
    class ButtonHandler implements OnClickListener
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try
            {
                int a=Integer.parseInt("" + t1.getText());
                int b=Integer.parseInt("" + t2.getText());
                int sum=a+b;
                t3.setText("" + sum);
               
            }
            catch (Exception ex) {
                // TODO: handle exception
                t3.setText(ex.getMessage());
            }
            }
        }
       
    }

In future posts, we shall cover all of android.

Thursday, June 16, 2011

Basics of the Genetic Algorithm in Matlab

Genetic Algorithm Optimization in MATLAB

A genetic algorithm is a population-based optimization method inspired by natural selection. It repeatedly selects promising candidate solutions and creates new solutions using crossover and mutation.

Genetic algorithms can be useful when:

  • The objective function is nonlinear, discontinuous or difficult to differentiate.
  • The search space contains several local optima.
  • Traditional derivative-based methods are unsuitable.

A genetic algorithm is not automatically faster or better than every other optimization technique. The appropriate method depends on the problem. For the linear examples below, linprog would normally be more efficient, but they provide a simple introduction to MATLAB’s ga syntax.

Genetic algorithm and natural selection

Main components

  • Fitness function: measures the quality of a candidate solution.
  • Selection: chooses candidates for reproduction.
  • Crossover: combines information from selected candidates.
  • Mutation: introduces controlled random changes.

MATLAB syntax

[x, fval, exitflag] = ga( ...
    fitnessFunction, ...
    numberOfVariables, ...
    A, b, ...
    Aeq, beq, ...
    lowerBounds, upperBounds);

MATLAB expresses linear inequality constraints in this form:

A × x ≤ b

Use empty brackets, [], when an optional constraint is not required.

Problem 1: Minimization with linear constraints

Minimize:

z = −x1 + 2x2

Subject to:

  • −x1 + 3x2 ≤ 10
  • x1 + x2 ≤ 6
  • x1 − x2 ≤ 2
  • x1 ≥ 0 and x2 ≥ 0

Fitness function: fitness1.m

function y = fitness1(x)
    y = -x(1) + 2*x(2);
end

MATLAB commands

A = [-1  3;
      1  1;
      1 -1];

b = [10;
      6;
      2];

lb = [0 0];

[x, fval, exitflag] = ga( ...
    @fitness1, 2, A, b, [], [], lb, []);

The solution should be approximately:

x ≈ [2 0]
fval ≈ -2

Genetic algorithms use randomness, so the displayed values may differ slightly between runs.

Problem 2: Converting greater-than constraints

Minimize:

z = 20x1 + 40x2

Subject to:

  • 36x1 + 6x2 ≥ 108
  • 3x1 + 12x2 ≥ 36
  • 20x1 + 12x2 ≥ 100
  • x1 ≥ 0 and x2 ≥ 0

MATLAB requires inequalities in the form A × x ≤ b. Therefore, multiply each greater-than constraint by −1:

  • −36x1 − 6x2 ≤ −108
  • −3x1 − 12x2 ≤ −36
  • −20x1 − 12x2 ≤ −100

Fitness function: fitness2.m

function y = fitness2(x)
    y = 20*x(1) + 40*x(2);
end

MATLAB commands

A = [-36  -6;
      -3 -12;
     -20 -12];

b = [-108;
      -36;
     -100];

lb = [0 0];

[x, fval, exitflag] = ga( ...
    @fitness2, 2, A, b, [], [], lb, []);

The solution should be approximately:

x ≈ [3.7647  2.0588]
fval ≈ 157.6471

Important observation

The original third row of matrix A used [-20 -10], although the stated constraint contained 12x₂. It has been corrected to [-20 -12].

Learn With Champak: Experiment with different population sizes, mutation settings and stopping criteria to observe how they affect the quality and speed of the result.

Tuesday, June 14, 2011

Solving Equations using Matlab

Equations and Polynomials in MATLAB

Algebra has a long history shaped by Indian, Babylonian, Egyptian, Greek and Persian mathematicians. Brahmagupta made important contributions to arithmetic, algebra and the treatment of zero, while the word algebra comes from the title of Al-Khwarizmi’s work Al-Kitab al-Mukhtasar fi Hisab al-Jabr wal-Muqabala.

Basic rule of equations

An equation remains balanced when the same valid operation is performed on both sides. If LHS = RHS, then:

  • LHS + x = RHS + x
  • LHS − x = RHS − x
  • LHS × x = RHS × x
  • LHS ÷ x = RHS ÷ x, provided x ≠ 0

Representing a polynomial in MATLAB

MATLAB represents a polynomial using a vector of coefficients arranged in descending powers.

p = [1 0 6 20];

This represents x3 + 6x + 20.

Evaluating the polynomial

Use polyval to evaluate the polynomial for a given value of x.

For x = 1

>> polyval(p, 1)

ans =

    27

For x = 2.5

>> polyval(p, 2.5)

ans =

    50.6250

For the complex value x = 1 + i

>> polyval(p, 1 + i)

ans =

    24.0000 + 8.0000i

Finding the roots

Use the roots function to calculate the roots of the polynomial.

>> roots(p)

ans =

    1.0000 + 3.0000i
    1.0000 - 3.0000i
   -2.0000

The roots can also be stored in a vector:

>> r = roots(p);

Reconstructing the polynomial

Use poly to recover the polynomial coefficients from its roots.

>> v = poly(r)

v =

    1.0000    0.0000    6.0000   20.0000

Plotting the polynomial

Create a vector of x-values, evaluate the polynomial and plot the result.

x = 0:0.01:10;
y = polyval(p, x);
plot(x, y);
grid on;
xlabel('x');
ylabel('p(x)');
title('p(x) = x^3 + 6x + 20');

Multiplying polynomials

Use conv to multiply two polynomials. To multiply the original polynomial by x − 2:

>> product = conv(p, [1 -2])

product =

     1    -2     6     8   -40

The resulting polynomial is x4 − 2x3 + 6x2 + 8x − 40.

Dividing polynomials

Use deconv to divide polynomials. Dividing the product by x − 2 returns the original polynomial:

>> deconv(product, [1 -2])

ans =

     1     0     6    20

Learn With Champak: Practise these commands by changing the coefficients, evaluating the polynomial at different values and comparing the resulting graphs.

Saturday, June 11, 2011

3 D Plots in Matlab

3 D Plots in Matlab

To create 3D plots , first of all we need to create 3 variables. x,y and z.

Create the x and y  arrays:-
>> x=-3:0.25:3;
>> y=-3:0.25:3;

Create the meshgrid:-

>> [X Y]=meshgrid(x,y);


Define:-
Z=sqrt(X.^2 + Y.^2);

mesh(X,Y,Z);





>>surf(X,Y,Z);



>>surf(X,Y,-Z);





>>waterfall(X,Y,Z):-
 >>contour(x,y,z):
 >>>> surfc(X,Y,Z):-
 Now we arrive to the view part:
View is used to give the viewing angle of the plot. the syntax of the command is :-
view(azimuth,elevation);
where  the azimuth is an angle measured in degrees  in the x-y plane and is measured relative to the negative y axis and positive as always in the anti clockwise direction.

The elevation is the angle measured from the x-y plane in the direction of the z - axis.
default values are azimuth=-37.5 and elevation =35.
Here are the results of applying different views to the last plot:-

>> view(-65,30);

 >> view(-37.5,40);

Next we shall move to interpolation using Matlab.

Matrices in Matlab

Matrices in Matlab

Matrix manipulation is probably the most important part of Matlab. It is what most people will use it for. In this post we shall learn how to create matrices, perform basic manipulations, and to solve simultaneous equations using Matlab.

Creating a Matrix:- 

>> a=[1,4,-1;1,1,-6;3,-1,-1];
a

a =

     1     4    -1
     1     1    -6
     3    -1    -1

>>

Transposing the matrix :-

>> a'

ans =

     1     1     3
     4     1    -1
    -1    -6    -1

>>

Inverse of a Matrix:-

>> inv(a)

ans =

    0.0986   -0.0704    0.3239
    0.2394   -0.0282   -0.0704
    0.0563   -0.1831    0.0423

>>

>> a*inv(a)

ans =

    1.0000   -0.0000         0
   -0.0000    1.0000         0
    0.0000    0.0000    1.0000

>> The identity matrix as you would expect.

Now let's try and solve the given equation using Matlab:-

1) x + y + z =9
2)2x - 3y + 4z =13
3)3x + 4y + 5z=40

Create two matrices a & b:-

>> a=[1,1,1;2,-3,4;3,4,5]

a =

     1     1     1
     2    -3     4
     3     4     5

>> b=[9;13;40]

b =

     9
    13
    40

>>
This equation can be represented as 
AX=B
=>inv(A) * A * X= inv(A) * B multiplying both sides by inv(A)
=> X=inv(A)* B 

>> inv(a)*b

ans =

    1.0000
    3.0000
    5.0000

>>
which means 
x=1
y=3
z=5

Now, let's try it the other way round.

The same equation can be represented as X'*A'=B' where ' represents the Transpose.
multiplying both sides by inv(A')
=>X'*A'*inv(A')=B'*inv(A')
=>X'=B'*inv(A')


>> b'*inv(a')

ans =

    1.0000    3.0000    5.0000

which means 
x=1
y=3
z=5



Thursday, June 9, 2011

Simple Plots in Matlab

Simple Plots in Matlab

This is a continuation of the earlier post Matlab Basics
Creating Plots in Matlab is probably the easiest in all possible options present in the Software Universe. We shall study only the most basic methods in this Post and then advance to comparatively complex features.

Create a Vector for representing the X - Axis
>> x=[-2*pi:.001:2*pi];
>>
>> y=sin(x);
>> plot(x,y)
>> 




>> plot(cos(x),sin(x))
>>

 


 
>> plot(cos(x),sin(x).*cos(x))
>> 
%Remember the .* represents the dot product, and is necessary because all variables in Matlab are vectors and by default it will try to multiply them as matrices.


Next, we shall advance to more complex formatting of Plots, and then 3D plots.


Creating Variables and Simple Expressions in MATLAB

Basics of Matlab
The easiest way to begin learning Matlab is to start with the Command Window under the Desktop Menu.

Executing expressions in Matlab is absolutely simple !!! Just write the expression and view the results.

1) >> 3+4/10

ans =

    3.4000

>> 


To create a Variable

Simply write the expression
>> x=10

x =

    10

>> x*x + x

ans =

   110

>>

The variable is created automatically and initialized


To view the created variables write
>> x=1,y=2

x =

     1


y =

     2

>> who

Your variables are:

ans  x    y   


Point to remember: There are no scalars in Matlab. Only vectors. Thus x would  actually be [x].
Point to remember: To suppress an output use ; at the statements end.
Point to remember: use clc to clear the display.

>> x=5;
>> x=5

x =

     5

>> 




Creating Vectors

Vectors can be created in the following manners :
>> a=[1,2,3,4,5]

a =

     1     2     3     4     5

>>



>> a=[1:1:10]

a =

     1     2     3     4     5     6     7     8     9    10

>>



The first case is a simple enumeration, the second case specifies the starting value, the increment/decrement and the last value.

>> a=[10:-1:1]

a =

    10     9     8     7     6     5     4     3     2     1

>>


We shall learn 2D and 3D plotting next.