Introduction To MATLAB Prof. Muhammad Saeed Mathematical Modeling and Simulation.

Post on 27-Mar-2015

244 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

Transcript

Introduction To MATLAB

Prof. Muhammad Saeed

Mathematical Modeling and Simulation

Basic Features MATLAB Windows

1. Command ( << )2. Command History3. Workspace 4. Current Directory5. Editor6. Profiler7. Help

Simple Math in Command Window1. Operators: +, -, *, /, \, ^2. Variables: ans, pi, inf, NaN, i, j, realmin, realmax,

……, user-defined

2Mathematical Modeling and Simulation

3. Precision: significant digits and formatsa) ‘format short’, ‘format short e’and ‘format short g’b) ‘format long’, ‘format long e’,’ format long g’c) ‘format hex’d) ‘format bank’e) ‘format +’4. Complex numbers

a) ‘imag()’, ‘real()’, ‘abs()’ and ‘angle()’

Display and Help1. ‘who’ , ‘whos’, clear, clear a*, clear –regexp, clc,

disp, ver, version, diary, commands2. Comments in MATLAB (%)3. Ellipses ( … ) and ‘;’ at the end of commands

4. Built-in Functions and Help using fx, start and Help

3Mathematical Modeling and Simulation

4Mathematical Modeling and Simulation

5Mathematical Modeling and Simulation

….. Arrays and Matrices

66Mathematical Modeling and Simulation

….. Arrays and Matrices

7Mathematical Modeling and Simulation

….. Arrays and Matrices

88Mathematical Modeling and Simulation

Basic Matrix Functions1. rank(a)2. inv(A)3. A\y, solves set of linear equations, A is a matrix and y is a vector4. det(A)5. eig(A), [v d]=eig(A) calculates eigen values and eigen vectors6. trace(A)10. sp=sparse(A), 11. full(sp)12. or(a,b)13. xor(a,b)

99Mathematical Modeling and Simulation

M-File Scripts

1. Specific Functions used in M-Files:echo, disp, input, keyboard, pause, pause(n),

waitforbuttonpress2. All functions and control statements of MATLAB can

be used in script files.3. Extension is ‘m’ ( file.m )4. M-File directly runs on the command line ( >>) as simple statements do. It is evaluated in MATLAB workspace.

1010Mathematical Modeling and Simulation

1. Function name must be identical to M-File name. 2. Function start with ‘function’ keyword and ends with

‘end’ keyword3. 1st line in M-File must be the function declaration4. The ‘error’ and ‘warning’ functions in the M-File are

like ‘sprintf ‘5. Script file called in a function is evaluated in function

workspace6. Subfunctions are called from the 1st function’s body7. Help for subfunction can be displayed by

>>helpwin func/subfunc8. Functions can have zero input or zero output arguments.9. Functions can be called with fewer input and output

arguments than are specified in the function definition but not with more arguments than specified.

M-File Functions

1111Mathematical Modeling and Simulation

……….. M-File Functions

10. Function arguments can be determined by two variables: nargin and nargout.11. The first set of contiguous comment lines after the function declaration are the help text for the function12. The ‘return’ statement is not necessary.13. Script file called in a function is evaluated in function WS14. Unlimited number of input and output arguments by

specifying ‘varargin’ as the last argument and ‘varargout’ for output arguments.15. ‘pcode’ command compiles the function16. Functions can be nested. 17. One-Line‘inline’ functions can be defined as in C (by #define)18. Anonymous functions are defined by handles .19. Handles of MATLAB functions can also be created

1212Mathematical Modeling and Simulation

Set Functions1. isequal(a, b), compares as a whole2. unique(a), removes duplications3. ismember(a,b), ismember(a,’xyz’), element by element4. union(a,b), must be rows or columns5. intersect(a,b)6. diff(a)

Base Conversion Functions1. dec2bin(x), bin2dec(x)2. dec2hex(x), hex2dec(x)3. dec2base(x, base), base2dec(x, base)

Precision Functionsdigits(n), vpa(pi), vpa(x, d), vpa(‘pi’)

1313Mathematical Modeling and Simulation

Data Analysis1. mean(a), for a 2D matrix each rows s mean will be

calculated.2. mean(a,1), row values are averaged3. mean(a,2), column values are averaged4. median(a)4. std(a), standard deviation5. var(a), Variance5. cov(a), covariance6. corrcoef(a), correlation coefficient7. diff(a), rows are subtracted8. min(a)9. max(a)10. cumsum(a)11. cumprod(a)

End

1414Mathematical Modeling and Simulation

top related