GRAPHICS & OTHER MATLAB
FEATURES
% Author: Spike
% Date: 25/3/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function
y=mult(varargin)
if nargin>0
y=varargin{1};
for index=2:nargin
y=y.*varargin{index};
end;
end;
>>
mult(5,6,7,8)
ans =
1680
>> mult([1
2],[3 4],[5 6])
ans =
15
48
25.16 Sub-functions
• A function
existing in its own M-file is visible to all other functions
- can be called by any function
(or from main command window)
• At times this
global visibility is not desirable:
- some functions are very specific
to a particular task and should not be used for anything else
• Matlab allows
hiding of functions by placing multiple functions, all in the same file:
- the first function is globally
visible (can be called by anyone)
- the 2nd and subsequent functions
in the same file can only be called by those functions in that file
• For example the
temperature example from the previous lecture might be better with only the
main program dailyTemps visible
- change dailyTemps from a script
file to a function
- place all the other functions
(TempTable etc.) in the dailyTemps.m file after the dailyTemps function
25.17 Review
• Formal vs. actual
parameters and returned values
• Pass by value vs.
pass by reference
• Function
workspaces & scope rules
• Run-time structure
& the stack
• Return, error
& warning
• Variable numbers
of parameters & returned values
• Global variables
• Efficiency issues
• Sub-functions
26 GRAPHICS & OTHER MATLAB
FEATURES
• Matlab is a rich
environment that has continued to evolve (and grow!) across its lifetime
• This course has
concentrated on Matlab as an introductory programming environment for engineers
• Therefore there
are many aspects and facets of Matlab that have not been covered during the
course
• Today’s lecture
briefly highlights a number of Matlab’s major features that have not been
covered in the course
- fair emphasis on graphics capabilities
Reference:
For Engineers (Ch. 1, 5, 6, 9,
10, 11)
Mastering (Ch.
12, 18-20, 22-27, 30, 32-33)
Student (Ch.
14-22)
26.1 Motivation
• Matlab is an
extremely rich programming language and environment
- many features that
could not be adequately covered in such a short course
• Data visualisation
is a vital tool of scientists and engineers
- graphical 2D and
3D representation of data
- Matlab
incorporates many graphical routines
• Matlab
incorporates many in-built functions of high utility to scientists and
engineers
- built-in knowledge
of problem domains which can be employed with little preparation
- students are
likely to employ these additional features of Matlab in future courses and in
their careers.
|
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Email: kursusmatlab@gmail.com 26.2 2D Graphics –plot
• Most commonly used
and versatile function for plotting 2D data is plot()
• plot() has many
variants, common forms of usage are:
plot(x_vals,y_vals)
OR
plot(x_vals,y_vals,format)
• Basic form is a
line plot connecting values in 2D space.
|
EDU»
x=-2*pi:0.1:2*pi;y=cos(x);z=sin(x);
EDU»
plot(x,y,'ms-',x,z,'bp:');
EDU» xlabel('Angle
(Radians)'); title('Sine vs. Cosine');
26.3 Axes & Labels
• A useful figure
not only contains an image of the raw data but also meaningful labels for the
axes, a title and other relevant data.
|
• Matlab
incorporates a number of commands for labelling and modifying existing
figures:
title(): Give the figure a title
xlabel(): Label the horizontal axis
ylabel(): Label the vertical axis
grid(): Draw grid-lines on graph
box(): Enclose (or remove enclosing) figure in a
box
text(): Place text at the specified location on
the figure
gtext(): Interactively (with mouse) place text
legend(): Create a legend box
axis(): Control over axis scaling, range,
orientation, appearance, etc.
|
26.4 Printing Figures
• It is often useful
to obtain a hard-copy of a figure or save it as a file for incorporation in
other documents
- for example this is how the
figures in the lecture slides are done.
|
• The Matlab command
that provides this functionality is print
- print sends the currently active
figure to the printer or saves it to a file
• print supports
many different formats for images
- different printer manufacturers
have different graphics languages
- the most widely supported
(standard) is postscript (for stand-alone documents) and encapsulated
postscript (for figures that will be included)
• For example:
print % Send the current
figure to the
% default printer
print –deps
plotex.eps % Save the current
% figure to
% a file called plotex.eps
% & save as encapsulated
% postscript
26.5 Multiple Figures & sub-plots
• It is possible in
Matlab to create multiple independent figures, and within a figure to have
multiple plots
• By default there
is a single figure window
- created the first time any type
of plotting is done
- over-written with each new plot
(see hold on)
- new figure windows may be created
with the figure(n) where n is an integer value
• if figure n window
already exists it becomes the active window
• all plots go to
the currently active window
• Individual figure
windows can be sub-divided into multiple plots with the subplot() command
- subplot(m,n,p) splits the
current figure into an m-by-n matrix of plots and makes the p'th plot the
active one
- numbering is row-major order
- all plot, label etc. commands
modify the p'th plot only
26.6 Other 2D plots
• While plot is the
most commonly used 2D plot function there are a number of others available:
- log scale
- area
- pie chart
- error
- bar
- histogram
- polar
- stem
- etc.
|
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Email: kursusmatlab@gmail.com • The following script illustrates a number of the different plot functions for a single matrix of data:
- speaker and word recognition
error rates for 16 people speaking the TI Digits database.
|
26.7 Multiple Plots Example
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% multiPlot.m
% An example of the different styles
of 2D
% plots and using multiple plots in
a single
% figure.
|
% Employs the datafile
/home/student/CS1E/speech.dat
% which contains speaker (1st
column) and word
% (2nd column) error rates for 16
speakers on
% the TI Digits speech database.
|
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Email: kursusmatlab@gmail.com % Author: Spike
% Date: 29/3/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Grab speech data
and split into separate
% word & speaker
error rate vectors.
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load
/home/student/CS1E/speech.dat
speaker=speech(:,1);
word=speech(:,2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% First figure:
plot, loglog, pie & pareto
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(1);
subplot(2,2,1);
plot(speaker,word,'*');
xlabel('Speaker
Recognition Error Rate (%)');
ylabel('Word
Recognition Error Rate (%)');
title('Word vs
Speaker Error Rate (by Speaker)');
subplot(2,2,2);
loglog(speaker,word,'*');
grid on;
xlabel('Speaker
Recognition Error Rate (%)');
ylabel('Word
Recognition Error Rate (%)');
title('Log Scale speaker
vs Word Error');
subplot(2,2,3);
grid off;
Multiple Plots
(Cont)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sort both speaker
and word error rates
% (speakers) on the
basis of ascending speaker
% recognition error
rate.
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[speakerS
index]=sort(speaker);
wordS=word(index);
pie(speakerS,speakerS>mean(speakerS));
title('Sorted Pie
Chart of Speaker Recognition Error');
subplot(2,2,4);
pareto(speaker);
xlabel('Index of
Original Order');
title('Pareto
Plot');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2nd figure: bar,
bar3, stairs & hist
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(2);
subplot(2,2,1);
bar(speech);
title('Bar plot of
Speaker & Word Error');
xlabel('Speaker
Number');
ylabel('Error Rate
(%)');
subplot(2,2,2);
bar3(word);
title('Bar3 plot of
Word Error Rate');
xlabel('Speaker
Number');
ylabel('Error Rate
(%)');
subplot(2,2,3);
stairs(speakerS);
title('Stairs Plot
of Speaker Error');
subplot(2,2,4);
hist(word);
xlabel('Word Error
Rate');
ylabel('Count (of
Speakers)');
title('Histogram of
Word Error Rates');
Multiple Plots
(Cont)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 3rd fig: stem,
errorbar, polar & rose
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(3);
subplot(2,2,1);
stem(speaker,word);
title('Stem plot of
Speaker vs. Word Error');
xlabel('Speaker
Error Rate');
ylabel('Word Error
Rate');
subplot(2,2,2);
errorbar(speakerS,wordS);
title('Errorbar of
Speaker with Word Error Variance');
subplot(2,2,3);
polar(speakerS,wordS);
title('Polar plot of
Speaker vs. Word Error');
subplot(2,2,4);
rose(word);
title('Rose plot of
Word Error');
%%%%%%%%%%%%%%%%%%%%%%%%
% 4th figure:
matrixplot
%%%%%%%%%%%%%%%%%%%%%%%%
figure(4);
plotmatrix(speech);
title('Plotmatrix of
speech data matrix');
26.8 2D Plots - Output
2D Plots Output (Cont)
26.9 Adding text
• It is possible to
place text on 2D & 3D plots with the text() function
• Syntax is:
text(location,string)
Consider the
following example:
bar(speaker);
xpos=1:size(speaker,1);
ypos=speaker+0.1;
labels=num2str(round(speaker));
text(xpos,ypos,labels);
26.10 3D Graphics – Line
• The 3D equivalent
of the plot() function is plot3()
- functionality extremely similar
to that of plot()
• Consider the
following example:
theta=-10*pi:0.02:10*pi;
y=sin(theta); z=cos(theta);
plot3(theta,theta.*y,theta.*z);
ylabel(‘x.sin(x)’);
zlabel(‘x.cos(x)’);
title(‘Plot of
x.sin(x) vs. x.cos(x)’);
26.11 3D Graphics – Surface
• Matlab has a
number of functions for visualising 3D surfaces:
- the following example shows four
of the more simple functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pittedDome.m
% Generate a simple pitted dome
% using 2 FOR loops and a random
% function to do the pitting.
|
% Used to show the surface plotting
% features of Matlab.
|
% Author: Spike
% Date: 29/3/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
width=50;
pitted=zeros(width,width);
for length=1:width
for breadth=1:width
pitted(length,breadth)=width*width-((length-width/2)^2+...
|
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Email: kursusmatlab@gmail.com (breadth-width/2)^2) + width*randn/3;
end;
end;
subplot(2,2,1);
surf(pitted);
title('Surf plot');
subplot(2,2,2);
contour(pitted);
title('Contour
plot');
subplot(2,2,3);
pcolor(pitted);
title('Pcolor
plot');
subplot(2,2,4);
surfl(pitted);
shading interp;
26.12 3D Surfaces Example
26.13 Polynomials
• Polynomials are
extremely important in many areas of science and engineering
• Matlab provides
built-in support for their representation and manipulation
- root finding
- reconstruction
- multiplication
- derivative finding
- fitting to data
>> quad=[2 0
-5 12 -73];
>> % y= 2x^4
-5x^2 + 12x -73
>> quadRoots =
roots(quad)
quadRoots =
-2.9527
2.4646
0.2440 + 2.2262i
0.2440 - 2.2262i
>>
quadRebuilt=poly(quadRoots)
quadRebuilt =
1.0000
-0.0000 -2.5000 6.0000
-36.5000
>> square=[1 3
-9]; %y=x^2+3x-9
>>
sixth=conv(quad,square)
sixth =
2
6 -23 -3
8 -327 657
>> % 2x^6 +
6x^5 -23x^4 -3x^3 +8x^2 -327x + 657
>>
cubic=polyder(quad) % Derivative
cubic =
8
0 -10 12
Polynomials (Cont)
>>
xvals=0:0.1:1;
>>
yvals=2*xvals.*xvals-9*xvals+27;
>>
yvals=yvals+randn(size(yvals));
>>
fitted=polyfit(xvals,yvals,2)
fitted =
5.4375
-11.7186 26.9350
>>
xinterp=linspace(0,1,100);
>>
yinterp=polyval(fitted,xinterp);
>>
plot(xvals,yvals,'-o',xinterp,yinterp,'--');
>> xlabel('X
values');
>> ylabel('Y
values');
>> title('Binomial
function with noise & fitted');
26.14 Interpolation & Splines
• Matlab includes
functions for interpolating values between those of a pre-existing set
- 1D, 2D, & multi-dimensional
interpolation
- linear, cubic & cubic spline
interpolation
• Functions
interp1(), interp2(), interpn()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% interpEx.m
% A simple example of 1D
interpolation.
|
%
% Author: Spike
% date: 30/3/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%
% y=2x^2-5x+12
%%%%%%%%%%%%%%
originalX=-2:2:6;
originalY=2*originalX.*originalX-5*originalX+12;
interpX=-2:0.05:6;
cubicY=interp1(originalX,originalY,interpX,'cubic');
splineY=interp1(originalX,originalY,interpX,'spline');
plot(originalX,originalY,'-o',interpX,cubicY,'--',interpX,splineY,':');
title('Interpolation
Example');
Interpolation (Cont)
• Cubic spline
functions also available separate to interp?() functions:
- spline() function fits piecewise
cubic splines between data pairs:
• can return
polynomial co-efficients, function values etc.
|
26.15 Optimisation
• Matlab has a
number of functions for optimising numeric functions:
- finding their zeros
• generalises to
finding where they meet any particular values
- minimisation
+ finding function
peaks and valleys
>>
x=-40:0.1:40;
>>
y=0.2*x.^4+6*x.^3-8*x.^2+7.2*x-10.1-x.^5/200;
>> plot(x,y);
title('Optimisation Example');
>>
minX=fmin('0.2*x.^4+6*x.^3-8*x.^2+7.2*x-10.1-x.^5/200',-30,0)
minX =
-15.8967
>> line([minX
minX],[max(y) min(y)]);
26.16 Integration, Differentiation &
Ordinary Differential Equations
• Matlab implements
functions for the numeric solution of integration, differentiation, and
ordinary differential equation problems
• Details of the
functions and their appropriate usage is beyond the scope of this course
- take a course on numerical
analysis
• Functions include:
Integration: quad(), quad8(), dblquad(), trapz(),
cumtrapz()
Differentiation:
ployfit(), polyder(), diff(), gradient(), del2()
ODEs: ode23(),
ode45(), ode113(), ode23s(), ode15s(), odeset(), odeget(), odefile()
26.17 Data-structures
• Matlab provides
limited support for more complex data-structures
- quite limited and weak support
compared to most general purpose 3rd generation programming languages
• Cell Array
- An array, whose individual
elements can be any type or size (including other cell arrays)
>>
cellEx{1,1}=[1 2; 3 4];
>>
cellEx{1,2}='1st row, 2nd column';
>>
cellEx{2,1}=6.4-5.1i;
>>
cellEx{2,2}=eye(3);
>> cellEx
cellEx =
[2x2 double] '1st row, 2nd column'
[6.4000- 5.1000i] [3x3 double]
>>
celldisp(cellEx);
cellEx{1,1} =
1
2
3
4
cellEx{2,1} =
6.4000 - 5.1000i
cellEx{1,2} =
1st row, 2nd column
cellEx{2,2} =
1
0 0
0
1 0
0
0 1
Data-Structures
(Cont)
• Structures
- A collection of (usually)
disparate types of information, each with a name
>>
unit.name='Sherman Tank';
>>
unit.attack=20;
>>
unit.armour=12;
>>
unit.health=30;
>>
unit(2).name='Alpha Squad';
>>
unit(2).attack=14;
>>
unit(2).armour=0;
>>
unit(2).health=20;
>> unit
unit =
1x2 struct array
with fields:
name
attack
armour
health
>> unit(1)
ans =
name: 'Sherman Tank'
attack: 20
armour: 12
health: 30
>>
fieldnames(unit)
ans =
'name'
'attack'
'armour'
'health'
26.18 Object Oriented
• Recent versions of
Matlab have supported object oriented programming:
- classes
- objects
- methods
- inheritance
- aggregation
• Unfortunately the
hybrid design of procedural syntax with tacked-on objects leads to a number
of difficulties
- method invocation (precedence
rules rather than explicit user indication of object that invokes)
- requirement to explicitly list
other classes in constructor’s superiorto(), inferiorto() calls means forward compatibility with new
classes is an on-going problem
26.19 GUI Design
• Matlab provides
sophisticated tools for building GUIs
- for instance the Matlab demos
which were constructed with Matlab’s GUI functions
• GUI – Graphical
User Interface
• Provides support
for:
- menus
- text boxes
- buttons
- sliders
- figures
- typefaces
- pointer & mouse events
- dialog boxes
• Also has a
built-in set of constructor tools to allow the user to visually design the
GUI
26.20 Toolboxes
• Matlab’s
functionality may be expanded modularly with the addition of toolboxes
• Toolboxes are a
suite of functions related to a specific problem area
- signal processing
- image processing
- neural networks
- communications
- fuzzy logic
- wavelets
- optimisation
- splines
- etc.
|
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB ONLINE - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Format bimbingannya tugas-tugas yang bisa membantu Skripsi, Tesis, DISERTASI
Bimbingan dilakukan secara online bisa lewat WA atau email Dijamin Bisa, atau bisa mengulang kembali. Kami juga dapat membantumembuatkan aplikasi atau program matlab/lainnya. Anda akan dilatih oleh Tim Profesional - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 081219449060. Email: kursusmatlab@gmail.com |
KURSUS MATLAB ONLINE Skripsi, Tesis, DISERTASI 081219449060
Selasa, 13 Januari 2015
GRAPHICS & OTHER MATLAB FEATURES KURSUS MATLAB ONLINE Skripsi, Tesis, DISERTASI 081219449060
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar