1 / 14

Introduction To MATLAB

Mathematical Modeling and Simulation. Introduction To MATLAB. Prof. Muhammad Saeed. Basic Features. MATLAB Windows Command ( << ) Command History Workspace Current Directory Editor Profiler Help Simple Math in Command Window Operators: +, -, *, /, , ^

byrd
Télécharger la présentation

Introduction To MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Basic Features • MATLAB Windows • Command ( << ) • Command History • Workspace • Current Directory • Editor • Profiler • Help • Simple Math in Command Window • Operators: +, -, *, /, \, ^ • Variables: ans, pi, inf, NaN, i, j, realmin, realmax, • ……, user-defined Mathematical Modeling and Simulation

  3. Precision: significant digits and formats • ‘format short’, ‘format short e’and ‘format short g’ • ‘format long’, ‘format long e’,’ format long g’ • ‘format hex’ • ‘format bank’ • ‘format +’ • Complex numbers • a) ‘imag()’, ‘real()’, ‘abs()’ and ‘angle()’ • Display and Help • 1. ‘who’ , ‘whos’, clear, clear a*, clear –regexp, clc, disp, ver, version, diary, commands • 2. Comments in MATLAB (%) • 3. Ellipses ( … ) and ‘;’ at the end of commands • 4. Built-in Functions and Help using fx,start andHelp Mathematical Modeling and Simulation

  4. Arrays and Matrices • Initialization: • Row Matrix: a=[1,2,3,4] , [1 2 3 4] , [1:1:4] or [1:4] • a=1:2:20, b=1:10 ,(1:1:10), (1:7) • Column Matrix: a=[1;2;3;4] • iii. 3x3 Matrix: d3=[1,2,3;4,5,6;7,8,9] , [1 2 3;4 5 6;7,8,9] , • [1:3;4:6;7:9] , [1:exp(1.4567):100] , angle=[0:pi/10:pi] • iv. sine=sin(angle) • v. d=[a b] , d=[a b;b a] or d=[a b 0 1 2 3 b], c(:)=1.5 • vi. x=(0:0.1:2)*pi,linspace(0,2*pi,21), logspace(0, 3, 5) • vii. ones(n, m), ones(n), zeros(n), zeros(n, m), eye(n), eye(n, m), rand(n), rand(n, m), randn(n), randn(n, m), randi(10,5), randperm(n), diag([ ]), diag(a), • diag(a, n), diag(a,-n) Mathematical Modeling and Simulation

  5. ….. Arrays and Matrices • Operations: • a(2), a(2:6), a(3:end), a(12:-1:5), a(3:1:9), a([7 4 6 2]) • Transpose : a.’, [1:5]’, (0:0.1:1)’ • Conjugate Transpose : a’ • Maths:3*(a-1), a*2, 3\a, a/3, a+4, a+b, a*b, a.*b, a./b, a^2, a.^2, 2./a • Extraction: a=b([2 3],[1 3 4]), a=b(:,1:3) • Special Functions: • sort(a), sort(a,’ascend’), sort(a,’descend’), sort(a,1), sort(a,2), sortrows(a), sortrows(a,1), sortrows(a,[1 3]), • sortrows(a,-2), • find(a>7), find(a, k), find(a, k, ’first’), find(a, k,’last’), • max(a), min(a), • flipud(a), fliplr(a), rot90(a), rot90(a,2), • triu(a). tril(a), • ……………….. continued Mathematical Modeling and Simulation

  6. ….. Arrays and Matrices • repmat(a,1,4),repmat(a, 2, 2), repmat(pi,size(a)), repmat(pi, 2, 3), • reshape(a, 4, 3), reshape(1:10, 2, 5) • numel(a), length(a), size(a), size(a,1), size(a,2), ndims(a), • prod(a), prod(a,1), prod(a,2), • dot(a,b), cross(a,b), • sub2ind(size(a),3,5), ind2sub(size(a),15), • sum(a), • diag(diag(a)), blkdiag(a,b,c), Mathematical Modeling and Simulation 6

  7. ….. Arrays and Matrices • Data Types: declaration, conversion and display • int8, uint8……. Int64, uint64, char, double, single, inf, NaN, ones(n,m,’uint32’), • cast(a,’int16’), class(a), • realmax(‘type’), realmin(‘type’), intmin, intmax, • num2str, int2str, mat2str, str2double, str2num, • strmatch(‘…’, str), • regexp, regexpi • sprintf(…..), fprintf(….. ) • Operators: • +, -, *, /, \, ^ , • =, <, >, <=, >=, • ==, ~=, • ‘, .’, .*, ./, .^, .\ • &, |, ~, &&, || Mathematical Modeling and Simulation

  8. Basic Matrix Functions • 1.rank(a) • 2. inv(A) • 3. A\y, solves set of linear equations, A is a matrix and y is a vector • 4. det(A) • 5. eig(A), [v d]=eig(A) calculates eigen values and eigen vectors • 6. trace(A) • 10. sp=sparse(A), • 11. full(sp) • 12. or(a,b) • 13. xor(a,b) Mathematical Modeling and Simulation 8

  9. M-File Scripts • 1. Specific Functions used in M-Files: • echo, disp, input, keyboard, pause, pause(n), waitforbuttonpress • 2. 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. Mathematical Modeling and Simulation 9

  10. M-File Functions • 1. Function name must be identical to M-File name. • 2. Function start with ‘function’ keyword and ends with ‘end’ keyword • 3. 1st line in M-File must be the function declaration • 4. The ‘error’ and ‘warning’ functions in the M-File are like ‘sprintf ‘ • 5. Script file called in a function is evaluated in function workspace • 6. Subfunctions are called from the 1st function’s body • 7. Help for subfunction can be displayed by • >>helpwin func/subfunc • 8. 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. Mathematical Modeling and Simulation 10

  11. ……….. 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 function • 12. The ‘return’ statement is not necessary. • 13. Script file called in a function is evaluated in function WS • 14. Unlimited number of input and output arguments by specifying ‘varargin’ as the last argument and ‘varargout’ for output arguments. • 15. ‘pcode’command compiles the function • 16. 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 Mathematical Modeling and Simulation 11

  12. Set Functions • 1. isequal(a, b), compares as a whole • 2. unique(a), removes duplications • 3. ismember(a,b), ismember(a,’xyz’), element by element • 4. union(a,b), must be rows or columns • 5. intersect(a,b) • 6. diff(a) • Base Conversion Functions • 1. dec2bin(x), bin2dec(x) • 2. dec2hex(x), hex2dec(x) • 3. dec2base(x, base), base2dec(x, base) • Precision Functions • digits(n), vpa(pi), vpa(x, d), vpa(‘pi’) Mathematical Modeling and Simulation 12

  13. Data Analysis • 1. mean(a), for a 2D matrix each rows s mean will be calculated. • 2. mean(a,1), row values are averaged • 3. mean(a,2), column values are averaged • 4. median(a) • 4. std(a), standard deviation • 5. var(a), Variance • 5. cov(a), covariance • 6. corrcoef(a), correlation coefficient • 7. diff(a), rows are subtracted • 8. min(a) • 9. max(a) • 10. cumsum(a) • 11. cumprod(a) Mathematical Modeling and Simulation 13

  14. End Mathematical Modeling and Simulation 14

More Related