Download presentation
Presentation is loading. Please wait.
1
ENE MATHLAB® Lecture 1
2
MATLAB® overview High-level language for technical computing.
Development environment for managing code, files, and data. Interactive tools for iterative exploration, design, and problem solving. Mathematical functions for linear algebra, statistics, signal processing, numerical technique, and etc. 2-D and 3-D graphic functions for visualizing data. Tools for building custom graphical user interfaces.
3
User interface
4
Entering commands At prompt (>>) type 1+2 and press Enter.
>> 1+2 MATLAB will display the answer as shown below. ans = 3
5
Variables MATLAB does not require any type declaration or dimension statements. If the variable already exists, MATLAB automatically changes its contents and allocates new storage.
6
Numbers The format of numbers can be set using the ‘format’ command.
FORMAT SHORT Scaled fixed point format with 5 digits. FORMAT LONG Scaled fixed point format with 15 digits for double and 7 digits for single. FORMAT SHORT E Floating point format with 5 digits. FORMAT LONG E Floating point format with 15 digits for double and 7 digits for single. FORMAT SHORT G Best of fixed or floating point format with 5 digits. FORMAT LONG G Best of fixed or floating point format with 15 digits for double and 7 digits for single.
7
Format function
8
Format function
9
Operators Addition: + Substraction: - Multiplication: * Division: /
Power: ^
10
Functions MATLAB have a hugh number of standard mathematical functions. For a list of the elementary functions, type “help elfun”. For a list of advanced mathematical functions, type “help specfun” or “help elmat” Examples of functions: Abs, sqrt, exp, sin, pi, i, j, inf, and etc.
11
Entering Matrices There are several different ways to enter matrices into MATLAB: Enter an explicit list of elements. Load matrices from external data files. Generate matrices using built-in functions. Create matrices with your own functions and save them in files.
12
Entering Matrices These are a few basic conventions:
Separate the elements of a row with blanks or commas. Use a semicolon (;) to indicate the end of each row. Surround the entire list of elements with square brackets, [ ].
13
Entering Matrices This is called “Dürer’s matrix”.
The summation along any row, column, or two main diagonals will always be equal.
14
Entering Matrices For example, try to enter a Dürer’s matrix into MATLAB. At command window, type >> A = [ ; ; ; ] MATLAB displays the derised matrix: A =
15
Sum Let us verify the properties of Dürer’s matrix using MATLAB.
>> sum(A) ans =
16
Transpose MATLAB has two transpose operators:
The apostrophe operator (A’): a complex conjugate transposition. The dot-apostrophe operator (A.’): transposing without affecting the sign of complex elements. >> A' ans =
17
Diag >> diag(A) ans = >>sum(diag(A)) ans = 34
18
Subscripts The element in row I and column j is denoted by A(i,j).
>> A(1,4) + A(2,4) + A(3,4) + A(4,4) ans = 34
19
Increase the matrix size
By putting ‘;’, the result will not be displayed. The size of the matrix will be automatically increased to accommodate the newcomer. >> B = A; >> B(4,5) = 26 B =
20
The Colon Operator Ex. A row vector contains the integers from 1 to 10: >> C = 1:10 C = Ex. To obtain 0 to 4*pi with an increment of pi/4. >> 0:pi/4:pi ans =
21
The Colon Operator Ex. The 1st 4 elements of the 3rd column of A.
ans = 2 11 7 14
22
Generating matrices MATLAB provides four functions to generate basice matrices: zeros – all zeros. ones – all ones. rand – uniformly distributed random elements. randn – normally distributed random elements.
23
Generating matrices
24
The load function The load function reads binary files containing matrices generated by earlier MATLAB session, or reads text files containing numeric data. >> load data1.dat
25
Concatenation The process of joining smal matrices to make bigger ones. >> B = [A A+32; A+48 A+16] B =
26
Deleting rows and columns
27
Arrays
28
Arrays >> x = [0:9]'; >> table1 = [x x.^2 2.^x] table1 =
29
Arrays
30
Creating a plot A plot function has different forms depending on the input arguments. If y is a vector, plot (y) produces a piecewise linear graph of the elements of y versus the index of the element y. If two vectors are specified, plot(x,y) is used to produce a graph of y versus x.
31
Creating a plot Example of plotting a vector x, ranging from 0 to 2, versus the sine of those values. The axes can be labelled using ‘xlabel’ and ‘ylabel’. The figure is shown on the next page.
32
Creating a plot
33
Plotting multiple data in one graph
A function ‘plot’ can be used to obtain multiple x-y pair arguments with customizable colors to allow discrimination among sets of data. A legend command is used to identify the individual plots.
34
Plotting multiple data in one graph
35
Specifying line styles and colors
MATLAB allows us to specify line styles, line colors, or even markers using ‘plot’ function.
36
Specifying line styles and colors
37
Specifying line styles and colors
38
Plotting lines and markers
If a marker is specified not a line style, only the marker will be drawn. The below example plots the data twice using a different number of points for the dotted line and marker plots.
39
Plotting lines and markers
40
Graphing complex data When the arguments to plot are complex, the imaginary part is ignored except when you pass plot a single complex argument. For example, plot(Z) where Z is a complex vector or matrix is equivalent to plot(real(Z), imag(Z))
41
Graphing complex data
42
Adding plots to an existing graph
A command ‘hold’ allows you to add plots to an existing graph.
43
Adding plots to an existing graph
44
Displaying multiple plots in 1 figure
The ‘subplot’ command enables you to display multiple plots in the same figure. The figure is partitioned into an m-by-n matrix of small subplots and selects the pth subplot for the current plot.
45
Displaying multiple plots in 1 figure
46
Setting axis limits MATLAB is automatically setting axis limits to span the range between the maxima and minima. The ‘axis’ commant allows you to specify your own limit: For three-dimentional graphs: To enable automatic limit selection again:
47
Setting axis visibility
The default is set for invisible axis. To make the axis visible or invisible using: axis on axis off
48
Setting grid lines The default is set for no grid line.
To make the grid lines on and off using: grid on grid off
49
Adding axis labels and titles
The ‘x-label’, ‘y-label’, and ‘z-label’ commands add x-, y-, and z-axis labels. The ‘title’ command adds a title at the top of the figure. The ‘text’ function inserts text anywhere in the figure.
50
Adding axis labels and titles
51
Adding axis labels and titles
52
Saving a figure
53
Graph components
54
Figure tools
55
Figure toolbars
56
Plotting tools
57
Plotting tools
58
Any question??
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.