XOR Function

20 November 2011



Generate XOR function using McCulloch-Pitts neuron by writing an M-file. The truth table for the XOR function is.

X1    X2    Y
0     0     0
0     1     1
1     0     1
1     1     0 

example38.m
%XOR function using McCulloch-Pitts neuron
clear;
clc;

%Getting weights and threshold value
disp('Enter Weights');
w11 = input('Weight w11 = ');
w12 = input('Weight w12 = ');
w21 = input('Weight w21 = ');
w22 = input('Weight w22 = ');
v1 = input('Weight v1 = ');
v2 = input('Weight v2 = ');

disp('Enter Threshold Value');
theta = input('theta = ');

x1 = [0 0 1 1];
x2 = [0 1 0 1];
z = [0 1 1 0];

con = 1;
while con
    zin1 = x1 * w11 + x2 * w21;
    zin2 = x1 * w21 + x2 * w22;
  
    for i = 1:4
        if zin1(i) >= theta
            y1(i) = 1;
        else
            y1(i) = 0;
        end
      
        if zin2(i) >= theta
            y2(i) = 1;
        else
            y2(i) = 0;
        end
    end
  
    yin = y1 * v1 + y2 * v2;

    for i = 1:4
        if yin(i) >= theta
            y(i) = 1;
        else
            y(i) = 0;
        end
    end
  
    disp('Output of Net');
    disp(y);

    if y == z
        con = 0;
    else
        disp('Net is not learning enter another set of weights and Threhold value');
        w11 = input('Weight w11 = ');
        w12 = input('Weight w12 = ');
        w21 = input('Weight w21 = ');
        w22 = input('Weight w22 = ');
        v1 = input('Weight v1 = ');
        v2 = input('Weight v2 = ');
        theta = input('theta = ');
    end
end

disp('McCulloch-Pitts Net for XOR function');

disp('Weights of Neuron Z1');
disp(w11);
disp(w21);

disp('Weights of Neuron Z2');
disp(w12);
disp(w22);

disp('Weights of Neuron Y');
disp(v1);
disp(v2);

disp('Threshold value');
disp(theta);

ANDNOT

Generate  ANDNOT function using McCulloch-Pitts neural net by a MATLAB program. The truth table for the ANDNOT function is as follows:

X1    X2    Y
0     0     0
0     1     0
1     0     1
1     1     0 

example37.m
%ANDNOT function using Mcculloch-Pitts neuron
clear;
clc;

%Getting weights and threshold value
disp('Enter weights');
w1 = input('Weight w1 = ');
w2 = input('Weight w2 = ');

disp('Enter Threshold Value');
theta = input('thete = ');

y = [0 0 0 0];
x1 = [0 0 1 1];
x2 = [0 1 0 1];
z = [0 0 1 0];

con = 1;
while con
    zin = x1 * w1 + x2 * w2;
  
    for i = 1:4
        if zin(i) >= theta
            y(i) = 1;
        else
            y(i) = 0;
        end
    end
  
    disp('Output of Net');
    disp(y);
    if y == z
        con = 0;
    else
        disp('Net is not learning enter another set of weights and threshold value');
        w1 = input('Weight w1 = ');
        w2 = input('Weight w2 = ');
        theta = input('thete = ');
    end
end

disp('Mcculloch-Pitts Net for ANDNOT function');

disp('Weights of Neuron');
disp(w1);
disp(w2);

disp('Threshold value');
disp(theta);

Activation Function

Write a MATLAB program to generate a few activation functions that are being used in neural networks. The activation functions play a major role in determining the output of the functions. One such program for generating the activation functions is as given below.

example21.m

%Illustration of various activation function used in NN's
x = -10:0.1:10;
tmp = exp(-x);
y1 = 1 ./ (1 + tmp);
y2 = (1 - tmp) ./ (1 + tmp);
y3 = x;
subplot(231); plot(x, y1); grid on;
axis([min(x) max(x) -2 2]);
title('Logistic Function');
xlabel('(a)');
axis('square');
subplot(232); plot(x, y2); grid on;
axis([min(x) max(x) -2 2]);
title('Hyperbolic Tangent Function');
xlabel('(b)');
axis('square');
subplot(233); plot(x, y3); grid on;
axis([min(x) max(x) min(x) max(x)]);
title('Identity Function');
xlabel('(c)');
axis('square');

Conversions between inches and centimeters

15 November 2011
Write a class that contains the following two method:


/** Converts from inches to centimeters */

public static double inchToCentimeter(double in)

/** Converts from centimeters to inches */
public static double centimeterToInch(double cm)


The formula for the conversion is:

centimeters = 2.54 x inches

Write a test program that invokes these methods to display the following tables:

Inches     Centimeters     Centimeters     Inches
 
1.0        2.54            5.0             1.96
2.0        5.08            10.0            3.93
...
9.0        22.86           45.0            17.71
10.0       25.4            50.0            19.68

Main.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package conversionbetweeninchesandcentimeters;

/**
 *
 * @author dekwan
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        sop("Inches    Centimeters    Centimeters    Inches\n");

        // SOP Inches to Centimeters
        for (int i = 1; i <= 10; i++)
        {
            sop("" + i);
            sop("         " + inchToCentimeter(i));
            sop("            " + i * 5);
            sop("             " + centimeterToInch(i * 5) + "\n");
        }
    }

    /** Converts from inches to centimeters */
    public static double inchToCentimeter(double in)
    {
        return in * 2.54;
    }

    /** Converts from centimeters to inches */
    public static double centimeterToInch(double cm)
    {
        return cm / 2.54;
    }

    private static void sop(String string)
    {
        System.out.print(string);
    }

}



Print C++

10 November 2011
Write a program that produces the following output:

