Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to MATLAB

Similar presentations


Presentation on theme: "Introduction to MATLAB"— Presentation transcript:

1 Introduction to MATLAB

2 What is MATLAB? MATLAB stands for MATrix LABoratory.
MATLAB is a high-performance language for technical computing. 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

3 Why Learn and Use MATLAB?
Heavily used in EET/CET courses with DSP content (CET311, EET350, EET453) 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.

4 MATLAB in DSP Product Development
Develop and Test Algorithms in MATLAB SIMULINK Simulation DSP Processor Platform Code Composer MATLAB + PC = DSP Processor!! (just less efficient)

5 Why Learn MATLAB (and DSP)?
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

6 How Can I Learn MATLAB? 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 MathWorks web site Study some of the built-in help files and demos Dive right in and use it!

7 This Presentation The MATLAB System The basics of MATLAB computation
The basics of MATLAB graphing The basics of MATLAB programming Various course examples Mathematics Electronics Physics Signal Processing(*)

8 The MATLAB System Development Environment.
MATLAB desktop Editor and debugger for MATLAB programs (“m-files”) Browsers for help, built-in and on-line documentation Extensive demos The MATLAB Mathematical Function Library. 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. “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. 2D and 3D plots Editing and annotation features The MATLAB Application Program Interface (API). A library that allows you to write C and Fortran programs that interact with MATLAB.

9 MATLAB Development Environment (Workspace)

10 MATLAB “Help” Utilities
MATLAB is so rich that ‘help’ is essential Command name and syntax Command input/output parameters Usage examples Help command help command_name help [partial_name] tab Help documents Demos

11 MATLAB Function Library (A Subset)
matlab\general General purpose commands. matlab\ops Operators and special characters. matlab\lang Programming language constructs. matlab\elmat Elementary matrices and matrix manipulation. matlab\elfun Elementary math functions. matlab\specfun Specialized math functions. matlab\matfun Matrix functions - numerical linear algebra. matlab\datafun Data analysis and Fourier transforms. matlab\polyfun Interpolation and polynomials. matlab\funfun Function functions and ODE solvers. matlab\sparfun Sparse matrices. matlab\scribe Annotation and Plot Editing. matlab\graph2d Two dimensional graphs. matlab\graph3d Three dimensional graphs. matlab\specgraph Specialized graphs. matlab\graphics Handle Graphics.

12 Some Elementary Functions
Exponential. exp Exponential. expm Compute exp(x)-1 accurately. log Natural logarithm. log1p Compute log(1+x) accurately. log Common (base 10) logarithm. log Base 2 logarithm and dissect floating point number. pow 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 Next higher power of 2.

13 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.

14 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 ',num2str(freq),' Hz Sinewave with ',num2str(phase), ' Degree phase'])

15 MATLAB Graphics: 2D Functions (Physics Example)

16 MATLAB Graphics: 2D Functions (Physics Example)

17 MATLAB Graphics: 3D Functions (DSP Example)

18 Basic MATLAB Computation: Representation of Numbers and Variables
MATLAB operates on n row by m column matrices: A n x m quantity is an array A 1 x m or a n x 1 quantity is a vector [ ] A 1 x 1 quantity is a scalar [8] [ ]

19 Basic MATLAB Computation: Basic Operations
Array manipulation (Magic Square example) Sum, diag, transpose, colon operator, indexing Array, vector, and scalar operators Matrix and vector addition and multiplication Element-by-element operations Variable statements and definitions

20 MATLAB Plotting and Graphics
Rich set of commands for 2D and 3D plotting of functions Command formatting and editing GUI formatting and editing Image display capabilities Animation capabilities Simple “copy and paste” for publishing generated figures

21 MATLAB Plotting Basic Commands
plot – x,y line plots stem – n,y discrete plots (standard representation of digital signals) bar – vertical bar plots plot3 – 3D x,y,z line plots mesh, surf, etc. – 3D 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

22 Basic Plotting - Examples
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” 2D plot of sinc(x) 3D plot of sinc(x) [“plot_sinc” m-file] GUI editing View by rotation Animation [“brownian_demo” m-file]

23 Basic MATLAB Programming
Scripts 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 Use variables as function parameters No restriction on parameter names Can return variable results Used for general purpose programs Example: “yy=dydx(x,y)

24 Structure of m-file Functions: Examples
“one_period” Use of “num2str” for variable formatting “sumofsines” Use of parameter-controlled data input loops “fft_plot” Use of MATLAB functions as subroutines Use of “nargin” test and branch

25 Mathematics Example: Polynomial Algebra (Convolution Operator)
Polynomial products and factoring: >> p1=[1,3,2]; >> p2=[1,5,4,4]; >> pc=conv(p1,p2) pc = >> deconv(pc,p2) ans = >> deconv(pc,p1)

26 Mathematics Example: Linear Systems
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 6.0000

27 Mathematics Example: Polynomial Roots
Find the roots of the following system: MATLAB code: >> roots([ ]) ans = 0.8592

28 Mathematics Example: Polynomial Roots
Graphical Solution: >> a=12; >> b=-1; >> c=-8; >> x=-1:0.1:1; >> y=a*x.^2+b*x+c; >> plot(x,y)

29 Electronic Circuits Example: Mesh Analysis (Linear System)
Find the currents in each branch: I1, I2 -7I1 + 6I2 = 5 6I1 – 8I2 = -10 >> A=[-7,6;6,-8]; >> B=[5;-10]; >> X=linsolve(A,B) ans = 1.0000 2.0000 A*X = B

30 Electronic Circuits Example: FET Operating Point
Find the DC operating point of the following circuit:

31 Electronic Circuits Example: FET Operating Point
M-file: [Vgs_o,Id_o]=NFET_OP(Idss,Vp,Rs) [v,id]=nfet_op(12,-4,1200);

32 Physics Example: Graphical Solution of a Trajectory
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?

33 Physics Example: Field Goal Problem
Solution: The general solution is the “trajectory equation.” where y = height, x = distance from goal, v0 = 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.

34 Physics Example: Field Goal Problem
MATLAB code for a graphical solution: >> v0=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*v0^2*(cos(theta_r)).^2); >> plot(theta,zz) >> hold Current plot held >> plot(theta,z)

35 Physics Example: MATLAB Results

36 Signal Processing Examples
Fourier Synthesis and the Gibbs Phenomenon “square_jms” m-file Finite Impulse Response (FIR) Filter Design Use of “fvtool” Analog-to-Digital Converter Emulation “adc” m-file Digital Transfer Function in the Z-domain “plotH” m-file

37 Thank U


Download ppt "Introduction to MATLAB"

Similar presentations


Ads by Google