2.introduction To Matlab
2.introduction To Matlab
2.introduction To Matlab
EDA
Topics
• What is Matlab
• Variables
• Vectors
• Scripts
Vectors
EDA
Vectors
• 1-by-1 arrays are also called scalars 5
5 88 3 11
• 1-by-N arrays are also called row vectors
3
5 7 2
You can also create a column vector by separating the elements by semicolons.
>>g = [3;7;9]
g =
3
7
9
Referencing the Elements of a Vector
If A is a vector (i.e., a row or column vector), then
A(1) is its first element, A= [ a b c d]
A(2) is its second element
1 2 3 4 Index
Example
>> A = [ 3 4.2 -7 10.1 0.4 -3.5 ];
>> A(3) Ans = -7
>> Index = 5;
>> A(Index) Ans = 0.4
r = [2 4 20] ;
w = [9 -6 3];
u = [ r w]
The result is the vector u = [2, 4, 20, 9, -6, 3].
Example:
>> phrase1 = 'This is sentence #1. ';
>> phrase2 = 'This is sentence #2.';
>> sentence = [phrase1 phrase2]
sentence =
•The linspace command also creates a linearly spaced row vector, but
instead you specify the number of values rather than the increment.
Example,
>> linspace(5,8,31)
is equivalent to 5:0.1:8.
Example,
>> x=logspace(-1,1,4)
x =
To find the largest element xmax and its location locmax in a vector, we
use
[xmax, locmax] = max(x)
Example
y=
-0.0000 -0.6428 -0.9848 -0.8660 -0.3420 0.3420 0.8660 0.9848 0.6428 0.0000
Scripts
Good Programming Practice
EDA
Script
Scripts files are program files with .m extension. In these files, you write
series of commands, which you want to execute together. Scripts do not
accept inputs and do not return any outputs. They operate on data in the
workspace.
Create and run script file
• Using the command prompt: type ‘edit’ in the command prompt. This will
open the editor. You can directly type edit and then the filename (with .m
extension)
• Using the IDE: Alternatively, if you are using the IDE, choose NEW ->
Script. This also opens the editor and creates a file named Untitled. You
can name and save the file after typing the code.
• After creating and saving the file, you can run it in two ways: Clicking the
Run button on the editor window or Just typing the filename (without
extension) in the command prompt: >> prog1 The command window
prompt displays the result
• The file must be in your current directory or it will not run
Script
Comment/uncomment
Run button
Line numbers
Comments
Comments
Single line comments:
• The percent symbol (%) is used for indicating a comment line.
• Comment will not be processed
• Script
Error message will appear in
command window with error
location (line number) and
error explanation
Error types
• Syntax errors such as omitting a parenthesis or comma, or spelling a
command name incorrectly.
• demo will bring up MATLAB Examples in the Help Browser, which has
examples of some of the features of MATLAB
• help will explain any function; help help will explain how help works
• doc will bring up a documentation page in the Help Browser
Example
• Live scripts contain output and graphics with the code that produced
them, together in a single interactive environment called the Live
Editor.
• To create a live script in the Live Editor, go to the Home tab and
click New Live Script . It has .mlx extension.
• You can divide your code into sections and run each section
individually by clinking the vertical striped bar to the left of the code.
• The output is displayed to the right of the code.
Good programming practice
A good program is:
• Efficient
• Correct (correct syntax & semantics)
• Readable (Easy to read & understand)
• Easy to maintain & enhance
Good programming practice
• Although variables named result and RESULT are different, avoid this as
it would be confusing
• Store results in named variables (rather than using ans) if they are to
be used later
EDA
Exercise
1. Calculate the area and circumference of a circle with a radius of 4 cm.
Circumference = 25.13 cm
2. Create a script file called ShpereScript that will compute and display
volume of a shpere, assume radius=5cm.
Exercise – Flow in a circular channel
The flow rate Q in m3/s in an open channel of circular cross-section
shown in the figure is given by
Q=
8 sinθ 1 - cosθ
5/2
EDA
Commands for managing the
work station
Command Purpose
clc Clears command window
clear Removes variables from memory
exist Checks for exitence of file or variable
global Declares variables to be global
help Searches for a help topic
lookfor Searches help entries for a keyword
quit Stops Matlab
who Lists current variables
whos Lists current variable and indicates if they have
imaginary parts
Decimal-to-Integer Conversion
Functions
Matlab function x y Description
2.7 2.0000
y = fix(x) -1.9 -1.0000 Round towards zero
2.49-2.51j 2.0000-2.0000i
2.7 3.0000
y = round(x) -1.9 -2.0000 Round to nearest integer
-2.49-2.51j 2.0000-3.0000i
2.7 3.0000
y = ceil(x) -1.9 -1.0000 Round toward infinity
2.49-2.51j 3.0000-2.0000i
2.7 2.0000
y = floor(x) -1.9 -2.0000 Round toward minus infinity
2.49-2.51j 2.0000-3.0000i
Complex Number Manipulation
Functions
• MIT courseware