Tuesday, 6 May 2014

Fuzzy logic Matlab Toolbox 

Start with the MATLAB
By -
Ashish AC Nishad

What Is Fuzzy Logic?

Description of Fuzzy Logic

In recent years, the number and variety of applications of fuzzy logic have increased significantly. The applications range from consumer products such as cameras, camcorders, washing machines, and microwave ovens to industrial process control, medical instrumentation, decision-support systems, and portfolio selection.
To understand why use of fuzzy logic has grown, you must first understand what is meant by fuzzy logic.
Fuzzy logic has two different meanings. In a narrow sense, fuzzy logic is a logical system, which is an extension of multivalued logic. However, in a wider sense fuzzy logic (FL) is almost synonymous with the theory of fuzzy sets, a theory which relates to classes of objects with unsharp boundaries in which membership is a matter of degree. In this perspective, fuzzy logic in its narrow sense is a branch of FL. Even in its more narrow definition, fuzzy logic differs both in concept and substance from traditional multivalued logical systems.
In Fuzzy Logic Toolbox software, fuzzy logic should be interpreted as FL, that is, fuzzy logic in its wide sense. The basic ideas underlying FL are explained very clearly and insightfully in Foundations of Fuzzy Logic. What might be added is that the basic concept underlying FL is that of a linguistic variable, that is, a variable whose values are words rather than numbers. In effect, much of FL may be viewed as a methodology for computing with words rather than numbers. Although words are inherently less precise than numbers, their use is closer to human intuition. Furthermore, computing with words exploits the tolerance for imprecision and thereby lowers the cost of solution.
Another basic concept in FL, which plays a central role in most of its applications, is that of a fuzzy if-then rule or, simply, fuzzy rule. Although rule-based systems have a long history of use in Artificial Intelligence (AI), what is missing in such systems is a mechanism for dealing with fuzzy consequents and fuzzy antecedents. In fuzzy logic, this mechanism is provided by the calculus of fuzzy rules. The calculus of fuzzy rules serves as a basis for what might be called the Fuzzy Dependency and Command Language (FDCL). Although FDCL is not used explicitly in the toolbox, it is effectively one of its principal constituents. In most of the applications of fuzzy logic, a fuzzy logic solution is, in reality, a translation of a human solution into FDCL.
A trend that is growing in visibility relates to the use of fuzzy logic in combination with neurocomputing and genetic algorithms. More generally, fuzzy logic, neurocomputing, and genetic algorithms may be viewed as the principal constituents of what might be called soft computing. Unlike the traditional, hard computing, soft computing accommodates the imprecision of the real world. The guiding principle of soft computing is: Exploit the tolerance for imprecision, uncertainty, and partial truth to achieve tractability, robustness, and low solution cost. In the future, soft computing could play an increasingly important role in the conception and design of systems whose MIQ (Machine IQ) is much higher than that of systems designed by conventional methods.
Among various combinations of methodologies in soft computing, the one that has highest visibility at this juncture is that of fuzzy logic and neurocomputing, leading to neuro-fuzzy systems. Within fuzzy logic, such systems play a particularly important role in the induction of rules from observations. An effective method developed by Dr. Roger Jang for this purpose is called ANFIS (Adaptive Neuro-Fuzzy Inference System). This method is an important component of the toolbox.
Fuzzy logic is all about the relative importance of precision: How important is it to be exactly right when a rough answer will do?
You can use Fuzzy Logic Toolbox software with MATLAB technical computing software as a tool for solving problems with fuzzy logic. Fuzzy logic is a fascinating area of research because it does a good job of trading off between significance and precision—something that humans have been managing for a very long time.
In this sense, fuzzy logic is both old and new because, although the modern and methodical science of fuzzy logic is still young, the concepts of fuzzy logic relies on age-old skills of human reasoning.
Fuzzy logic is a convenient way to map an input space to an output space. Mapping input to output is the starting point for everything. Consider the following examples:
  • With information about how good your service was at a restaurant, a fuzzy logic system can tell you what the tip should be.
  • With your specification of how hot you want the water, a fuzzy logic system can adjust the faucet valve to the right setting.
  • With information about how far away the subject of your photograph is, a fuzzy logic system can focus the lens for you.
  • With information about how fast the car is going and how hard the motor is working, a fuzzy logic system can shift gears for you.
A graphical example of an input-output map is shown in the following figure.
To determine the appropriate amount of tip requires mapping inputs to the appropriate outputs. Between the input and the output, the preceding figure shows a black box that can contain any number of things: fuzzy systems, linear systems, expert systems, neural networks, differential equations, interpolated multidimensional lookup tables, or even a spiritual advisor, just to name a few of the possible options. Clearly the list could go on and on.
Of the dozens of ways to make the black box work, it turns out that fuzzy is often the very best way. Why should that be? As Lotfi Zadeh, who is considered to be the father of fuzzy logic, once remarked: "In almost every case you can build the same product without fuzzy logic, but fuzzy is faster and cheaper."

Why Use Fuzzy Logic?

Here is a list of general observations about fuzzy logic:
  • Fuzzy logic is conceptually easy to understand.
    The mathematical concepts behind fuzzy reasoning are very simple. Fuzzy logic is a more intuitive approach without the far-reaching complexity.
  • Fuzzy logic is flexible.
    With any given system, it is easy to layer on more functionality without starting again from scratch.
  • Fuzzy logic is tolerant of imprecise data.
    Everything is imprecise if you look closely enough, but more than that, most things are imprecise even on careful inspection. Fuzzy reasoning builds this understanding into the process rather than tacking it onto the end.
  • Fuzzy logic can model nonlinear functions of arbitrary complexity.
    You can create a fuzzy system to match any set of input-output data. This process is made particularly easy by adaptive techniques like Adaptive Neuro-Fuzzy Inference Systems (ANFIS), which are available in Fuzzy Logic Toolbox software.
  • Fuzzy logic can be built on top of the experience of experts.
    In direct contrast to neural networks, which take training data and generate opaque, impenetrable models, fuzzy logic lets you rely on the experience of people who already understand your system.
  • Fuzzy logic can be blended with conventional control techniques.
    Fuzzy systems don't necessarily replace conventional control methods. In many cases fuzzy systems augment them and simplify their implementation.
  • Fuzzy logic is based on natural language.
    The basis for fuzzy logic is the basis for human communication. This observation underpins many of the other statements about fuzzy logic. Because fuzzy logic is built on the structures of qualitative description used in everyday language, fuzzy logic is easy to use.
The last statement is perhaps the most important one and deserves more discussion. Natural language, which is used by ordinary people on a daily basis, has been shaped by thousands of years of human history to be convenient and efficient. Sentences written in ordinary language represent a triumph of efficient communication.

When Not to Use Fuzzy Logic

Fuzzy logic is not a cure-all. When should you not use fuzzy logic? The safest statement is the first one made in this introduction: fuzzy logic is a convenient way to map an input space to an output space. If you find it's not convenient, try something else. If a simpler solution already exists, use it. Fuzzy logic is the codification of common sense — use common sense when you implement it and you will probably make the right decision. Many controllers, for example, do a fine job without using fuzzy logic. However, if you take the time to become familiar with fuzzy logic, you'll see it can be a very powerful tool for dealing quickly and efficiently with imprecision and nonlinearity.

What Can Fuzzy Logic Toolbox Software Do?

You can create and edit fuzzy inference systems with Fuzzy Logic Toolbox software. You can create these systems using graphical tools or command-line functions, or you can generate them automatically using either clustering or adaptive neuro-fuzzy techniques.
If you have access to Simulink software, you can easily test your fuzzy system in a block diagram simulation environment.
The toolbox also lets you run your own stand-alone C programs directly. This is made possible by a stand-alone Fuzzy Inference Engine that reads the fuzzy systems saved from a MATLAB session. You can customize the stand-alone engine to build fuzzy inference into your own code. All provided code is ANSI compliant.
Because of the integrated nature of the MATLAB environment, you can create your own tools to customize the toolbox or harness it with another toolbox, such as the Control System Toolbox™, Neural Network Toolbox™, or Optimization Toolbox™ software.
 
