Selasa, 13 Januari 2015

Save all Matlab output in the file fname until a diary off command is


home                    Return the cursor to the top left corner
more                    Force output to be paginated (presented a page at a time)
diary <fname>    Save all Matlab output in the file fname until a diary off command is executed. Useful for recording testing results and saving program output.
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 0822 9988 2015.  Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Kami memberikan jaminan dan garansi dilatih hingga bisa dengan biaya 4-6 juta selama 10 kali pertemuan. 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) 0822 9988 2015.   Email: kursusmatlab@gmail.com
echo                     Echo to the screen each line as it is executed until an echo off command is received. Useful for tracing and debugging but generally confusing for normal program execution.
14.8       A 2nd Example
Consider the problem of generating artificial 3D terrain. Such terrian might be used in virtual reality simulations (e.g., wargames), to model the interaction of terrain and species spread etc.

Matlab has powerful 3D graphing tools including tools for generating 3D surface plots.
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 0822 9988 2015.  Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Kami memberikan jaminan dan garansi dilatih hingga bisa dengan biaya 4-6 juta selama 10 kali pertemuan. 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) 0822 9988 2015.   Email: kursusmatlab@gmail.com

The terrain can be represented as a 2D grid (x and y co-ordinates) with the height at that point being saved. Hence a 2D matrix is our basic numeric representation of the terrain.

How do we generate “realistic” terrain? Very difficult due to the many agents responsible (e.g., erosion, tectonic forces, temperature extremes) for shaping the terrain. We will accept an extremely simple model that:
• starts from an initial random configuration of peaks and valleys and
• applies a simple model of erosion that smoothes adjacent contour features together.
• simple models for meteor impacts and the creation of plateaus.
14.8.1    Final Approach
• A set of user functions that perform the following tasks
-              create an initial random map configuration
-              smooth/erode/melt an existing map
-              add a meteor impact to an existing map
-              add a plateau to an existing map
14.9       Map Generation
>> help mapGen
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  MAPGEN   Generate a random map (grid of elevation data)
    [X,Y,Z] = mapGen()  - Generate a 50x50 grid of elevation data
    [X,Y,Z] = mapGen(N) - Generate an NxN grid of elevation data
    [X,Y,Z] = mapGen(N,M) - Generate an NxM grid of elevation data
 >> [X Y Z0] = mapGen(60);
>> surfl(X,Y,Z0);
>> shading interp
>> colormap copper
>> axis off
>> print -deps map0.ps

14.10     Map Smoothing
  >> Z40 = mapSmooth(Z0,40);
  >> surfl(X,Y,Z40);  shading interp; axis off

>> Z41 = mapPlateau(Z40);
>> Z51 = mapSmooth(Z41,10,0.2);
>> surfl(X,Y,Z51); axis off; shading interp; 

14.11     Map Code
• A quick look at some of the code:
14.11.1 mapGen
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % MAPGEN   Generate a random map (grid of elevation data)
%   [X,Y,Z] = mapGen()  - Generate a 50x50 grid of elevation data
%   [X,Y,Z] = mapGen(N) - Generate an NxN grid of elevation data
%   [X,Y,Z] = mapGen(N,M) - Generate an NxM grid of elevation data
%
% Author: Spike
% Date:   11/10/98
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [X, Y, Z] = mapGen(xSize,ySize)
% Provide a default size if none given
if nargin==0
   xSize=50;
   ySize=50;
% Else if a square map then set both dimensions
elseif nargin==1
   ySize=xSize;
end;
% Create x and y arrays (vectors)
x=linspace(0,1,xSize);
y=linspace(0,1,ySize);
[X Y]=meshgrid(x,y);  % Make a grid of all x & y
% The "real" work. Create a set of heights for t
% the map as a matrix of size xSize by ySize.
% Heights are derived from a uniform
% distribution with mean 0 and
% std. dev. of 1. Z=randn(xSize,ySize);

Map Code (Cont)
14.11.2 MapPlateau
% Comments removed  function newMap = mapPlateau(oMap,radius,height,xCentre,yCentre)
% Build the map to hold the new heights
newMap=zeros(size(oMap,1),size(oMap,2));
% We don't have an acceptable radius.
% 10% of the length of the shortest side of the
% map.
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 0822 9988 2015.  Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Kami memberikan jaminan dan garansi dilatih hingga bisa dengan biaya 4-6 juta selama 10 kali pertemuan. 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) 0822 9988 2015.   Email: kursusmatlab@gmail.com
if (nargin==1 | radius>min(size(oMap,1),size(oMap,2)));
   radius=min(size(oMap,1),size(oMap,2))* (10.0+randn)/100.0;
end;
% We don't have a supplied height. Generate one
% that is roughly the same as the current
% highest peak.
if (nargin<3)
   height=max(max(oMap));
   height=height*(100.0+2*randn)/100.0;
end;
% No centres provided, or not valid centre.
% Generate a random centre for the plateau.
if (nargin<5|xCentre<1|xCentre>size(oMap,2)|yCentre<1|yCentre>size(oMap,1))
   xCentre=round(rand*size(oMap,2));
   yCentre=round(rand*size(oMap,1));
end;
% xVals=1:size(oMap,2);
% yVals=1:size(oMap,1);

Map Code (Cont)
% For each point on the surface determine
% whether it is now part of the plateau or not.
% If it is then set its height to be height. If
% it isn't then preserve the old height.
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 0822 9988 2015.  Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Kami memberikan jaminan dan garansi dilatih hingga bisa dengan biaya 4-6 juta selama 10 kali pertemuan. 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) 0822 9988 2015.   Email: kursusmatlab@gmail.com
% NOTE: There should be a "cleaner" way of doing
% this without loops (such as the find()
% function) but the calculate of inside or
% outside the circle [needs to be done for i & j
% values simultaneously] seems to preclude such
% an approach.
for iIndex=1:size(oMap,2)
   for jIndex=1:size(oMap,1)
      if ((iIndex-xCentre)^2+(jIndex-yCentre)^2<=radius^2)
        newMap(jIndex,iIndex)=height* (100.0+randn)/100.0;
      else
         newMap(jIndex,iIndex)=oMap(jIndex,iIndex);
      end;
   end;
end;
  
14.12     Principles of  “Good” Programs
• meet specification
-              verifiable
-              dependable
• natural
-              abstraction
-              modularisation
-              encapsulation
¨• understandable
-              maintainable
-              portable
• efficient
• “elegant”
14.13     Review
• Matlab background
- history
• Chief features of Matlab
• Driving Matlab
-              running Matlab
-              help & demos
-              controlling the command environment
• Examples of Matlab use
• Principles of good programming
15           MATLAB SYNTAX
• All programming languages have their own syntax
- rules for using the language
• Expressions are the statements that compose a program
- effectively the “lines” of a program
• Today’s lecture examines:
- The rules of Matlab syntax
- Assignment
- Commenting
- Useful Matlab functions
- Script M-Files
References:       
                                             For Engineers (Ch. 1)
                                             User’s Guide (Ch.1, 2, 4, 24)
                                             Mastering (Ch. 2, 4, Appendix A)
15.1       Some Simple Calculations
• Matlab capable of simple mathematical operations analogous to a calculator:
>> 9.3 + 5.6
ans =
   14.9000
>> 13.1 - 4.113
ans =
    8.9870
>> 10.1 * 890.99
ans =
   8.9990e+03
>> 9.6 / 3.2
ans =
    3.0000
>> 9.9 ^ 3.1
ans =
   1.2203e+03
>> diary off
15.2       Basic Mathematical Operators
• The following basic mathematical operators are supported by Matlab:
Operation            Symbol
Addition (a + b)   +
Subtraction (a-b)              
-Multiplication (a . b)       *
Division (a  b)    / or \
Exponentiation (ab)         ^
15.3       Expressions
• Single lines (calculations or commands) of Matlab have one of two general forms:
                              variable = expression
y=sin(3.14);
z=(y*4.6)^12.9 – 13.456;
or
                              expression
plot(x,y,’-+’);
z^3.5
• Where the expressions are a combination of:
- mathematical & other built-in operators
- variables
- built-in function names
- user defined functions
- bracketing for precedence
15.4       Variables & Assignment
• Often we wish to perform a calculation and save a copy of the result
• general form:
                              <variable> = <calculation>
e.g.,  resultInPercent = labOutOf50 + examOutOf50;
=                            The assignment operator
- read as: "becomes equal to”
- transfers the results of the calculation on the right-hand side to the variable on the left-hand side.
• For instance
resultInPercent = labOutOf50 + examOutOf50;
- the variable resultInPercent becomes equal to the sum of the values stored in labOutOf50 and examOutOf50

15.5       The ans variable
• The results of calculations do not need to be saved to a variable explicitly.
• If no variable is specified then the result is automatically saved to the ans (answer) variable.
- This variable may be subsequently used
e.g.,
>> 91.3 – 14
ans =
    77.3
>> z=ans * 2
z =
   144.6
• In general you should not use ans but your own meaningfully named variables
15.5.1    Examining a Variable’sValue
• Simply typing a variable’s name alone is interpreted as a command to show the value stored in that variable
e.g.,
>>z
z =
   144.6
15.6       Semicolon, Comma & Period
• By default Matlab interprets the end of a line as the end of a statement/expression
15.6.1    Semicolon
• Semicolon ;  terminates the current expression and suppresses output (to the screen) of the result
e.g.,  volume = pi * radius^2 * height;
The value is calculated and stored in volume but not echoed back to the screen.
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 0822 9988 2015.  Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Kami memberikan jaminan dan garansi dilatih hingga bisa dengan biaya 4-6 juta selama 10 kali pertemuan. 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) 0822 9988 2015.   Email: kursusmatlab@gmail.com
• A semicolon should be used on most lines of code as we are not interested in intermediary results
15.6.2    Comma
• The comma , can be used to separate multiple statements on the same line. The value of any variables will be echoed to the screen.

e.g., x=5.7, y=89.12
will echo those variables and their names back on the screen.
15.6.3    Period
• Long statements can be split over lines by 3 periods
e.g., incomeInHand = salary – tax – unionFee …
                                             - superAnnuation;
15.7       Layout Conventions
• Very little restriction on layout in Matlab programs
• However common layout conventions make programs easier to…
- read
- understand
- maintain
• Basic conventions:
- one statement (on thought) per line
- indent lines to show different parts of program
- blank lines separate different parts of program
- comments help readers (including the author!) understand the program
- break long lines into readable segments
15.8       Comments
• Matlab’s comment character is the percent-symbol %
- remainder of the line past the % is ignored by the computer
• Comments are for the humans:
- help us understand what is occurring in the program
• Good comments:
- provide information not immediately obvious
- described the intended effect of the portion of code to which they refer
-              conform to usual conventions of prose
-              are always correct and up to date
- are clearly distinguishable from the surrounding code (well set apart)
15.9       Using Comments
• A minimum set of comments in any program should include:
- the name of the program
- the program's purpose (what it does)
- who wrote the program and when
- any underlying assumptions made during coding
- description of any (non-obvious) variables
- description of the purpose of each major subsection of the program:
• important formula
• functions
• major loops and control structures
15.10     Comments & Matlab Help
• Matlab adopts a convention in which comments serve to document individual programs and functions:
- when help is invoked on a script file or function all comment lines up to the first line of code or a blank line are shown as help for that function/script.
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 0822 9988 2015.  Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Kami memberikan jaminan dan garansi dilatih hingga bisa dengan biaya 4-6 juta selama 10 kali pertemuan. 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) 0822 9988 2015.   Email: kursusmatlab@gmail.com
The Script File helpex.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% helpex.m
%            An example of the way Matlab
%            uses comments and help to
%            document a script. If you type
%            help helpex you will see all of
%            these comments up till the first
%            blank line below.
% Author: Spike
% Date: 11/2/1999
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This comment won't be seen
x=9.1;    % Do something trivial
Running Matlab
>> help helpex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  helpex.m
               An example of the way Matlab
               uses comments and help to
               document a script. If you type
               help helpex you will see all of
               these comments up till the first
               blank line below.
  Author: Spike
  Date: 11/2/1999
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15.11     Some useful Mathematical Functions
• Matlab contains literally hundreds of built-in functions
Name    Function
abs(x)    Absolute value
acos(x)  Inverse cosine
acosh(x)               Inverse hyperbolic cosine
angle(x) Phrase angle
asin(x)   Inverse sine
asinh(x) Inverse hyperbolic sine
atan(x)  Inverse tangent
atanh(x)               Inverse Hyperbolic tangent
ceil(x)    Round towards +infinity
conj(x)   Complex conjugate
cos(x)    Cosine
cosh(x)  Hyperbolic cosine
exp(x)    Exponentiation e^x
fix(x)      Round towards zero
floor(x)  Round towards –infinity
gcd(x,y) Greatest common divisor
imag(x)  Complex imaginary part
lcm(x,y) Least common multiple
log(x)     Natural logarithm
log10(x)               Common (base 10) logarithm
real(x)    Complex real part
rem(x,y)               Remainder after division
round(x)               Round towards nearest int
sign(x)   Sign
sin(x)      Sine
sinh(x)   Hyperbolic sine
sqrt(x)    Square root
tan(x)     Tangent
tanh(x)  Hyperbolic tangent
 From Version 5.0 User’s Guide, Prentice Hall, 1998.

