Soft Computing Sample File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Soft Computing

LAB PRACTICAL FILE

B.Tech IT

8th Semester

SUBMITTED TO: SUBMITTED BY:


Mr.Madhur Jain Divit Jain
35120803117
IT Department

BHAGWAN PARSHURAM INSTITUTE OF TECHNOLOGY


List of Experiment

S. No. Index

1. Introduction to Matlab.

2. Write a MATLAB program for Perceptron net for an AND function


with bipolar inputs and targets.

3. Generate ANDNOT function using McCulloch-Pitts neural net by


MATLAB program.

4. Write a program in MATLAB to perform Union, Intersection and


Complement operations.

5. Write a program in MATLAB to plot various membership functions.

6. Use Fuzzy toolbox to model tip value that is given after a dinner which
can be-not good, satisfying, good and delightful and service which is
poor, average or good and the tip value will range from Rs. 10 to 100.

7. To implement FIS Editor.

8. To calculate the weights for given patterns using hetero associative


neural net.

9. To store vector in an auto-associative net. Find weight matrix & test


the net with input.
Experiment 1

Introduction to Matlab.
Matlab (Matrix Laboratory) is an interactive software system for numerical computations
and graphics. As the name suggests, Matlab is especially de-signed for matrix computations:
solving systems of linear equations, computing eigenvalues, and eigenvectors, factoring matrices,
and so forth.

MATLAB is a high-performance language for technical computing. It integrates


computation, visualization, and programming environment. Furthermore, MATLAB is a modern
programming language environment: it has sophisticated data structures, contains built-in editing
and debugging tools, and supports object-oriented programming. These factors make MATLAB
an excellent tool for teaching and research. MATLAB has many advantages compared to
conventional computer languages (e.g., FORTRAN) for solving technical problems. MATLAB is
an interactive system whose basic data element is an array that does not require dimensioning. The
software package has been commercially available since 1984 and is now considered as a standard
tool at most universities and industries worldwide.

It has powerful built-in routines that enable a very wide variety of computations. It also has
easy to use graphics commands that make the visualization of results immediately available.
Specific applications are collected in packages referred to as toolbox. There are toolboxes for
signal processing, symbolic computation, control theory, simulation, optimization, and several
other fields of applied science and engineering. Matlab is designed to solve problems numerically,
that is, in finite-precision arithmetic. Therefore it produces approximate rather than exact solutions,
and should not be confused with a symbolic computation system (SCS) such as Mathematica or
Maple. It should be understood that this does not make Matlab better or worse than an SCS; it is a
tool designed for different tasks and is therefore not directly comparable.

The MATLAB layout is divided into 4 windows (white area):

1. Command Window (centre), where you will type in all commands after the double arrow
“>>”
2. Command History (bottom right), showing a history of commands in the order you typed
them.
3. Workspace (top right), which will show your current variables. We will come back to this
when we introduce variables.
4. Current Folder (left) has a toolbar with your current directory shown. All your work will
be saved in this directory.
The MATLAB Desktop Layout

Global Tabs
Open the MATLAB R2012b for the first time, you will notice three tabs -- the Home tab, the Plots
tab, and the Apps tab. These three tabs are always there no matter what you are doing in MATLAB.
For that reason, they are called global tabs.

The Home tab, shown below, is where you go to do general purpose operations like creating new
files, importing data, managing your workspace, and setting your Desktop layout.
The Plots tab, shown below, is where you go to create MATLAB Plots. The Plots tab
displays a gallery of plots available in MATLAB and any toolboxes that were installed. To create
a plot from the gallery, you select the variables in the Workspace that you want to plot and then
select the type of visualization you want to use for that data. The downward facing arrow on the
far right brings down the full extent of the plot gallery with many more choices. The gallery is
smart, only showing plots that are appropriate for the data that you've selected.

The last of the global tabs is the Apps tab, shown below. It is the place you go to run interactive
MATLAB applications. The Apps tab presents a gallery of installed applications. The downward
facing arrow on the far right brings down the full extent of the applications gallery with many more
choices. To start an application you simply click on the correspondent icon.

Contextual Tabs
Features of MATLAB

• It is a high-level language for numerical computation, visualization and application


development.
• It also provides an interactive environment for iterative exploration, design and problem
solving.
• It provides vast library of mathematical functions for linear algebra, statistics, Fourier
analysis, filtering, optimization, numerical integration and solving ordinary differential
equations.
• It provides built-in graphics for visualizing data and tools for creating custom plots.
• MATLAB's programming interface gives development tools for improving code quality
maintainability and maximizing performance.
• It provides tools for building applications with custom graphical interfaces.
• It provides functions for integrating MATLAB based algorithms with external applications
and languages such as C, Java, .NET and Microsoft Excel.

MATLAB Advantages

• Implement and test your algorithms easily.


• Develop the computational codes easily.
• Debug easily.
• Use a large database of built in algorithms.
• Process still images and create simulation videos easily.
• Symbolic computation can be easily done.
• Call external libraries.
• Perform extensive data analysis and visualization.
Experiment 2

Write a MATLAB program for Perceptron net for an AND function with
bipolar inputs and targets.
% Perceptron for AND Function
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:

