Practical File
Soft Computing
Master of Technology
In
Computer Science &
Engineering
SUBMITTED BY: - SUBMITTED
TO: -
Abhishek Gautam Mrs.
Divyashree
2224006
University Institute of
Engineering & Technology,
Maharshi Dayanand University,
Rohtak
INDEX
Sr. No. Program Teacher’
s
signature
1. Introduction to MATLAB.
2. Program to find greater of two numbers.
3. Program to perform Union, Intersection and
Complement operations.
4. Program for Hebb net to classify two
dimensional input patterns in bipolar with their
targets.
5. Program to generate ANDNOT function using
McCulloch-Pitts neural net.
6. Program to generate XOR function using
McCulloch-Pitts neural net.
7. Program for Perceptron net for an AND
function with bipolar inputs and targets.
8. Program to store a vector, find the weight
matrix with no self-connection. Test this using a
discrete Hopfield net.
9. Program to store a vector in an auto-associative
net. Find weight matrix & test the net with
input.
10. Program to calculate the weights using hetero-
associative neural net for mapping four input
vectors to two output vectors.
Practical – 1
Aim: Introduction to MATLAB.
Matlab is a commercial “Matrix Laboratory” package which operates as an
interactive programming environment. It is a multi-paradigm numerical computing
environment and fourth-generation programming language. It is a proprietary
programming language developed by MathWorks. It allows matrix manipulations,
plotting of functions and data, implementation of algorithms, creation of user
interfaces, and interfacing with programs written in other languages, including C, C+
+, C#, Java, Fortran and Python. Millions of engineers and scientists worldwide use
MATLAB to analyze and design the systems and products transforming our world. It
is used for machine learning, signal processing, image processing, computer vision,
communications, computational finance, control design, robotics, and much more.
The MATLAB application is built around the MATLAB scripting language. Common
usage of the MATLAB application involves using the Command Window as an
interactive mathematical shell or executing text files containing MATLAB code.
Matlab program script files always have filename ending with “.m”; the programming
language is exceptionally straightforward since almost every data object is assumed to
be an array. Graphical output is available to supplement numerical result.
Online help is available from the matlab prompt (a double arrow), both generally
(listening all available commands):
>> help
[a long list of help topic Follows]
And for specific commands:
>> help fft
[a help message on the fft function follows].
How To Quit Matlab
The answer to the most popular question concerning any program is this:
Leave a Matlab session by typing quit or by typing exit to the Matlab prompt.
Program – 2
Aim: Program to find greater of two numbers.
a=input('enter the value of a');
b=input('enter the value of b');
if(a==b)
disp('equal numbers')
end
if(a>b)
disp('a is greater')
disp(a)
elseif(b>a)
disp('b is greater')
disp(b)
end
Output:
Program – 3
Aim: Program to perform complement, union and intersection operations.
u = input('Enter first matrix');
v = input('Enter second matrix');
w = union(u,v);
p = intersect(u,v);
q = 1-u;
t = 1-v;
display('union of two matrices');
display(w);
display('intersection of two matrices');
display(p);
display('complement of 2nd matrix');
display(t);
display('complement of 1st matrix');
display(q);
Output:
Program – 4
Aim: Program for Hebb net to classify two dimensional input patterns in
bipolar with their targets.
clear;
clc;
E=[1 1 1 1 1 -1 -1 -1 1 1 1 1 1 -1 -1 -1 1 1 1 1];
F=[1 1 1 1 1 -1 -1 -1 1 1 1 1 1 -1 -1 -1 1 -1 -1 -1];
X(1,1:20)=E;
X(2,1:20)=F;
W(1:20)=0;
t=[1 -1];
b=0;
for i=1:2
W=W+X(i,1:20)*t(i);
b=b+t(i);
end
disp('Weight Matrix');
disp(W);
disp('Bias');
disp(b);
Output:
Program – 5
Aim: Program to generate ANDNOT function using McCulloch-Pitts
neural net.
clear;
clc;
% Getting weights and threshold value
disp('Enter the weights');
w1=input('Weight w1=');
w2=input('Weight w2=');
disp('Enter threshold value');
theta=input('theta=');
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=');
thete=input('theta=');
end
end
disp('McCulloch Pitts Net for ANDNOT function');
disp('Weights of neuron');
disp(w1);
disp(w2);
disp('Threshold value=');
disp(theta);
Output:
Enter the weights
Weight w1=1
Weight w2=1
Enter threshold value
theta=1
Output of net= 0 1 1 1
Net is not learning Enter another set of weights and threshold value
Weight w1=1
Weight w2=-1
theta=1
Output of net=0 0 1 0
McCulloch Pitts Net for ANDNOT function
Weights of neuron
1
-1
Threshold value=
1
Program – 6
Aim: Program to generate XOR function using McCulloch-Pitts neural net.
clear;
clc;
% Getting weights and threshold value
disp('Enter the 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 threshold 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);
Output:
Enter the weights
Weight w11=1
Weight w12=-1
Weight w21=-1
Weight w22=1
Weight v1=1
Weight v2=1
Enter threshold value
theta=1
Output of net= 0 1 1 0
McCulloch Pitts Net for XOR function
Weights of neuron z1
1
-1
Weights of neuron z2
-1
1
Weights of neuron y
1
1
Threshold value=
1
Program – 7
Aim: Program for Perceptron net for an AND function with bipolar inputs
and targets.
clear;
clc;
x=[1 1 -1 -1;1 -1 1 -1];
t=[1 -1 -1 -1];
w=[0 0];
b=0;
alpha=input('enter learning rate=');
theta=input('enter threshold value=');
con=1;
epoch=0;
while con
con=0;
for i=1:4
yin=b+x(1,i)*w(1)+x(2,i)*w(2);
if yin>theta
y=1;
end
if yin<=theta & yin>=-theta
y=0;
end
if yin<-theta
y=-1;
end
if y-t(i)
con=1;
for j=1:2
w(j)=w(j)+alpha*t(i)*x(j,i);
end
b=b+alpha*t(i);
end
end
epoch=epoch+1;
end
disp('perceptron for AND function');
disp('final weight matrix');
disp(w);
disp('Final Bias');
disp(b);
Output:
Program – 8
Aim: Program to store the vector (1 1 1 -1). Find the weight matrix with no
self-connection. Test this using a discrete Hopfield net with mistake in first
and second component of stored vector i.e (0 0 1 0). The given pattern in
binary form is (1 1 1 0).
clear;
clc;
x=[1 1 1 0];
tx=[0 0 1 0];
w=(2*x'-1)*(2*x-1);
for i=1:4
w(i,i)=0;
end
con=1;
y=[0 0 1 0];
while con
up=[4 2 1 3]
for i=1:4
yin(up(i))=tx(up(i))+y*w(1:4,up(i));
if yin(up(i))>0
y(up(i))=1;
end
end
if y==x
disp('convergence has been obtained');
disp('The converged output: ');
disp(y);
con=0;
end
end
Output:
Program – 9
Aim: Program to store vector [-1 -1 -1 -1] and [-1 -1 1 1] in an auto-
associative net. Find weight matrix. Test the net with [1 1 1 1] as input.
clear;
clc;
x= [ -1 -1 -1 -1; -1 -1 1 1];
t=[1 1 1 1];
w=zeros(4,4);
for i=1:2
w=w+x(i,1:4)'*x(i,1:4);
end
yin=t*w;
for i=1:4
if yin(i)>0
y(i)=1;
else
y(i)=-1;
end
end
disp('The calculated weight matrix');
disp(w);
if x(1,1:4)==y(1:4)|x(2,1:4)==y(1:4)
disp('The Vector is a Known vector');
else
disp('the vector is a unknown vector');
end
Output:
Program - 10
Aim: Program to calculate the weights using hetero-associative neural net
for mapping four input vectors to two output vectors.
clear;
clc;
x= [ 1 1 0 0;1 0 1 0; 1 1 1 0; 0 1 1 0];
t=[1 0; 1 0; 0 1; 0 1];
w=zeros(4,2);
for i=1:4
w=w+x(i,1:4)'*t(i,1:2);
end
disp('weight matrix');
disp(w);
Output: