Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to Matlab Original: Jing Wang Presenter: Abdul Jabbar Siddiqui Date: January 20th, 2017 What is Matlab • High level language for technical computing • Stands for MATrix LABoratory • Everything is a matrix - easy to do linear algebra • MATLAB is available for Windows, Macintosh and UNIX systems. It is used by more than one million people in industry and academia. The basics 2 The basics: Matrix calculation • ^: exponentiation • *: multiplication • /: division • +: addition • -: subtraction Array Operations • Evaluated element by element .' : array transpose .^ : array power .* : array multiplication ./ : array division >> A=[1 2;3 4]; >> B=[5 6;7 8]; >> A*B 19 22 43 50 But: >> A=[1 2;3 4]; >> A^2 7 10 15 22 But: >> A.*B 5 21 >> A.^2 1 9 12 32 4 16 Some Built-in functions • mean(A):mean value of a vector • max(A), min (A): maximum and minimum. • sum(A): summation. • median(A): median value • std(A): standard deviation. • det(A) : determinant of a square matrix • Inv(A): Inverse of a matrix A Indexing Matrices Given the matrix: A m Then: n = 0.9501 0.2311 0.6068 0.4860 0.4231 0.2774 A(1,2) = 0.6068 Aij ,i = 1...m, j = 1...n A(3) = 0.6068 index = (i −1)m + j A(:,1) = [0.9501 1:m 0.2311 ] A(1,2:3)=[0.6068 0.4231] Adding Elements to a Vector or a Matrix >> A=1:3 A= 1 2 3 >> A(4:6)=5:2:9 A= 1 2 3 5 7 >> B=1:2 B= 1 2 >> B(5)=7; B= 1 2 0 0 7 9 >> C=[1 2; 3 4] C= 1 2 3 4 >> C(3,:)=[5 6]; C= 1 2 3 4 5 6 >> D=linspace(4,12,3); >> E=[C D’] E= 1 2 4 3 4 8 5 6 12 Graphics - 2D Plots plot(xdata, ydata, ‘marker_style’); For example: >> x=-5:0.1:5; >> sqr=x.^2; >> pl1=plot(x, sqr, 'r:o'); Graphics - Overlay Plots Use hold on for overlaying graphs So the following: Gives: >> hold on; >> cub=x.^3; >> pl2=plot(x, cub,‘b:s'); Graphics - Annotation Use title, annotation xlabel, ylabel and legend for >> title('Demo plot'); >> xlabel('X Axis'); >> ylabel('Y Axis'); >> legend([pl1, pl2], 'x^2', 'x^3'); Graphics-Stem() • stem()is to plot discrete sequence data • The usage of stem() is very similar to plot() cos(nπ/4) 1 >> >> >> >> n=-10:10; f=stem(n,cos(n*pi/4)) title('cos(n\pi/4)') xlabel('n') 0.5 0 -0.5 -1 -10 -5 0 n 5 10 subplots • Use subplots to divide a plotting window into several panes. Create a .m script. x=0:0.1:10; f=figure; f1=subplot(1,2,1); plot(x,cos(x),'r'); grid on; title('Cosine') f2=subplot(1,2,2); plot(x,sin(x),'d'); grid on; title('Sine'); Cos ine S ine 1 1 0.8 0.8 0.6 0.6 0.4 0.4 0.2 0.2 0 0 -0.2 -0.2 -0.4 -0.4 -0.6 -0.6 -0.8 -0.8 -1 0 5 10 -1 0 5 10 Audio signal command waveplay(y,Fs); • Plays the audio signal stored in the vector y on a PCbased audio output device. You specify the audio signal sampling rate with the integer Fs in samples per second. The default value for Fs is 11025Hz. • .wave file is a file format for storing the signal amplitude of a sound wave. It can be loaded into MATLAB, displayed, processed, played, and written back to a disk file Audio signal command [y, Fs, bits] = wavread(‘tarzan.wav’); • - y is a vector containing the signal amplitude in ‘tarzan.wav’ - Fs is a scalar representing the sampling frequency - bits is a scalar representing the sampling depth (e.g. 8 bits used to represent one sample) wavwrite(y,Fs,N,filename) • - writes the data stored in the variable y to a WAVE file called filename. The data has a sample rate of Fs Hz and is N-bit. Discrete Fourier Transform Discrete Fourier Transform M-files : Script and function files When problems become complicated and require re– evaluation, entering command at MATLAB prompt is not practical Solution : use M-files Script Function Collections of commands User defined commands Executed in sequence when called Normally has input & output Saved with extension “.m” Saved with extension “.m” M-files : script and function files (function) • Function is a ‘black box’ that communicates with workspace through input and output variables. INPUT FUNCTION – Commands – Functions – Intermediate variables OUTPUT M-files : script and function files (function) Every function must begin with a header: function output=function_name(inputs) Output variable Must match the file name input variable function y=add3(a) a=a+1; a=a+1; y=a+1; Click View then Header and Footer to change this footer Debugging with Matlab • Examine/change the value of variables • Look at the stack of function • Evaluate arbitrary expression Click View then Header and Footer to change this footer Flow Control:(if, while , for) • If Statement Syntax if (Condition_1) Matlab Commands elseif (Condition_2) Matlab Commands elseif (Condition_3) Matlab Commands else Matlab Commands end Some Dummy Examples if ((a>3) & (b==5)) Some Matlab Commands; end if (a<3) Some Matlab Commands; elseif (b~=5) Some Matlab Commands; end if (a<3) Some Matlab Commands; else Some Matlab Commands; end Click View then Header and Footer to change this footer • While Loop Syntax while (condition) Matlab Commands end Dummy Example while ((a>3) & (b==5)) Some Matlab Commands; end Click View then Header and Footer to change this footer Some Dummy Examples • For loop syntax for i=Index_Array Matlab Commands end for i=1:100 Some Matlab Commands; end for j=1:3:200 Some Matlab Commands; end for m=13:-0.2:-21 Some Matlab Commands; end for k=[0.1 0.3 -13 12 7 -9.3] Some Matlab Commands; end Practice Problems Click View then Header and Footer to change this footer Time domain Signal plot Click View then Header and Footer to change this footer Practice Problems Click View then Header and Footer to change this footer Frequency spectrum Click View then Header and Footer to change this footer Click View then Header and Footer to change this footer Click View then Header and Footer to change this footer