Enter Learning rate=1


Enter Threshold Value=0.5
"Perceptron for AND Function"
"Final Weight Matrix"
1. 1.
"Final Bias"
-1.
Experiment 3

Generate ANDNOT function using McCulloch-Pitts neural net by MATLAB


program.
%ANDNOT function using McCulloch-Pitts neuron
clear;
clc;
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.
Experiment 4

Write a program in MATLAB to perform Union, Intersection and Complement


operations.
%Enter Data
u=input('Enter First Matrix');
v=input('Enter Second Matrix');
%To Perform Operations
w=max(u,v);
p=min(u,v);
q1=1-u;
q2=1-v;
%Display Output
display('Union Of Two Matrices');
display(w);
display('Intersection Of Two Matrices');
display(p);
display('Complement Of First Matrix');
display(q1);
display('Complement Of Second Matrix');
display(q2);

Output:

Enter First Matrix = [0.3 0.4]


Enter Second Matrix = [0.1 0.7]
"Union Of Two Matrices"
0.3 0.7
"Intersection Of Two Matrices"
0.1 0.4
"Complement Of First Matrix"
0.7 0.6
"Complement Of Second Matrix"
0.9 0.3
Experiment 5

Write a program in MATLAB to plot various membership functions.


%Triangular Membership Function
x=(0.0:1.0:10.0)';
y1=trimf(x, [1 3 5]);
subplot(311)
plot(x,[y1]);
%Trapezoidal Membership Function
x=(0.0:1.0:10.0)';
y1=trapmf(x, [1 3 5 7]);
subplot(312)
plot(x,[y1]);
%Bell-Shaped Membership Function
x=(0.0:0.2:10.0)';
y1=gbellmf(x, [1 2 5]);
subplot(313)
plot(x,[y1]);

Output:
Experiment 6

Use Fuzzy toolbox to model tip value that is given after a dinner which can be-
not good, satisfying, good and delightful and service which is poor, average or
good and the tip value will range from Rs. 10 to 100.

We are given the linguistic variables quality of food and sevice as input variables which can be
written as:
Quality(not good,satisfying,good,delightful)
Service(poor,average,good)
Similarly Output variable is Tip value which may range from Rs. 10 to 100.

A Fuzzy system comprises the following modules:


1. Fuzzification Interface
2. Fuzzy Inference Engine
3. Deffuzification Interface

Fuzzy sets are defined on each of the universe of discourse:


Quality,service and tip value.
The values for Quality variable are selected for their respective ranges:

Similarly values for Service variable are selected for their respective ranges:
In general a compositional rule for inference involves the following procedure:
1. Compute memberships of current inputs in the relevant antecedent fuzzy set of rule.
2. If the antecedents are in conjunctive form, the AND operation is replaced by a minimum,
if OR then by Maximum and similarly other operations are performed.
3. Scale or clip the consequent fuzzy set of the rule by a minimum value found in step 2 since
this gives the smallest degree to which the rule must fire.
4. Repeat steps 1-3 for each rule in the rule base.
Superpose the scaled or clipped consequent fuzzy sets formed by such a superposition.
There are numerous variants of the defuzzifications.
The output will be displayed as:
Experiment 7

To implement FIS Editor.


FIS stands for Fuzzy Inference System. In FIS fuzzy rules are used for approximate reasoning. It
is the logical framework that allows us to design reasoning systems based on fuzzy set theory.
To illustrate these concepts we use example of Water Tank:

FIS editor consists of following units:-


i) Input
ii) Inference System
iii) Output

The Water Level is considered as the Input variable and Valve status is taken as Output Variable.
The Input-Output Variable’s Membership functions should be plotted along with their ranges:

The following screen appearance is obtained by clicking on the FIS Rule system indicator:

Rules are added by selecting variable’s values and clicking on add rule menu each time a new rule
is added.

The fuzzy Rules defined for water tank are:-


IF level is ok,THEN there is no change in valve.
IF level is low,THEN valve is open in fast mode.
IF level is high,THEN valve is closed in fast mode.
The result is displayed as plots of input-output membership functions:

Water Level(ok,low,high)
Valve Status(no change,open fast,closed fast)
The output in accordance with the input and rules provided by user is shown as (view-rule viewer):
OUTPUT:

Experiment 8
To calculate the weights for given patterns using hetero associative neural net.
Write an M-file to calculate the weights for the following patterns using hetero-associative
neural net for mapping four input vectors to two output vectors

S1 S2 S3 S4 t1 t2
1 1 0 0 1 0
1 0 1 0 1 0
1 1 1 0 0 1
0 1 1 0 0 1

% Hetero-associative neural net for mapping input vectors to 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:-

"Weight Matrix"
2. 1.
1. 2.
1. 2.
0. 0.

Experiment 9
To store vector in an auto-associative net. Find weight matrix & test the net
with input.
Write an M-file 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.

% Auto-association problem
clc;
clear;
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:

"The calculated Weight Matrix"


2. 2. 0. 0.
2. 2. 0. 0.
0. 0. 2. 2.
0. 0. 2. 2.
"The Vector is a UnKnown vector"

You might also like