Md Rakibul Hasan
Lecturer, EEE, BRACU
[Link]@[Link]
✓ Command
Window
✓ Workspace
✓ Current
Folder
❖Built-in Functions: sin, cos, sqrt, abs, ceil, isprime, gcd,
lcm, find, rot90, size, length, isempty, isequal, and so on…
❖ To know more about the a function, you can use help
help sin
Or, better:
doc sin
which opens up an indexed help window with further details about sin
❖ Command: edit <filename>.m
➢ An m-file, or script file is a simple text file
➢ You can place MATLAB commands there instead of using command window
➢ Save your works
➢ Objects (e.g. data, text, color) in MATLAB is represented by matrices.
❑ Scalar: s=5
❑ Vector: a=[1 2 3]; %row vector
b=[4;5;6]; %column vector
c=1:5 %row vector
❑ Matrix: mat=[1 2 3; 4 5 6; 7 8 9]
% Use percent sign for comments
% Suppress output by adding a semicolon at the end of that command
Function Description
zeros(i,j) creates an i x j matrix of all zeros
ones(i,j) creates an i x j matrix of all ones
rand(i,j) creates an i x j matrix of random elements (between 0 and 1)
creates an i x i identity matrix (a matrix of zeros with ones on
eye(i)
the diagonal)
*Slide Courtesy: [Link]
❑Find Difference between:
➢ a*b & a.*b
➢ a/b & a./b
➢ a^3 & a.^3
*Slide Courtesy: [Link]
b=[1 2 3 4;5 6 7 8; 9 10 11 12;13 14 15 16]
Expression Result Description
5:10 ans = 5 6 7 8 9 10 Values 5 to 10.
10:-1:5 ans = 10 9 8 7 6 5 Values 10 to 5 (using decrement of 1)
b(1:2,3:4) ans = 3 4 \n 7 8 Row 1 to 2, Column 3 to 4
ans = 1 5 9 13 2 6 10 14 Produces one long column, which is a concatenation of the
b(:)
3 7 11 15 4 8 12 16 individual columns.
Slicing from the concatenated columns. In this case, you are
b(5:8) ans = 2 6 10 14
getting the values of the second column.
end is a special value which indicates the end of the matrix. You
b(end) ans = 16 could have also written b(end,end) to specify the ending row and
the ending column.
This gets the values of the last column (rows 1 to 4 and column
4). Since you are specify all rows and the final column, you could
b(1:4,4) ans = 4 8 12 16
also use the notation: b(1:end,end) or the colon operator on its
own, which specifies the entire range of rows b(:,end)
Reverses the order of the rows so that the last row is first and the
b(end:- ans = 13 14 15 16 9 10
first row is last. Read this as: Rows last to first with a decrement
1:1,:) 11 12 5 6 7 8 1 2 3 4
of 1 (end:-1:1) and all columns(:)
*Slide Courtesy: [Link]
*Slide Courtesy: [Link]
*Slide Courtesy: [Link]
i
*Slide Courtesy: [Link]
*Slide Courtesy: [Link]
*Slide Courtesy: [Link]
*Slide Courtesy: [Link]
*Slide Courtesy: [Link]
➢ Two ways to plot figures: mouse-operation vs. scripting.
Graphing functions MATLAB command
Label the horizontal axis. xlabel('text')
Label the vertical axis. ylabel('text')
Attach a title to the plot. title('text')
Change the limits on the x and y axis. axis([xmin xmax ymin ymax])
Keep plotting in the same window. hold on
Turn off the "keep-plotting-in-the-
hold off
same-window-command".
Type doc plot on Command Window
*Slide Courtesy: [Link]
*Slide Courtesy: [Link]
*Slide Courtesy: [Link]
1. Plot sin(x) on the interval [-pi,pi] using spacing 0.5, 0.1 and 0.01 between
the points where you will sample the function.
2. Attach labels to the axis of the previous plot, and give a title to the graph.
3. Plot 5cos(𝑥 2 +1) on [-2pi,2pi].
❖ Coursera online course
Introduction to Programming with MATLAB
[Link]
❖ Mathworks
• [Link]
• [Link]
ge/