08 December, 2011

PLOTTING 2-D GRAPHS USING MATLAB


2-D graphs in MATLAB (7.10.0 R-2010a) can be easily plotted using ‘plot ( )’ function. I am not describing the details of  MATLAB and using plot function.
An example of plotting a 2-D graph (sine curve):
x=0:0.01:2*pi;
y=sin(x);    
plot(x,y);

An example of plotting two graphs simultaneously:
x=0:0.01:2*pi;
y=sin(x);
z=cos(x);
plot(x,y);
hold on
plot(x,z,'--r'); %’--r’plots dashed red colored graph
hold off

Title, x-axis label, y-axis label, legend, arrow can be added using the option ‘Insert’ in figure window or using the Plot Edit Toolbar.
Grid lines can also be added using property editor-axes (go to edit option and then click axes properties) or by simply checking the edit plot option in tools and then right clicking on graph and checking the grid option. In the figure grid lines are added for only y-axis.
Legends can be edited by double clicking on them (initially you will see data1 and data2 written). Also properties of it can be edited using the property editor.
I have changed the x-limits to 8(explained later) so that the legend does not hide a part of graph. This is not necessary, but doing this makes the graph look neat and realizable at every point.
Edit Plot option in ‘Tools’ can be useful to avoid certain commands or alterations in commands and also properties of  the figure, graph paper, graph can be changed.

1)  FIGURE PROPERTIES
Check the Edit Plot option in tools and then left-click outside the graph i.e. the area which is purple in the figure. This gets the rectangle formed by 8 square bullets at the outermost of the figure. Now right click in the same area and you will get four options: Color, Close Figure, Show Property Editor (by which you can give the figure window a desired name and get the color options), Show M-Code.

2) GRAPH PAPER PROPERTIES
Edit Plot option being checked; click inside the graph paper and not on the graph drawn such that the eight rectangular bullets appear on the boundary of graph paper as shown in figure and then right click. 
Add Data option is a useful one and is discussed later.
Color option allows you to change the color of graph paper (here I have changed it to light green).
Show Property Editor option opens property editor-axes as shown in figure. From here you can give labels, set limits of the axes (here set to 8), grids for respective axis can be added and many more properties can be changed.
3) GRAPH PROPERTIES
In order to study graph properties we take a different example:
x=0:0.1:2*pi;
y=sin(x);
plot(x,y);


 

To edit the graph or plot we follow the same procedure i.e. check the edit plot option and now click on the graph or plot and nowhere else and the rectangular bullets will appear on the graph and now right click it.
Option color allows to change the color of plot, Line Style (way of joining points or plotting) can be set to solid, dash, dot, dash-dot, none.
In the figure shown below color of graph has been changed to black and Line Style is kept solid.


Line width is thickness of the graph and 'Marker' is another beautiful option which can make the graph look better, and its size can be adjusted by 'Marker Size'.
In the figure drawn below marker selected is ‘*’ as it can be seen checked and various types of it can be selected.
These options can be added by commands also and it is important to know them. We can edit the graph from the figure window but if multiple graphs are plotted like in case of 3-D figure where 200 or more graphs can be plotted on the same graph paper editing is not possible as we will have to select them one by one and do the required changes. Commands are convenient in this case as they can be put in the loop and the required condition will be applied to all the graphs. This you can evident from my next post.

Show Property Editor opens Property Editor-Lineseries window and similar options of Line style, Line width, Marker, Marker Size are available and we can opt for different Line Style color and different marker color which we earlier couldn’t do.
X data source and Y data source can be also specified by just putting the expression in the respective column. In the figure shown below Ydata source has been put ‘cos(x).*exp(-0.2*x)’ and x dat source auto and the resultant graph is shown.
In the Edit Plot mode, in all the possible three domains: figure, graph paper and graph explained earlier, Property Editor function is the best function as you can access all possible options for editing and many additional ones too, which are useful like different color can be filled in markers (closed), outline can be of different color and the graph can be given a third color. In the edited damped sine the marker is filled with red, outlined with blue and graph is dashed black. A simple graph of damped sine can be drawn simply by defining x vector and putting y= sin(x).*exp(-0.2*x) and can be made presentable by editing.
graph without editing
same graph edited using various property editors
2-D GRAPH TYPES
Many types of graphs can be plotted using MATLAB : bar graphs, pie charts etc.
I will explain the useful graph types in brief, so that they can be used for basic work. Let us assume the following data of a company:

  Year
2005
2006
2007
2008
2009
2010
Company A profit(in crores)

  500.43

 562.62

 357.81

 597.39

 624.61

  731.98

Now if we simply plot the data, the graph is not very much understandable, as shown

The x-axis is plotted with step size 0.5 i.e. 2005, 2005.5, 2006… In order to change it to one go to Property Editor-Axes as shown in above figure. Click the ‘Ticks..’ option, ‘edit axes ticks’ window will pop up as shown and then check the ‘Step by’ option and put 1 in the text box and click apply.
Graph can be made presentable by using bar graphs.

BAR GRAPHS
For the above plotted graph or the normal plot or Lineseries graph, go to Property Editor-Axes and change the Plot Type option to bar as shown in the figure 2.
Also the Plot Type can be changed using 'Add Data' option which I earlier mentioned in graph paper properties and this option can be selected by Plot Browser also.
Direct command bar(var1,var2) can be used to plot bar graphs. Commands can also be used to plot a certain color bar graph, line width can also be specified by commands. 

figure1 bar graph
But still it is not presentable. We can again follow the plot editing techniques using ‘Edit Plot’ option in the three domains. But this time, Property Editor in the third domain will be Property Editor-Barseries as shown in figure 2 and after editing the graph will appear as in figure shown below. Bar width and color can be changed by respective options, in Property Editor-Barseries.
figure-2 bar graph
Data of the corresponding bar can be written above the respective bar with the help of Text Box and properties of it can also be edited by double-clicking on it. Property Editor-Text box will open up and properties can be modified.  