CCCCCCCCC         ++              ++
CC                ++              ++
CC          ++++++++++++++  ++++++++++++++
CC          ++++++++++++++  ++++++++++++++
CC                ++              ++
CCCCCCCCC         ++              ++

main.cpp

#include<iostream>

using namespace std;

int main()
{
    cout << "CCCCCCCCC         ++              ++" << endl;
    cout << "CC                ++              ++" << endl;
    cout << "CC          ++++++++++++++  ++++++++++++++" << endl;
    cout << "CC          ++++++++++++++  ++++++++++++++" << endl;
    cout << "CC                ++              ++" << endl;
    cout << "CCCCCCCCC         ++              ++" << endl;

    return 0;
}

Print Programming Assignment 1

Write a programming that produces the following output:

****************************************
*         Programming Assignment 1     *
*          Computer Programming I      *
*           Author: Dek Wan            *
*      Due Date: Wednesday, Nov 10     *
****************************************

In your program, subtitute Dek Wan with your own name. If necessary, adjust the positions and the number of stars to produce a rectangle.

main.cpp

#include<iostream>

using namespace std;

int main()
{
    cout << "****************************************" << endl;
    cout << "*         Programming Assignment 1     *" << endl;
    cout << "*          Computer Programming I      *" << endl;
    cout << "*           Author: Dek Wan            *" << endl;
    cout << "*      Due Date: Thursday, Nov 10      *" << endl;
    cout << "****************************************" << endl;

    return 0;
}

Displaying Patterns

05 November 2011
Write a method to display a patter as follow:
1
            2 1
          3 2 1
...
n n-1 ... 3 2 1

The method header is

public static void displayPattern(int n)


Input:
9

Output:
1
       21
      321
     4321
    54321
   654321
  7654321
 87654321
987654321

Main.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package displayingpatterns;

import java.util.Scanner;

/**
 *
 * @author dekwan
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        Scanner scan = new Scanner(System.in);

        displayPattern(scan.nextInt());
    }

    public static void displayPattern(int n)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = n; j > i; j--)
            {
                sop(" ");
            }

            for (int j = i; j > 0; j--)
            {
                sop("" + j);
            }

            sop("\n");
        }
    }

    public static void sop(String str)
    {
        System.out.print(str);
    }

}


Sorting Three Numbers

04 November 2011
Write the following method to display three numbers in decreasing order:

public static void displaySortedNumbers(double num1, double num2, double num3)


Input:
5678 1234 6789

Output:
6789.0
5678.0
1234.0

Main.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sortingthreenumbers;

import java.util.Scanner;

/**
 *
 * @author dekwan
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        Scanner scan = new Scanner(System.in);
        double num1 = scan.nextDouble();
        double num2 = scan.nextDouble();
        double num3 = scan.nextDouble();

        displaySortedNumbers(num1, num2, num3);
    }

    public static void displaySortedNumbers(double num1, double num2, double num3)
    {
        double temp;

        while (num1 < num2 || num2 < num3)
        {
            temp = num1;
            num1 = num2;
            num2 = temp;

            while (num2 < num3)
            {
                temp = num2;
                num2 = num3;
                num3 = temp;
            }
        }

        sopl("");
        sopl("" + num1);
        sopl("" + num2);
        sopl("" + num3);
    }

    public static void sopl(String str)
    {
        System.out.println(str);
    }

}



Summing int digits in an interger

Write a method that computes the sum of the digits in an interger. Use the following method header:

public static int sumDigits(long n)


For example, sumDigits(234) return 9 (2 + 3 + 4).

Input:
234

Output:
9

Main.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package summingthedigitsinaninterger;

import java.util.Scanner;

/**
 *
 * @author dekwan
 */
public class Main
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        Scanner scan = new Scanner(System.in);
       
        long number = scan.nextInt();
        sopl(sumDigits(number) + "\n");
    }

    public static void sopl(String str)
    {
        System.out.println(str);
    }

    public static int sumDigits(long digit)
    {
        int num;
        int sum = 0;
       
        while (digit != 0)
        {
            num = (int) digit % 10;
            digit = digit / 10;
            sum = sum + num;
        }

        return sum;
    }

}


Math: Triangular Numbers

03 November 2011
A triangulat number is define as 1 + 2 + 3 + ... + n for n = 1, 2, ..., and so on. So, the first few numbers are 1, 3, 6, 10, ... Write the following method that return a triangular number:


public static int getTriangularNumber(int n)


Write test program that displays the first 100 triangular number with 10 numbers on each line.

Input:
100

Ouput:
1 3 6 10 15 21 28 36 45 55
66 78 91 105 120 136 153 171 190 210
231 253 276 300 325 351 378 406 435 465
496 528 561 595 630 666 703 741 780 820
861 903 946 990 1035 1081 1128 1176 1225 1275
1326 1378 1431 1485 1540 1596 1653 1711 1770 1830
1891 1953 2016 2080 2145 2211 2278 2346 2415 2485
2556 2628 2701 2775 2850 2926 3003 3081 3160 3240
3321 3403 3486 3570 3655 3741 3828 3916 4005 4095
4186 4278 4371 4465 4560 4656 4753 4851 4950 5050

Main.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package triangularnumbers;

import java.util.Scanner;

/**
 *
 * @author dekwan
 */
public class Main
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        Scanner scan = new Scanner(System.in);

        int number = scan.nextInt();

        sop("\n");

        for (int i = 0; i < number; i += 10)
        {
            for (int j = 1; j <= 10; j++)
            {
                sop(getTriangularNumber(i + j) + " ");
            }
            sop("\n");
        }
    }

    public static int getTriangularNumber(int n)
    {
        return (n * (n + 1)) / 2;
    }

    public static void sop(String str)
    {
        System.out.print(str);
    }
}




Understanding Women