e.g.,
               distance = sqrt((x1-x2)^2+(y1-y2)^2);
               height = velocity*sin(launchAngle)*time - …
                                             0.5*gravity*time*time;
15.12     Script M-Files
• Re-entering the same program (set of instructions) multiple times is wasteful
-              ideally need a means of creating once and re-using as required
Script (M-Files) are Matlab's mechanism for this
• We will use these extensively
15.12.1 Basic Approach using M-Files
1.            Create m-file with a text editor (or Matlab's built-in Script editor).
2.            In Matlab command window enter name of script file (e.g., if file called example1.m then enter example1) to run it.
Kami ada di Jakarta Selatan. KAMI MEMBERIKAN KURSUS MATLAB - HUBUNGI MASTER ENGINEERING EXPERT (MEE) 0822 9988 2015.  Kami membuka kursus Matlab untuk pemula dan mahasiswa atau insinyur yang ingin memperdalam Matlab dan menerapkan dalam bidang teknikal, engineering, rekayasa, dsb. Kami memberikan jaminan dan garansi dilatih hingga bisa dengan biaya 4-6 juta selama 10 kali pertemuan. 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) 0822 9988 2015.   Email: kursusmatlab@gmail.com
3.            While still errors in the script
3.1         Modify & save script using editor
3.2         Rerun script in Matlab
15.12.2 Naming
• M-files (scripts) must all have the suffix ".m"
-              When a script name is entered Matlab "tacks" a ".m" on the end and looks for a file of that name
e.g.,  >> amoeba
  Matlab searches for a file called amoeba.m
15.13     Matlab Name Space
• Matlab employs the following rules in resolving an identifier entered by the user (e.g.,  aristotle):
1.            Looks for a variable (aristotle) in the current workspace…if that is not found…
2.            Looks for a built-in function (aristotle())…if that is not found…
3.            Looks for a script/m-file (aristotle.m) in the Matlab's current directory (generally the one it was started in)…if that is not found…
4.            Looks for a script/m-file (aristotle.m) in Matlab's search path (in the order listed in the search path)
15.13.1 Matlab Search Path
• If an M-file can not be found by Matlab there are two possible solutions:
-              In Matlab command window cd to directory containing M-file
-              Add the directory containing the M-file to Matlab's search path:
• addpath command
• path browser (GUI interface)
• useful approach when several related M-files
15.14     Review
• Matlab as calculator
• Expressions in Matlab
• Variables & assignment
- assignment operator
- default variable (ans)
• Separators
- comma; seni-colon; 3-periods
• Layout conventions & comments
• Script M-files
16           MATLAB VARIABLES & DATA TYPES
• Variables & their usage form a vital part of problem solving in any language:
-              "containers" for user supplied values
-              intermediary working values
-              final results
• Each programming language has its own syntax (rules) regarding variables:
-              declaration
-              valid identifiers
• Different languages support different basic data types and have their own rules regarding the different types
-              integer, reals, strings etc.

Tidak ada komentar:

Posting Komentar