Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Hong Kong Polytechnic University Industrial Centre 2008 1 MatLAB Lesson 1 : Overview & Environment Edward Cheung Room W311g.

Similar presentations


Presentation on theme: "The Hong Kong Polytechnic University Industrial Centre 2008 1 MatLAB Lesson 1 : Overview & Environment Edward Cheung Room W311g."— Presentation transcript:

1 The Hong Kong Polytechnic University Industrial Centre 2008 1 MatLAB Lesson 1 : Overview & Environment Edward Cheung email: icec@polyu.edu.hk Room W311g

2 2 Introduction MatLAB stands for Matrix Laboratory A Scientific calculator A programming language Good in Matrix calculation and graph plotting written in Fortran and rewritten in C  common programming languages in Science & Engineering  MatLAB becomes a standard tool for scientists & engineers Other computational tools; Maple, Mathematica, MathCad Only Minor change since MatLAB 6 Latest version is MatLab ver.7 (R2008a/b)

3 3 What we’ll cover?

4 4 Start Up – UI, Windows & Environment Interactive, interpretative & programmable Command, command history & workspace window Transfer any command from the command history window to the command window by double clicking (also executes the command) or by click & drag Launch Pad shows all available products and launch them Workspace window keep track the variables defined in the programme MatLAB 7 provided more information on variable like size, bytes, Max, Min, etc. in addition to Name, Value and Class (right click at the Workspace Browser) The default data type use in calculation is double precision floating point numbers

5 5 Workspace Browser Data is not save on exit unless execute “save Workspace As” from the File Menu Or using the save function to save all to a MAT-file >>save % by default, stores all workspace variables in a binary format in the current directory in a file named matlab.mat Can also save in ASCII format (readable but larger in size) >> Save filename.dat {variables} –ascii >> help save

6 6 Some keys Arrow key to run previous command clear screen of command window while keeping history >> clc >> clear % delete all variables  Command History Window can be cleared from Edit Menu Who & whos  give variable information in the command window Double clicking on a variable listed in the workspace window launches a document window called  the array editor A semicolon (;) at the end of a command will suppress the output, try >>x=9; x=x+9

7 7 Keywords You can redefine keywords >iskeyword % list reserved names You can redefine function as variables Example >> sin=3; >> sin(90) ??? Index exceeds matrix dimensions. >> which sin % check if sin is a function sin is a variable. >> clear sin % reset sin back to a function >> which sin % check again sin is a built-in function. Most easy overlook is redefining i & j

8 8 Helpful Commands >> Help % give help topics >> Lookfor %Search all M-files for keyword Try >> help format >> format compact % no linefeed >> format loose % default with linefeed >> format long % fixed-pt 15 digits >> format rat % rational format >> format blank % 2 decimal places  e.g. Get pi with the above format No matter what format is selected, calculation are performed using double precision floating point numbers. >> format % reset to format default

9 9 Variables Variables are created with >> variable = expression Type of variables  Scalars  Vectors  Matrices  Strings

10 10 Variables All name must starts with a letter No special character except underscore, case sensitive X≠x Convention – lowercase for variable & uppercase for constants but there are exceptions (eg speed of light=c) MatLab 6 max length 31 characters  Namelengthmax = 63 (ver. 7 function) isvariablename – check the validity of variable name (boolean T=1; F=0), for example:- >> isvarname polyu >> isvarname poly-u >> isvarname poly_u

11 11 Scalar Variables A variable with one row and one column Creating scalars >> A=1; %creates a scaler >> B=1:10; % creates a vector >> C=[1,2;3,4]; creates a 2D matrix end can be use to terminate a function or indicate last array index >> iskeyword end ans = 1 >> x=[2 3 4]; >> x(end+2)=7 x = 2 3 4 0 7

12 12 Scalar Operations >> x=5; >> y=3; >> z=x+y; >> s=x-y; >> a=x*y; >> d=x/y; >> d=y\x; >> p=x^y;

13 13 Vector A vector can be a column matrix (nx1)or row matrix (1xn) Helpful function in vector creation Ones() & Zeros() >> x=ones(1,5) x = 1 1 1 1 1 >> x=zeros(5,1) x = 0

14 14 Create vector with scaling between elements >> x=linspace(1,5,5) x = 1 2 3 4 5 >> y=logspace(1,5,5) y = 10 100 1000 10000 100000 >> y(3) % addressing vector elements ans = 1000 >> x(7)=-9 % increase the size of a vector x = 1 2 3 4 5 0 -9 Array operators: Dot multiply or dot star.* -array multiplication dot divide or dot slash./ -array division Dot power or dot caret.^ -array power

15 15 The colon operator Create vector from a matrix >>x= xbegin:dx:xend Xbegin & xend = begin & end value of x dx = optional increment (default is 1) Example:- >> x=1.1:5.1 x = 1.1000 2.1000 3.1000 4.1000 5.1000 Transpose operator creates a column vector >> x=(1:5)' x = 1 2 3 4 5 >> b=0:2:10 % a vector starts at 0 ended at 10 with an increment 2

16 16 Matrices In MatLab, a matrix is a variable with more than 1 row and 1 column  Variables with a dimension 1x1 is a scalar Example:- create a 2x2 matrix and assigned to variable A >> A=[1 2;3 4] A = 1 2 3 4 (looks like a table) >> A=[1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 >> A(2,3) ans = 6

17 17 Matrix Sum >> lookfor sum TRACE Sum of diagonal elements. CUMSUM Cumulative sum of elements. SUM Sum of elements. SUMMER Shades of green and yellow colormap. ………. >> help sum SUM Sum of elements. For vectors, SUM(X) is the sum of the elements of X. For matrices, SUM(X) is a row vector with the sum over each column. ………

18 18 String >> a='polyu'; >> b=' '; >> c='ic'; >> [a b c] ans = polyu ic >> disp(a) polyu >> % display array >> disp('Solution 1.1') Solution 1.1

19 19 MatLab File Types M-file  plain ASCII text that is interpreted at run time  parsed once and "just-in-time" compiled  best for development as well as platform independency File Menu  New  M-file  % = comment statement >> what % give M-files & Mat files in current directory Filename restriction  No special character including space except underscore ‘_’

20 20 MatLab File Types Pcode file  a preparsed and encoded version of the M-file  Fast function load time; for large file, this is important  hide the source code from others.  Cannot convert Pcode back to the M-file source  Pcode is also platform independent MEX file  native C or C++ files that are dynamically linked directly into the MATLAB application at runtime.  Need to be compiled for different system hardware  Fastest runtime but can crash the MatLab application


Download ppt "The Hong Kong Polytechnic University Industrial Centre 2008 1 MatLAB Lesson 1 : Overview & Environment Edward Cheung Room W311g."

Similar presentations


Ads by Google