Tuesday, June 21, 2011

example 6

This example show to find the maximum and minimum point.
Also how to label both points.

here is the code; just copy and paste at m-file, save it and run it.

function shili06
h0=figure('toolbar','none',...
'position',[200 150 450 400],...
'name','example06');
t=0:pi/10:2*pi;
h=plot(t,sin(t));
xlabel('t=0 to 2*pi','fontsize',16);
ylabel('sin(t)','fontsize',16);
title('\it{0 to2\pi}','fontsize',16)
x=get(h,'xdata');
y=get(h,'ydata');
imin=find(min(y)==y);
imax=find(max(y)==y);
text(x(imin),y(imin),...
['\leftarrow=',num2str(y(imin))],...
'fontsize',15)
text(x(imax),y(imax),...
['\leftarrow=',num2str(y(imax))],...
'fontsize',15)

Explanation as follow:

function shili06%function's name that is shili06

h0=figure('toolbar','none',...%opening a figure
'position',[200 150 450 400],...%figure properties
'name','example06');%figure name

t=0:pi/10:2*pi;%defining value for t
h=plot(t,sin(t));%plotting the graph; x=t, y=sin(t)

xlabel('t=0 to 2*pi','fontsize',16);%labeling on x-axis; name="t=0 to 2*pi"; fontsize=16.
ylabel('sin(t)','fontsize',16);%labeling on y-axis; name="sin(t)"; fontsize=16.

title('\it{0 to2\pi}','fontsize',16)%the title of graph="0 to pi";it=italic and allowing for greek %symbol; fontsize=16

x=get(h,'xdata');%this i not so sure but i think is getting all the values of x from h
y=get(h,'ydata');%this i also not so sure but i think is getting all the values of y from h

imin=find(min(y)==y);%find=is to find the non-zero element;min(y)==y is to tell the find to %look for smallest element.

imax=find(max(y)==y);%find=is to find the non-zero element;max(y)==y is to tell the find to %look for biggest element.

text(x(imin),y(imin),...%indicating where the location of text should be.
['\leftarrow=',num2str(y(imin))],...%pointing the arrow to left, convert the number of "y minimum" to string
'fontsize',15)%fontsize=15

text(x(imax),y(imax),...%indicating where the location of text should be.
['\leftarrow=',num2str(y(imax))],...%pointing the arrow to left, convert the number of "y maximum" to string
'fontsize',15)%fontsize=15

%remark:In matlab, number need to converted to string, then only it can be displayed.

No comments:

Post a Comment