I plotted another dashed lineseries red graph to make it look better. However, it is not necessary. The final edited graph, may look like shown below: 

AREA GRAPH
It fills the area under lineseries graph with the color of graph and then the outline or plot color can be changed. Change the Plot Type to Area after plotting the graph. area(var1,var2) command can also be used to plot the graph.
                                      
STAIR AND STEM GRAPH
Commands stairs(x), stairs(x,y) etc. can be used for stairs and stem(x), stem(x,y) can be used for plotting stem graph. These can also be selected from the Plot Type or the Plot catalog and editing can be done in edit plot mode.


PIE CHARTS
Pie charts are used for one variable. Consider in a survey if 52% of students opt engineering, 14% medical,18% commerce  and remaining 16% other fields and we are required to draw a pie chart then following can be done:

a=[52 14 18 16 ];    %declare the vector with data to be plotted
pie(a)                     % forms a pie chart for data a

Each patch can be edited e.g color, outline etc. and a patch can be separated out by defining another vector whose dimensions are equal to that of vector of data. If we are required to separate other fields students patch. Then 4th value should be put1and rest zero,
c=[0 0 0 1];
pie(s,c)
Pie charts can also be made from the Plot Catalog from Figure palette or from Plot Type.

3-D graphs, 3-D pie charts, 3-D bar graphs, 3-D figures can also be drawn using MATLAB which I will explain in my next post.


28 September, 2011

FLOWING WATER INDICATOR


Submersible water pumps in underground water tanks are commonly used for filling overhead tanks. Sometimes, the water pump may run dry in case of low water level. This may cause an internal damage to the pump. To avoid this an fully/semi-automatic pump driver system and water level indicator can be used which will switch on the pump if water-level of the overhead tank is below a particular level and if there is adequate water in the underground tank. It will also switch it off or keep it switched off, if the overhead tank is full or in case of low water level in the underground tank. But this method is a bit costly and obviously it is fully automatic and is the best solution. But I made something which I think is the cheapest solution to the problem i.e. FLOWING WATER/WATER STREAM INDICATOR.
If the pump will run dry, there will be no water in the pipe through which the overhead tank fills. This condition can be detected by a simple electrical circuit. Impure water is a good conductor of electricity and I used this principle.
In the above circuit LED glows when water is flowing and switch is closed. I used a 16 V source for this circuit and adjusted potentiometer to around 1.66 k(explained later) so that voltage drop across LED is less than 10V. As flowing water resistance is in series, value of the components may vary which can be easily calculated.

SENSORS FITTED ON A
THERMOCOL BLOCK
I used a waste thermocol block of dimensions about 20cm x 8cm x 8cm (according to my plastic overhead tank and the pipe in it as shown in picture) for getting the sensors(conductors) fixed just below the pipe. So you may use a block (water resistant) of different dimensions for fitting the sensors/conductors.
I used two 2.5” brass screws (as they don’t rust) as my sensors and fitted them 3cm apart from each other along the axial line of pipe and tied two ends of wire on the heads of both the screws.

The length of  the wire tied to both the screw heads was 30 feet in this case and this can vary according to the place where you want to fix the LED and LED can be fitted anywhere inside your home or near the motor switch.


CALCULATION OF VARIOUS PARAMETERS

Value of resistance in series(R1) will vary according to the length of wire, type and pressure of water.

In the circuit used above, replace 10k pot and led with a multimeter i.e. connect a multimeter with the sensors under flowing water and with full length of wire as shown in the circuit:




Set the multimeter to voltage measuring mode and get the reading. It will give us the value of open circuit voltage which would be approximately V(16V in this case) and then set the multimeter to current  measuring mode and get the short circuit current reading.It will be a few milli-amperes due to the flowing water resistance.In my case it was around 4-5mA and so the resistance offered by water was approximately16V/5mA=3.2k .Now come back to the original circuit with LED. We need to limit the forward voltage of led to 5-10V. I wanted it to be 6V which can be monitored by connecting a voltmeter(multimeter set involtage mode) in parallel with the led.Keep on adjusting the potentiometer till you get 6V or desired forward voltage across LED.Current will decrease accordingly which we dont need to worry about as it would be less than 5mA and forward current limit of a LED is 20mA. Set the multimeter to resistance mode and measure the set potentiometer resistance(R2).The current can be calculated by a simple kvl equation:
                                                   V-I(R1+R2)-V1=0
V=source voltage
 I=series current
R1= resistance offered by water stream
R2=series resistance(10 k pot. which was set)
         
For my project I substituted V=16, R1=3.2k, V1=6V and R2=1.66k

                                              16-I(3.2k+1.66k)-6=0
                                                 
                                                    I=2.05mA
This circuit can be used in any case where you need to detect flowing water. Rain water can be detected by replacing both the screws and using sensor which works for rain drops. Sensor can be made by etching a pcb which look likes below :
But now the values of the components will change because even a drop of water will glow the led.

IMAGES OF WATER STREAM DETECTOR:
   (view enlarged images by clicking on them)

CIRCUIT TESTING
BLOCK FITTED IN OVERHEAD TANK
TWO NUT-BOLTS FITTED VERTICALLY
TO FIX THE BLOCK

TWO NUT-BOLTS FITTED HORIZONTALLY
FOR THE SAME



LED FITTED NEAR MOTOR SWITCH


WATER STREAM FALLING ON
BOTH SCREWS

FULL VIEW OF THE SYSTEM
WITH OVERHEAD TANK