Was this topic helpful?   

Monday, 22 April 2013

MATLAB by Example

By Ashish AC Nishad

1 Introduction

This document1 is aimed primarily for postgraduates and project students
who are interested in using MATLAB in the course of their work. Previous
experience with MATLAB is not assumed. The emphasis is on “learning by
doing” - try the examples out as you go along and read the explanations after.
If you read the online version2 you can paste the scripts into your editor and
save a lot of time.
2 Getting started
On the teaching system you can type matlab at a Terminal window, or look
in the Programs/ Matlab submenu of the Start menu at the bottom-left of
the screen. Depending on the set-up, you might start with several windows
(showing documentation, etc) or just a command line. This document will
focus on command line operations. In the examples below, >> represents matlab's
command-line prompt.
3 The MATLAB Language
The MATLAB interface is a command line interface rather like most BASIC
environments. However MATLAB works almost exclusively with matrices :
scalars simply being 1-by-1 matrices. At the most elementary level, MATLAB
can be a gloried calculator:
1Copyright
c 1996 by G. Chand, Cambridge University Engineering Department, Cambridge
CB2 1PZ, UK. e-mail: tpl@eng.cam.ac.uk. This document may be copied freely for the
purposes of education and non-commercial research.

>> fred=6*7
>> FRED=fred*j;
>> exp(pi*FRED/42)
>> whos
MATLAB is case sensitive so that the two variables fred and FRED are different.
The result of an expression is printed out unless you terminate the
statement by a ` ;'. j (and i) represent the square root of -1 unless you dene
them otherwise. who lists the current environment variable names and whos
provides details such as size, total number of elements and type. Note that the
variable ans is set if you type a statement without an ` =' sign.
4 Matrices
So far you have operated on scalars. MATLAB provides comprehensive matrix
operations. Type the following in and look at the results:
>> a=[3 2 -1
0 3 2
1 -3 4]
>> b=[2,-2,3 ; 1,1,0 ; 3,2,1]
>> b(1,2)
>> a*b
>> det(a)
>> inv(b)
>> (a*b)'-b'*a'
>> sin(b)
The above shows two ways of specifying a matrix. The commas in the
specication of b can be replaced with spaces. Square brackets refer to vectors
and round brackets are used to refer to elements within a matrix so that b(x,y)
will return the value of the element at row x and column y. Matrix indices
must be greater than or equal to 1. det and inv return the determinant and
inverse of a matrix respectively. The ' performs the transpose of a matrix. It
also complex conjugates the elements. Use .' if you only want to transpose a
complex matrix.
The * operation is interpreted as a matrix multiply. This can be overridden
by using .* which operates on corresponding entries. Try these :
>> c=a.*b
>> d=a./b
>> e=a.^b
For example c(1,1) is a(1,1)*b(1,1). The Inf entry in d is a result of
dividing 2 by 0. The elements in e are the results of raising the elements in a
to the power of the elements in b. Matrices can be built up from other matrices:
>> big=[ones(3), zeros(3); a , eye(3)]
big is a 6-by-6 matrix consisting of a 3-by-3 matrix of 1's, a 3-by-3 matrix of
0's, matrix a and the 3-by-3 identity matrix.
It is possible to extract parts of a matrix by use of the colon:
>> big(4:6,1:3)
This returns rows 4 to 6 and columns 1 to 3 of matrix big. This should result
in matrix a. A colon on its own species all rows or columns:
>> big(4,:)
>> big(:,:)
>> big(:,[1,6])
>> big(3:5,[1,4:6])
The last two examples show how vectors can be used to specify which noncontiguous
rows and columns to use. For example the last example should
return columns 1, 4, 5 and 6 of rows 3, 4 and 5.
5 Constructs
MATLAB provides the for, while and if constructs. MATLAB will wait for
you to type the end statement before it executes the construct.
>> y=[];
>> for x=0.5:0.5:5
y=[y,2*x];
end

>> y
>> n=3;
>> while(n ~= 0)
n=n-1
end
>> if(n < 0)
-n
elseif (n == 0)
n=365
else
n
end
The three numbers in the for statement specify start, step and end values for
the loop. Note that you can print a variable's value out by mentioning it's
name alone on the line. ~= means `not equal to' and == means `equivalent to'.
6 Help
The help command returns information on MATLAB features:
>> help sin
>> help colon
>> help if
help without any arguments returns a list of MATLAB topics. You can also
search for functions which are likely to perform a particular task by using
lookfor:
>> lookfor gradient
The MATLAB hypertext reference documentation can be accessed by typing
doc.
7 Programs
Rather than entering text at the prompt, MATLAB can get its commands from
a .m le. If you type edit prog1, Matlab will start an editor for you. Type in
the following and save it.

for x=1:10
y(x)=x^2+x;
end
y
The step term in the for statement defaults to 1 when omitted. Back inside
MATLAB run the script by typing:
>> prog1
which should result in vector y being displayed numerically. Typing
>> plot(y)
>> grid
should bring up a gure displaying y(x) against x on a grid. Like many matlab
routines plot can take a variable number of arguments. With just one
argument (as here) the argument is taken as a vector of y values, the x values
defaulting to 1,2,..., Note the effect of resizing the gure window.
The Teaching System is set up so that if you have a directory called matlab
in your home directory, then .m scripts there will be run irrespective of which
directory you were in when you started matlab.
8 Applications
8.1 Graphical solutions
MATLAB can be used to plot 1-d functions. Consider the following problem:
Find to 3 d.p. the root nearest 7.0 of the equation 4x3 + 2x2 􀀀 200x 􀀀 50 = 0
MATLAB can be used to do this by creating le eqn.m in your matlab directory:
function [y] = eqn(x)
% User dened polynomial function
[rows,cols] = size(x);
for index=1:cols
y(index) = 4*x(index)^3+2*x(index)^2-200*x(index)-50;
end
The rst line denes `eqn' as a function – a script that can take arguments.
The square brackets enclose the comma separated output variable(s) and the
round brackets enclose the comma separated input variable(s) - so in this case
there's one input and one output. The % in the second line means that the rest
of the line is a comment. However, as the comment comes immediately after
the function denition, it is displayed if you type :
>> help eqn
The function anticipates x being a row vector so that size(x) is used to nd
out how many rows and columns there are in x. You can check that the root is
close to 7.0 by typing:
>> eqn([6.0:0.5:8.0])
Note that eqn requires an argument to run which is the vector [6.0 6.5 7.0 7.5
8.0].
The for loop in MATLAB should be avoided if possible as it has a large
overhead. eqn.m can be made more compact using the . notation. Delete the
lines in eqn.m and replace them with:
function [y] = eqn(x)
% COMPACT user dened polynomial function
y=4*x.^3+2*x.^2-200*x-50;
Now if you type ` eqn([6.0:0.5:8.0])' it should execute your compact eqn.m
le.
Now edit and save ploteqn.m in your matlab directory:
x est = 7.0;
delta = 0.1;
while(delta > 1.0e-4)
x=x est-delta:delta/10:x est+delta;
fplot('eqn',[min(x) max(x)]);
grid;
disp('mark position of root with mouse button')
[x est,y est] = ginput(1)
delta = delta/10;
end
This uses the function fplot to plot the equation specied by function eqn.m
between the limits specied. ginput with an argument of 1 returns the x- and
y-coordinates of the point you have clicked on. The routine should zoom into
the root with your help. To nd the actual root try matlab's solve routine:
>> poly = [4 2 -200 -50];
>> format long
>> roots(poly)
>> format
which will print all the roots of the polynomial : 4x3 + 2x2 􀀀 200x 􀀀 50 = 0 in
a 15 digit format. format on its own returns to the 5 digit default format.
8.2 Plotting in 2D
MATLAB can be used to plot 2-d functions e.g. 5x2 + 3y2 :
>> [x,y]=meshgrid(-1:0.1:1,-1:0.1:1);
>> z=5*x.^2+3*y.^2;
>> contour(x,y,z);
>> prism;
>> mesh(x,y,z)
>> surf(x,y,z)
>> view([10 30])
>> view([0 90])
The meshgrid function creates a `mesh grid' of x and y values ranging from -1
to 1 in steps of 0.1. If you look at x and y you might get a better idea of how z
(a 2D array) is created. The mesh function displays z as a wire mesh and surf
displays it as a facetted surface. prism simply changes the set of colours in the
contour plot. view changes the horizontal rotation and vertical elevation of the
3D plot. The z values can be processed and redisplayed
>> mnz=min(min(z));
>> mxz=max(max(z));
>> z=255*(z-mnz)/(mxz-mnz);
>> image(z);
>> colormap(gray);
image takes interprets a matrix as a byte image. For other colormaps try help
color.
9 Advanced plotting
Consider the following problem:
Display the 2D Fourier transform intensity of a square slit.
(The 2D Fourier transform intensity is the diffraction pattern). Enter the following
into square_fft.m :
echo on
colormap(hsv);
x=zeros(32);
x(13:20,13:20)=ones(8);
mesh(x)
pause % strike a key
y=fft2(x);
z=real(sqrt(y.^2));
mesh(z)
pause
w=fftshift(z);
surf(w)
pause
contour(log(w+1))
prism
pause
plot(w(1:32,14:16))
title('fft')
xlabel('frequency')
ylabel('modulus')
grid
echo off
The echo function displays the operation being currently executed. The program
creates a 8-by-8 square on a 32x32 background and performs a 2D FFT
on it. The intensity of the FFT (the real part of y) is stored in z and the D.C.
term is moved to the centre in w. Note that the plot command when given a 3
by 32 array displays 3 curves of 32 points each.
10 Input and output
Data can be be transferred to and from MATLAB in four ways:
1. Into MATLAB by running a .m le
2. Loading and saving .mat les
3. Loading and saving data les
4. Using specialised le I/O commands in MATLAB
The rst of this involves creating a .m le which contains matrix specications
e.g. if mydata.m contains:
data = [1 1;2 4;3 9;4 16;5 25;6 36];
Then typing:
>> mydata
will enter the matrix data into MATLAB. Plot the results (using the cursor controls,
it is possible to edit previous lines):
>> handout length = data(:,1);
>> boredom = data(:,2);
>> plot(handout length,boredom);
>> plot(handout length,boredom,'*');
>> plot(handout length,boredom,'g.',handout length,boredom,'ro');
A .mat le can be created by a save command in a MATLAB session (see below).
Data can be output into ASCII (human readable) or non-ASCII form:
>> save results.dat handout length boredom -ascii
>> save banana.peel handout length boredom
The rst of these saves the named matrices in le results.dat (in your current
directory) in ASCII form one after another. The second saves the matrices
in le banana.peel in non-ASCII form with additional information such as the
name of the matrices saved. Both these les can be loaded into MATLAB using
load :
>> clear
>> load banana.peel -mat
>> whos
>> clear
>> load results.dat
>> results
>> apple=reshape(results,6,2)
The clear command clears all variables in MATLAB. The mat option in load
interprets the le as a non-ASCII le. reshape allows you to change the shape
of a matrix. Using save on its own saves the current workspace in matlab.mat
and load on its own can retrieve it.
MATLAB has le I/O commands (much like those of C) which allow you
to read many data le formats into it. Create le alpha.dat with ABCD as its
only contents. Then:
>> fid=fopen('alpha.dat','r');
>> a=fread(fid,'uchar',0)+4;
>> fclose(fid);
>> fid=fopen('beta.dat','w');
>> fwrite(fid,a,'uchar');
>> fclose(fid);
>> !cat beta.dat
Here, alpha.dat is opened for reading and a is lled with unsigned character
versions of the data in the le with 4 added on to their value. beta.dat is then
opened for writing and the contents of a are written as unsigned characters to
it. Finally the contents of this le are displayed by calling the Unix command
cat (the '!' command escapes from matlab into unix).
>> help fopen
>> help fread
will provide more information on this MATLAB facility.
11 Common problems
If you need save data in les data1.mat data2.mat etc. use eval in a .m le e.g.
for x=1:5
results=x;
operation=['save ','data',num2str(x),' results']
eval(operation)
end
The rst time this loop is run, 'save data1 results' is written into the operation
string. Then the eval command executes the string. This will save 1 in data1.mat.
Next time round the loop 2 is saved in data2.mat etc.
num2str is useful in other contexts too. Labelling a curve on a graph can be
done by:
>> gamma = 90.210;
>> labelstring = ['gamma = ',num2str(gamma)];
>> gtext(labelstring)
num2str converts a number into a string and gtext allows you to place the label
where you like in the graphics window.
Saving a gure in postscript format can be done using print -deps pic.ps.
Entering print on its own will print the current gure to the default printer.
12 More information
The Help System's MATLAB page3 is a useful starting point for MATLAB information
with links to various introductions and to short guides on curve-
tting4, symbolic maths5, etc, as well as contact information if you need help.
The handout on Using Matlab6 contains information on the local installation of
MATLAB.
4http://www-h.eng.cam.ac.uk/help/tpl/programs/Matlab/curve tting.html
5http://www-h.eng.cam.ac.uk/help/tpl/programs/Matlab/symbolic.html
6http://www-h.eng.cam.ac.uk/help/tpl/programs/matlab5/matlab5.html

thanks



Thursday, 14 March 2013

MATLAB Tricks 

hi friend ,
             Today we make a hart in matlab.................

n=100;
x=linspace(-3,3,n);
y=linspace(-3,3,n);
z=linspace(-3,3,n);
[X,Y,Z]=ndgrid(x,y,z);
F=((-(X.^2) .* (Z.^3) -(9/80).*(Y.^2).*(Z.^3)) + ((X.^2) + (9/4).* (Y.^2) + (Z.^2)-1).^3);
isosurface(F,0)
lighting phong
caxis
axis equal
colormap('flag');
view([55 34]);

try this plot on your MATLAB software.

                           thanks ..............................................



 

Tuesday, 26 February 2013

hello friends ,
          once again i come with soft computing tricks  . read and do it.



Any Folder as Photo Folder

Powered by Ashish AC Nishad
Any Folder as a Photo Folder

This tricks shows you how to setup any folder to display its contents as a photo folder. In many cases, you save your images/photos in a folder different than "My Pictures" (under "My Documents"). Since this folder you choose is just like any other normal folders, its contents displayed as a listing of files. You can temporary change it by just right-click and select "View" >> "Thumbnails". But if you want to change the folder so that everytime you come back, the folder itself, as well as all its subfolders, display the contents as photo thumbnails.

Here's How

Right-click on the folder you choose and select "Properties"



Select "Customize" Tab. Then under "Use this folder type as a template", select "Pictures (best for many files)"



Check on "Also apply this template to all subfolders" to apply the setting to subfoders as well
namskar friends ,
i come with some more mathematical MATLAB Plot plz try it

1. Example of cosine plot

% Simple script to plot a cosine
% vector x takes only 10 values
x = linspace(0, 2*pi, 10);
y = cos(x);
plot(x,y)
xlabel('x')
ylabel('cos(x)')
title('Plotting a cosine')
grid on





 2 If we use 100 points rather than 10, and evaluate two cycles instead of one (like this):

x = linspace(0, 4*pi, 100);

We obtain a different curve:




 2. Now, a variation with line-syles and colors (type 'help plot' to see the options for line-types and colors):

clear; clc; close all;
% Simple script to plot a cosine
% vector x takes only 10 values
x = linspace(0, 4*pi, 100);
y = cos(x);
plot(x,y, 'ro')
xlabel('x (radians)')
ylabel('cos(x)')
title('Plotting a cosine')
grid on




Space curve

Use the command plot3(x,y,z) to plot the helix:

If x(t)=sin(t), y(t)=cos(t), z(t)=(t) for every t in ,
then, you can type this code in an 'm-file' or in the command window:

% Another way to assign values to a vector
t = 0 : pi/50 : 10*pi;
plot3(sin(t),cos(t),t);

https://youtu.be/fa4Uy6PXVS8