lecture on introduction to matlab
Post on 06-Apr-2018
242 Views
Preview:
TRANSCRIPT
-
8/3/2019 Lecture on Introduction to MATLAB
1/22
An Introduction to MATLABLesson 1
Mani MOHAN
CE407
Mani 2012 1
-
8/3/2019 Lecture on Introduction to MATLAB
2/22
MATLAB MATLAB==MATrix LABratory
It is widely used to solve different types ofscientific problems.
The basic data structure is a complex doubleprecision matrix.
Mani 2012 2
-
8/3/2019 Lecture on Introduction to MATLAB
3/22
Run MATLABFrom Start Menu
Select MATLAB
MATLAB PromptTells that MATLAB
Mani 2012 3
s rea y or yourcommand
-
8/3/2019 Lecture on Introduction to MATLAB
4/22
MATLAB Layout1 to 5 different windows can be selected to appear (View)
Current
directorywindow
Mani 2012 4
Commandwindow
CommandHistorywindow
-
8/3/2019 Lecture on Introduction to MATLAB
5/22
MATLAB shortcutsHelpSIMULINKOpen filesNew
file
Mani 2012 5
-
8/3/2019 Lecture on Introduction to MATLAB
6/22
MATLAB AS A CALCULATOR
>> 39*4.4+5
ans =176.6000
Command window
The MATLAB command
Mani 2012 6
omman w n ow
The result.
-
8/3/2019 Lecture on Introduction to MATLAB
7/22
MATLAB Variable names:
Starts with a letter
Up to 31 characters ( some use 19 or 21)
May contain letters, digits and underscore_ Case sensitive (A is not the same as a)
Mani 2012 7
-
8/3/2019 Lecture on Introduction to MATLAB
8/22
MATLAB Assignment Variable names:
A=2.3A =
The MATLAB command
Up to 31 characters ( some use 19 or 21)
May contain letters, digits and underscore_
Case sensitive (A is not the same as a)
Mani 2012 8
.
This is the result of theMATLAB statement
-
8/3/2019 Lecture on Introduction to MATLAB
9/22
Scalar Assignment
A=2.3
A=[2.3]
The square braces [ ] areused to define matrices.
and set its value to 2.3A =
2.3000
Mani 2012 9
=
2.3000
We can use them forscalars too.
-
8/3/2019 Lecture on Introduction to MATLAB
10/22
Row vectorThe square braces are
X=[2,3 7 ] Space or comma areused to separate
use o e ne a ma r x
Mani 2012 10
2 3 7
-
8/3/2019 Lecture on Introduction to MATLAB
11/22
Column vectorThe square braces are
X=[2;3 ; 7 ]
use o e ne a ma r x
semicolon are used to end arow.
Mani 2012 11
2
3
7
ou can a so use oend a row
-
8/3/2019 Lecture on Introduction to MATLAB
12/22
MATLAB StatementsMATLAB Statement Remarks
=5.66 s a sca ar
C=[5.66] An alternative way
X=[3.5 6.3, 33]X is a 1X3 matrix with elements 3.5 , 6.3and 33. Commas or space are used toseparate the elements in a row
Y=[1 Y is a 2X1 matrix whose elements are 1
Mani 2012 12
4 ]
an 4.
Y = [ 1 ; 4] Semicolon are used to indicate the end ofthe row.
A=1:5 Equivalent to A=[1 2 3 4 5]
-
8/3/2019 Lecture on Introduction to MATLAB
13/22
MATLAB StatementsMATLAB Statement Remarks
=
01
V=[ 2 3 5
3 3 8]
C=[1:3:11] C=[1 4 7 10]
Z=4\8 Z=2
Y=eye(2)
=
833
532V
Mani 2012 13
10
W = zeros(2,3)
=
000
000V
-
8/3/2019 Lecture on Introduction to MATLAB
14/22
Polynomialsroots(p) Find the roots of a
coefficients are given in p
roots([1 4 2.1]) Find the roots of
x2+4x+2.1=0
polyval(p,v) Evaluate the polynomial
Mani 2012 14
given in p at x=v
-
8/3/2019 Lecture on Introduction to MATLAB
15/22
Help A good idea is use the help
help provides information about theavailable functions and how to use them.
Tr
Mani 2012 15
help eighelp inv
help roots
-
8/3/2019 Lecture on Introduction to MATLAB
16/22
Programming in MATLAB There are two types of MATLAB programs
% script file
P=[1 3 2]
function [y]=fun(x)
y=x^2+3*x^2+2
script files function files
Mani 2012 16
-
8/3/2019 Lecture on Introduction to MATLAB
17/22
Programming in MATLAB
Script files
A Script file contains a set of MATLAB
command Use them when you have a long sequence of
statements to solve a problem Can run the program by typing its name in
Mani 2012 17
editor window.
-
8/3/2019 Lecture on Introduction to MATLAB
18/22
Logical Operators> Greater than
>= Greater than or equal
< Less than
-
8/3/2019 Lecture on Introduction to MATLAB
19/22
Logical Operators& AND
| OR
~ NOT
Mani 2012 19
if (X>6) |(x
-
8/3/2019 Lecture on Introduction to MATLAB
20/22
If structures
General form: If (x>0)
If condition
statements
elsestatements
sign=1
elseif (x==0)
sign=0
else
sign=-1
Mani 2012 20
enen
-
8/3/2019 Lecture on Introduction to MATLAB
21/22
for loops
for index=initial: increment: limit
statements
end
s=0
for i=1:3:11
s=s+i
end
Mani 2012 21
-
8/3/2019 Lecture on Introduction to MATLAB
22/22
Example MATLAB program to find the roots of
% program 1 performs four iterations of
% Newtons Method
X=.7
X =1.1111
X =1.0483
X =
Result
Mani 2012 22
for i=1:4X=X (2*cos(X)-1)/(-2*sin(X))
end
1.0472X =
1.0472
top related