Selasa, 13 Januari 2015

KURSUS MATLAB ONLINE Skripsi, Tesis, DISERTASI 081219449060


  0.5*gravity*seconds.^2;
fprintf('shell3 took %6.2f cpu seconds to complete\n',cputime-before);
toc;
23.21     Review
• Numeric limitations
- potential errors due to limitations
• Debugging in Matlab
• Script profiling in Matlab
• Order of an algorithm
• Timing functions in Matlab
• Guidelines for writing efficient code
• Looping versus implicit vectorisation
24           BASICS OF MATLAB FUNCTIONS
• Functions are a vital part of all procedural languages
-              issues of re-use, cohesion, & coupling
• Much of Matlab’s power focuses around the built-in functions
• Matlab allows users to write and employ their own functions
-              encourages modularity & re-use
• Today’s lecture is the 1st in a series of two concerning functions in Matlab. Topics covered include:
-              design principles for functions
-              function syntax & calling
-              parameters & returned values
References:        For Engineers (Ch. 1, 5)
                                                            Student (Ch. 12, 24)
                                                            Mastering (Ch.14, Appendix A)
24.1       Motivation
Modularisation is one of the key techniques employed in software design and engineering
-              splitting code into "small", relatively self-contained portions of code
Modularisation has the advantages of:
-              complexity control
-              re-usability
Different programming languages support modularisation to varying degrees
-              Matlab's support is rather weak
A common mechanism supported by most languages is that of user-defined functions (and/or procedures)
- named blocks of code that can be invoked (called) and which can communicate values to one another
24.2       Calling Functions
• Matlab's power draws from the many in-built functions
-              we have employed functions in our earliest examples
• Usage closely resembles the application of mathematical functions (e.g., y=f(x))
• General form:
[out1, out2,…,outn] = funcName(arg1,arg2,…,argm)
A single return value is common:
               result = funcName(arg1,arg2,…,argm)
Parameters are not mandatory:
               result = funcName
• Examples:
EDU» ex=[98 34 45 78 65 111 -20];
EDU» [sorted indexes]=sort(ex);
EDU» sorted
sorted =
   -20    34    45    65    78    98   111
EDU» indexes
indexes =
     7     2     3     5     4     1     6
EDU» fprintf('Mean=%5.1f, median=%d\n',mean(ex),median(ex));
Mean= 58.7, median=65
24.3       Overview of Function Call & Return Process
Function Call
1.            Save location in currently executing script
2.            Transfer to called function
a)            associate parameters with passed values
b)            start execution at first statement in function m-file
Function Return
1.            Finalise execution of function
a)            association output values with caller's variables
b)            dispose of function's workspace
2.            Transfer execution back to saved location of calling script
24.4       On Matlab's In-Built Functions
• Standard Matlab (without additional ToolBoxes) contains approximately 1,000 built-in functions
-              See Appendix of texts for detailed listings
• Functions fall into a number of categories including:
-              mathematical functions
-              linear algebra functions
-              data analysis & fourier transforms
-              interpolation, polynomial, integration and differentiation functions
-              2D & 3D graphics functions
-              GUI functions
-              String & I/O functions
-              data structure related functions
-              help & configuration functions
24.5       What Makes a Good Function – Software Engineering Principles
• How does a programmer decide what should and shouldn't be a function?
• Several principles of good software engineering practice should be used as guidelines:
cohesion: The function should perform a logical, self-contained operation. High cohesion is good.
coupling: The amount of inter-connection, duplication etc. between different functions. The inter-dependence of functions. Coupling should be minimised.
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

re-use: The degree to which a function may be applied to later programming tasks. High re-usability is good.
understandable: Measure of ability of a human to understand the function. High understandability is good.
• In general write relatively small, self-contained functions that perform a single logical task.
24.6       Function Declaration Syntax
• As for script files, functions are written using a standard (or Matlab's in-built) editor and saved in .m files
• The file name holding a function and the function name should match
-e.g., a function myFunction should be stored in the file myFunction.m
-              in reality Matlab checks the file name
• General syntax
function [fov1, fov2,…]=functionName(fa1,fa2,…)
<statement>
               :
<statement>
• First line declares the function
-              starts with reserved word function
-              followed by formal output variables (outputs of the function)
-              name of function (functionName)
-              followed by formal arguments (inputs to the function) in brackets
• Remaining lines are code that is executed
24.7       A First Function
In the file cube.m
% CUBE() - FIND THE CUBE OF A VALUE
%
% y=cube(z)
%
% A simple example of the syntax of
% a function with a single input argument
% (x), and a single output variable (y).
% Supports scalar and vector arguments.
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:   21/3/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = cube(x)
y = x.*x.*x;
In the Command Window
EDU» a=[1 2 3];
EDU» cube(a);
EDU» result=cube(a)
result =
     1     8    27
In Command Window
EDU» contrast
Power:   0.32
Function:   1.59
Multiplication:   0.29
24.8       Example: Timing of Functions
In Script m-file contrast.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%contrast.m
% Contrast execution time for calculating a
% cube when employing the raise operator,
% versus straight multiplication (but in a
% function).
% Author: Spike
% Date:   21/3/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
before=cputime;
for ii=1:100
   for jj=1:100
      num=jj^3;
   end;
end;
afterPower=cputime;
for ii=1:100
   for jj=1:100
      num=cube(jj);
   end;
end;
afterFunction=cputime;
for ii=1:100
   for jj=1:100
      num=jj*jj*jj;
   end;
end;
afterMult=cputime;
fprintf('Power: %6.2f\n',afterPower-before);
fprintf('Function: %6.2f\n',afterFunction-afterPower);
fprintf('Multiplication: %6.2f\n',afterMult-afterFunction);
24.9       Comments & Functions
• Matlab uses the first continuous block of comments in an M-file as the help text for that function or script
EDU» help cube
  CUBE() - FIND THE CUBE OF A VALUE

  y=cube(z)
  A simple example of the syntax of
  a function with a single input argument
  (x), and a single output variable (y).
  Author: Spike
  Date:   21/3/1999
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
• Help comment should contain:
-              name of function
-              task performed by function
-              calling methods
-              author & date of creation
-              any other necessary information
24.10     Top-Down View of Function Design
• An approach to designing functions
• Continuously decompose problem into sub-task
-              each task matches a function
-              task can be further sub-divided into sub-tasks
• hence functions can be split into sub-functions

24.11     Bottom-Up View of Functions
• Another approach to design of functions
-              opposite of top-down view
-              however not mutually exclusive
• often for large problems both approaches taken in concert (attacking from both ends)
• Determine fundamental tasks useful for current or any problem
-              functions become fundamental tasks
-              functions are combined to perform more powerful operations
• For example:
getArray(fname) – Get an array stored in a file
make2Dplot(data,labels) – Make a 2D plot of the
                                                            data using the supplied labels
linearSolve(A,B) – Solve the linear equation
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

24.12     The Black Box Paradigm
• One powerful and useful view of functions is that known as black-box
-              caller's perspective
-              however a guideline in designing a function so that it fits the paradigm
• The function itself is considered as a black-box
-              task the function performs is known
-              means of performing the task is unknown (unknowable)
-              caller requests the action performed by invoking the function using its name
-              caller provides data for function to operate upon as arguments (parameters) to the function
-              function (calle) communicates results of tasks as outputs to the caller

24.13     Parameters – Input Channel
• Caller of a function must provide data for function to operate upon
-              without supplied (new/different) data the function always performs exactly the same task
• Parameters are the values supplied by the caller to the function
-              also called arguments
• In Matlab parameters
-              are supplied immediately after the function call
-              are in a bracketed, comma separated list
-              do not propagate any changes made (to them) to the caller’s variables
• Parameters are vital in ensuring the cohesion and re-use of functions:
-              function needs no external knowledge other than its specialised task
-              has no "knowledge" of how it is used by caller
-              all the function needs to perform the task is supplied explicitly as parameters
24.14     Returned Values – Output Channel
• Caller of a function generally expects results of the operation requested
-              without returned values the caller can't employ the function as part of some larger whole (task)
• Returned values are the means by which Matlab functions communicate their results to the caller
• In Matlab the returned values are accessed by treating the function call similar to an expression
e.g.,
               [sorted index] = sort(values);
               average = mean(values);
• Returned values support both cohesion and re-use goals for functions
-              function computes values that complete a task without regard for how those values will be used
+ no need for knowledge of larger context
24.15     Example – Description
A program is needed to read and summarise the temperatures recorded at a locale in North America.
The temperatures are available on an hourly basis and kept in a file, measured in fahrenheit.

The program is to summarise the data by producing both a table and plot of the temperatures in celcius for a period of 24 hours.

The temperature at mid-day should also be highlighted.

Possible Structure

24.16     Example Results
>> dailyTemps
Please input the name of the file containing the temperatures daily.dat
 Hour:   0100  0200  0300  0400  0500 … 2400
 Temp:    2.2   0.6  -0.6  -4.4  -7.2 …  2.8
 The temperature at mid-day is 22.8 celcius
>> diary off

24.17     Example – dailyTemps
% dailyTemps - PROCESS DAILY TEMPERATURES
%
%            A script file (main program) for
%            processing a set of temperatures.
%            Reads a set of temperatures in
%            fahrenheit from a file, converts
%            them to celcius and produces a
%            table and graph of the result.
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

%            Employs the following functions:
%                           getTemps()
%                           processTemps()
%                           tempTable()
%                           tempPlot()
%            This script and the accompanying
%            functions are examples for the
%            Computer Tools lecture on functions.
%
% Author: Spike
% Date:   22/3/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fahren=getTemps;
if ~isempty(fahren)
  [celcius midday] = processTemps(fahren);
  tempTable(celcius,midday);
  tempPlot(celcius);
end;
24.18     Example – getTemps
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% getTemps - READ TEMPERATURES FROM A FILE
%
%  temps = getTemps
%            Prompts the user for a filename and reads
%            in the temperatures from that file,
%            returning them as a vector to the caller.
% Author: Spike
% Date:   22/3/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function temps = getTemps
fname = input('Please input the name of the file containing the temperatures ','s');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Open file for reading. If can't
% open the print warning & return
% an empty array
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid = fopen(fname,'r');
if fid==-1
  warning('Unable to open file');
  temps=[];
  return;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read the values from the file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
temps=fscanf(fid,'%f');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ensure we have a row vector
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if size(temps,1)>size(temps,2)
  temps=temps';
end;
24.19     Example – processTemps
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% processTemps - PROCESS DAILY TEMPERATURES
%
%            Convert daily temperatures in fahrenheit into
%            celcius and return the temperature at midday
% 
%  [celcius midday] = processTemps(fahrenheit)
%
%            If more than 24-hours worth of temperatures
%            are received then only the first 24 are used.
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

%            If there is no valid mid-day temperature then
%            an empty value is returned.
%
%            In addition the temperatures in Celcius are
%            rounded to one decimal place.
% Author: Spike
% Date:   22/3/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [cel, midday] = processTemps(fah)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ensure there is data to work with
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isempty(fah)
  cel=[];
  midday=[];
  warning('Empty vector of temperatures');
  return;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Handle cases where vector has more or
% less than 24 elements. dayEnd is the
% actual number of elements that will
% be converted.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Example – processTemps (Cont)
dayEnd=min(24,size(fah,2));
if dayEnd~=size(fah,2)
  warning('Received vector does not contain 24 values');
  fprintf('Using %d of the %d received\n',dayEnd,size(fah,2));
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Convert from Fahrenheit to celcius. The
% formula is c=(f-32)*5/9. However as we are
% rounding to one decimal place we multiply by
% 50 rather than 5 (i.e., shift 1 decimal place
% to the left), round off, then shift back to
% the right (divide by 10
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cel(1:dayEnd) = round((fah(1:dayEnd)-32)*50/9)/10;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Find the temp at midday ensuring that the vector
% has at least 12 elements
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if dayEnd>=12
  midday=cel(12);
else
  midday=[];
  warning('Unable to find temperature at midday');
end;
24.20     Example – tempTable
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% tempTable - PRODUCE A TABLE OF DAILY TEMPS
%
%  tempTable(temps)
%            Produce a table of the supplied temperatures
%
%  tempTable(temps,midday)
%            Produce a table with a line explicitly
%            noting the temperature at midday
%
%            Uses the sprintf function to produce
%            hours in the military format.
% Author: Spike
% Date:   22/3/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function tempTable(temps,midday)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Make sure there are temps to process
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isempty(temps)
  warning('No temperatures supplied');
  return;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Produce the hours in a table in
% military format. Done in a for
% loop due to difficulties with
% implicit vectorisation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('\n\n Hour:  ');
for hour=1:size(temps,2)
  fprintf(' %02d00 ',hour);
end;

Example – tempTable (Cont)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Output the temps in a table to
% 1 decimal place.
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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf('\n Temp: ');
fprintf('%6.1f',temps);
fprintf('\n');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% produce a message about temp at
% midday if appropriate.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin==2 & ~isempty(midday)
  fprintf('\n The temperature at mid-day is %.1f celcius\n',midday(1));
end;
24.21     Example – tempPlot
% tempPlot - PRODUCE A PLOT OF TEMPERATURES
%
%   tempPlot(temps)
%            Produces a 2D linegraph plot of temperature
%            versus hour of the day.
%            Will highlight the temperature at mid-day.
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

Tidak ada komentar:

Posting Komentar