Introduction to MATLAB in Science Engineering and Mathematics
Introduction to MATLAB in Science, Engineering, and Mathematics Instruction John Sebeson De. Vry University January 18, 2005 J. M. Sebeson - De. Vry University © 2005
What is MATLAB? o o MATLAB stands for MATrix LABoratory. MATLAB is a high-performance language for technical computing. n n n n Math and computation Algorithm development (optimized for DSP) Data acquisition Modeling, simulation, and prototyping Data analysis, exploration, and visualization Scientific and engineering graphics Application development, including graphical user interface building January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Why Learn and Use MATLAB? o o o Heavily used in EET/CET courses with DSP content (CET 311, EET 350, EET 453) Extensive built-in commands for scientific and engineering mathematics Easy way to generate class demonstrations and test examples Simple and intuitive programming for more complex problems Standard and widely-used computational environment with many features, extensions, and links to other software. January 18, 2005 J. M. Sebeson - De. Vry University © 2005
MATLAB in DSP Product Development Develop and Test Algorithms in MATLAB Code Composer SIMULINK Simulation DSP Processor Platform MATLAB + PC = DSP Processor!! (just less efficient) January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Why Learn MATLAB (and DSP)? o o o Digital Signal Processing (DSP) is the dominant technology today, and into the future, for small-signal electronic systems (i. e. , just about everything) MATLAB has become one of the standard design environments for DSP engineering Our students need to be literate and skilled in this environment: knowledgeable in both DSP and MATLAB January 18, 2005 J. M. Sebeson - De. Vry University © 2005
How Can I Learn MATLAB? o o o Keep a copy of this presentation for reference (available on my Web Page) Get MATLAB loaded on your PC Read the “Getting Started” Users Guide at the Math. Works web site Study some of the built-in help files and demos Dive right in and use it! January 18, 2005 J. M. Sebeson - De. Vry University © 2005
This Presentation o o o The MATLAB System The basics of MATLAB computation The basics of MATLAB graphing The basics of MATLAB programming Various course examples n n Mathematics Electronics Physics Signal Processing(*) January 18, 2005 J. M. Sebeson - De. Vry University © 2005
The MATLAB System o Development Environment. n n o The MATLAB Mathematical Function Library. n n n o n “Programming in the small" to rapidly create quick and dirty throw-away programs, or “Programming in the large" to create large and complex application programs. Graphics. n n o Elementary functions, like sum, sine, cosine, and complex arithmetic More sophisticated functions like matrix inverse, matrix eigenvalues, Bessel functions, and fast Fourier transforms. “Toolboxes” for special application areas such as Signal Processing The MATLAB Language. n o MATLAB desktop Editor and debugger for MATLAB programs (“m-files”) Browsers for help, built-in and on-line documentation Extensive demos 2 D and 3 D plots Editing and annotation features The MATLAB Application Program Interface (API). n A library that allows you to write C and Fortran programs that interact with MATLAB. January 18, 2005 J. M. Sebeson - De. Vry University © 2005
MATLAB Development Environment (Workspace) January 18, 2005 J. M. Sebeson - De. Vry University © 2005
MATLAB “Help” Utilities o MATLAB is so rich that ‘help’ is essential n n n o Help command n n o o Command name and syntax Command input/output parameters Usage examples help command_name help [partial_name] tab Help documents Demos January 18, 2005 J. M. Sebeson - De. Vry University © 2005
MATLAB Function Library (A Subset) matlabgeneral matlabops matlablang matlabelmat matlabelfun matlabspecfun matlabmatfun matlabdatafun matlabpolyfun matlabfunfun matlabsparfun matlabscribe matlabgraph 2 d matlabgraph 3 d matlabspecgraph matlabgraphics January 18, 2005 - General purpose commands. - Operators and special characters. - Programming language constructs. - Elementary matrices and matrix manipulation. - Elementary math functions. - Specialized math functions. - Matrix functions - numerical linear algebra. - Data analysis and Fourier transforms. - Interpolation and polynomials. - Function functions and ODE solvers. - Sparse matrices. - Annotation and Plot Editing. - Two dimensional graphs. - Three dimensional graphs. - Specialized graphs. - Handle Graphics. J. M. Sebeson - De. Vry University © 2005
Some Elementary Functions Exponential. exp - Exponential. expm 1 - Compute exp(x)-1 accurately. log - Natural logarithm. log 1 p - Compute log(1+x) accurately. log 10 - Common (base 10) logarithm. log 2 - Base 2 logarithm and dissect floating point number. pow 2 - Base 2 power and scale floating point number. realpow - Power that will error out on complex result. reallog - Natural logarithm of real number. realsqrt - Square root of number greater than or equal to zero. sqrt - Square root. nthroot - Real n-th root of real numbers. nextpow 2 - Next higher power of 2. January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Some Specialized Functions Number theoretic functions. factor - Prime factors. isprime - True for prime numbers. primes - Generate list of prime numbers. gcd - Greatest common divisor. lcm - Least common multiple. rat - Rational approximation. rats - Rational output. perms - All possible permutations. nchoosek - All combinations of N elements taken K at a time. factorial - Factorial function. January 18, 2005 J. M. Sebeson - De. Vry University © 2005
The MATLAB Language (M-file example) function one_period(amp, freq, phase) % ONE_PERIOD(AMP, FREQ, PHASE) % This function plots one period of a sine wave with a given amplitude, % frequency (in Hz), and phase ( in degrees). T=1000/freq; % Compute the period in ms t=0: T/100: T; % Define a 100 point ms time vector 1 period long y=amp*sin(2*pi*t/T+phase*pi/180); % One period of the sine function plot(t, y) % Plot the result and format the axes and title xlabel('milliseconds') ylabel('amplitude') title(['One Period of a ', num 2 str(freq), ' Hz Sinewave with ', num 2 str(phase), ' Degree phase']) January 18, 2005 J. M. Sebeson - De. Vry University © 2005
MATLAB Graphics: 2 D Functions (Physics Example) January 18, 2005 J. M. Sebeson - De. Vry University © 2005
MATLAB Graphics: 2 D Functions (Physics Example) January 18, 2005 J. M. Sebeson - De. Vry University © 2005
MATLAB Graphics: 3 D Functions (DSP Example) January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Basic MATLAB Computation: Representation of Numbers and Variables o MATLAB operates on n row by m column matrices: n A n x m quantity is an array [8 3 4 n 6 7 2] A 1 x m or a n x 1 quantity is a vector [8 n 1 5 9 1 6] A 1 x 1 quantity is a scalar [8] January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Basic MATLAB Computation: Basic Operations o Array manipulation (Magic Square example) n o Array, vector, and scalar operators n n o Sum, diag, transpose, colon operator, indexing Matrix and vector addition and multiplication Element-by-element operations Variable statements and definitions January 18, 2005 J. M. Sebeson - De. Vry University © 2005
MATLAB Plotting and Graphics o o o Rich set of commands for 2 D and 3 D plotting of functions Command formatting and editing GUI formatting and editing Image display capabilities Animation capabilities Simple “copy and paste” for publishing generated figures January 18, 2005 J. M. Sebeson - De. Vry University © 2005
MATLAB Plotting Basic Commands o o o o o plot – x, y line plots stem – n, y discrete plots (standard representation of digital signals) bar – vertical bar plots plot 3 – 3 D x, y, z line plots mesh, surf, etc. – 3 D surface plots show_img – display matrix as an image hold – hold current figure for multiple line plots subplot – put multiple plots in one figure frame Etc, etc. - See MATLAB help documentation January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Basic Plotting - Examples o o o o Plot of sin(x) function Stem of sin(x) function Bar of sin(x) function Several sine functions with “hold” Several sine functions with “subplot” 2 D plot of sinc(x) 3 D plot of sinc(x) [“plot_sinc” m-file] n n o GUI editing View by rotation Animation [“brownian_demo” m-file] January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Basic MATLAB Programming o Scripts n n n o String of MATLAB commands Stored as m-file (*. m) Use variables from command line Variables have names consistent with script variable names Used for “quick and dirty” programs Example: “dydx_script” Functions n n n n String of MATLAB commands Stored as m-file (*. m) Use variables as function parameters No restriction on parameter names Can return variable results Used for general purpose programs Example: “yy=dydx(x, y) January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Structure of m-file Functions: Examples o “one_period” n o “sumofsines” n o Use of “num 2 str” for variable formatting Use of parameter-controlled data input loops “fft_plot” n n Use of MATLAB functions as subroutines Use of “nargin” test and branch January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Mathematics Example: Polynomial Algebra (Convolution Operator) o Polynomial products and factoring: >> p 1=[1, 3, 2]; >> p 2=[1, 5, 4, 4]; >> pc=conv(p 1, p 2) pc = 1 8 21 26 20 8 >> deconv(pc, p 2) ans = 1 3 2 >> deconv(pc, p 1) ans = 1 5 4 4 January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Mathematics Example: Linear Systems o o o Solve the system: A*S=B MATLAB Code: >> A=[5, -2, -3; 0, 4, 3; 1, -1, 9]; >> B=[-3, -2, 60]'; % Note vector transpose (‘) >> S=linsolve(A, B) S= 1. 0000 -5. 0000 6. 0000 January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Mathematics Example: Polynomial Roots o Find the roots of the following system: o MATLAB code: >> roots([12 -1 -8]) ans = 0. 8592 -0. 7759 January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Mathematics Example: Polynomial Roots o Graphical Solution: >> >> >> a=12; b=-1; c=-8; x=-1: 0. 1: 1; y=a*x. ^2+b*x+c; plot(x, y) January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Electronic Circuits Example: Mesh Analysis (Linear System) Find the currents in each branch: I 1, I 2 -7 I 1 + 6 I 2 = 5 6 I 1 – 8 I 2 = -10 A*X = B January 18, 2005 >> A=[-7, 6; 6, -8]; >> B=[5; -10]; >> X=linsolve(A, B) ans = 1. 0000 2. 0000 J. M. Sebeson - De. Vry University © 2005
Electronic Circuits Example: FET Operating Point o Find the DC operating point of the following circuit: January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Electronic Circuits Example: FET Operating Point o o M-file: [Vgs_o, Id_o]=NFET_OP(Idss, Vp, Rs) [v, id]=nfet_op(12, -4, 1200); January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Physics Example: Graphical Solution of a Trajectory o Problem: A football kicker can give the ball an initial speed of 25 m/s. Within what two elevation angles must he kick the ball to score a field goal from a point 50 m in front of goalposts whose horizontal bar is 3. 44 m above the ground? January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Physics Example: Field Goal Problem Solution: The general solution is the “trajectory equation. ” where y = height, x = distance from goal, v 0 = take-off speed, θ 0 = take-off angle. Given the take-off speed of 25 m/s, the problem requires the solutions for θ 0 that result in a minimum height of y = 3. 44 m at a distance of x = 50 m from the goal post. Although an analytical solution is possible, a graphical solution provides more physical insight. January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Physics Example: Field Goal Problem o MATLAB code for a graphical solution: >> v 0=25; >> x=50; >> g=9. 8; >> y=3. 44; >> theta=5: . 1: 70; >> theta_r=theta*pi/180; >> z=y*ones(1, length(theta)); >> zz=x*tan(theta_r)-g*x^2. /(2*v 0^2*(cos(theta_r)). ^2); >> plot(theta, zz) >> hold Current plot held >> plot(theta, z) January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Physics Example: MATLAB Results January 18, 2005 J. M. Sebeson - De. Vry University © 2005
Signal Processing Examples o o Fourier Synthesis and the Gibbs Phenomenon n “square_jms” m-file Finite Impulse Response (FIR) Filter Design n o o Use of “fvtool” Analog-to-Digital Converter Emulation n “adc” m-file Digital Transfer Function in the Z-domain n “plot. H” m-file January 18, 2005 J. M. Sebeson - De. Vry University © 2005
- Slides: 36