Lab Session 1:
Introduction to MATLAB
Matrices and Matrix Computations
 MATLAB treats all variables as matrices
 Matrices are assigned to variables with an ’=’
sign
 Names are case sensitive
Example: Enter
1 2 3
4 5 6
7 8 9
A

 
 
 
 
 

 
5/15/2019
2
Cont‟d
>> A= [1 -2 3;-4 5 6; 7 8 -9]
A =
1 -2 3
-4 5 6
7 8 -9
 New rows are indicated by a new line or a
semicolon.
5/15/2019
3
Cont‟d
Example: Enter the column vector
>> B=[1;-4;7] Or >> B= [1 -4 7]'
B = B =
1 1
-4 -4
7 7
1
4
7
B
 
 
 
 
 
 
5/15/2019
4
Cont‟d
 Elements of a matrix can be expressions:
>> C= [1 1+1 9/3 2^3]
C =
1 2 3 8
5/15/2019
5
Definition of an array
where a = first value, h= step size & b= last value
Examples:
>> 0:2:10
ans =
0 2 4 6 8 10
>> 10:-2:1
ans =
10 8 6 4 2
a:h:b
5/15/2019
6
The “linspace” Function
where a = first value, b= last value & n = # of
elements to be considered
>> linspace(1,9,5)
ans =
1 3 5 7 9
linspace (a, b, n)
5/15/2019
7
Some special matrices generated by
built-in functions
>> ones (2) % a 2x2 matrix of 1s
ans =
1 1
1 1
>> zeros(2,4)%a 2x4 matrix of 0s
ans =
0 0 0 0
0 0 0 0
5/15/2019
8
Cont‟d
>> eye(3)%a 3x3 identity matrix
ans =
1 0 0
0 1 0
0 0 1
5/15/2019
9
The „diag‟ Function
1. Creates a matrix with specified values on the
(main) diagonal
>> d=[1 2 3];
>> D=diag(d)
D =
1 0 0
0 2 0
0 0 3
5/15/2019
10
Cont‟d
Note: the’;’ in “d=[1 2 3];” is used to suppress
the echoing of the input (intermediate results)
2. Extracts the diagonal entries
>> C=[1 -2 3;-4 5 6;7 8 -9];
>> c=diag(C)
c =
1
5
-9
5/15/2019
11
Some Useful Matrix Functions
 inv - inverse
 rank - rank
 eig - eigenvalues & eigenvectors
 lu - LU decomposition( in partial
pivoting sense)
 qr - QR factorization
5/15/2019
12
Cont‟d
For example,
 >> [U, D]=eig(A)
generates a matrix U whose columns are
eigenvctors of A & a diagonal matrix D
with the eigenvalues of on its diagonal.
 [L U P]=lu(A)
returns unit lower triangular matrix L, upper
triangular matrix U, and permutation matrix P so
that P*A = L*U.
5/15/2019
13
Matrix Operations
1. Transpose
2. Scalar Multiplication
3. Addition
4. Multiplication & Dot product of vectors
5. Division
6. Power
7. Component by component Multiplication,
Division and Power
5/15/2019
14
Polynomials
 Polynomials are specified by a vector whose
entries are the coefficients of the polynomial
Example: is specified by
[1 2 -10 1].
>> f=[1 2 -10 1] ;
1
10
2 2
3



 x
x
x
y
5/15/2019
15
Cont‟d
>> polyval(f,-2)%evaluates the polynomial at x=-2
ans =
21
>> roots(f)%finds the roots of a polynomial
ans =
-4.3511
2.2489
0.1022
5/15/2019
16
Output Formats
 All calculations in MATLAB are performed
in double precision. But the format of the
displayed output can be controlled by the
following commands:
 format short = Fixed point with 4 decimal
places (is the default format)
 format long = Fixed point with 15 decimal
places
 format short e = Scientific notation with4
decimal places
5/15/2019
17
Cont‟d
 format long e = Scientific notation with 15
decimal places
 format rat = approximation by ratio of
small integers
Once initiated, the chosen format remains
in effect until changed.
5/15/2019
18
Function Files
 Some elementary mathematical MATLAB
functions:
Trigonometric: sin, cos, tan
Exponential: exp - Natural exp
log - Natural log
log10 -Common log
sqrt -square root
abs - absolute value
5/15/2019
19
Lab Session 2:
Introduction to MATLAB -
Continued
2-D Plots
 Two dimensional graphs of functions are
generated by the command plot.
Example: Plot over the interval [-2, 2].
>> x = -2 : 0.25 : 2;
>> y = x.^2+1;
>> plot(x,y)
 The command plot( x, y, 'r+:') results in red
color with + for points and dotted lines.
1
2

 x
y
5/15/2019 21
Cont‟d
 Use help plot for more options.
 Other features can be added with commands
such as grid, xlabel, ylable, title, etc.
 Plots of parametrically defined curves can
also be made. For example:
 >> t = 0 : 0.1 :4*pi;
 >> x = cos(2*t);
 >>y = sin(3*t);
 >> plot(x,y)
5/15/2019 22
Multiple plots on a single graph
Example:
>> x=-2:0.1:2;
>> y1=x.^2+1;
>> y2=x.^2-1;
>> plot(x,y1,x,y2)
5/15/2019 23
Multiple plots on a single
graph
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
-1
0
1
2
3
4
5
5/15/2019
24
3-D Mesh Plots
 3-D mesh surface plots are drawn with the
function mesh.
Example: Graph of over the square
[-4, 4] X [-4, 4].
>> [x y]=meshgrid(-4:0.25:4,-4:0.25:4);
>> z=exp(-x.^2-y.^2);
>> mesh(x,y,z)
 Other commands related to 3-D graphs are
plot3 and surf.
2
2
y
x
e
z 


5/15/2019
25
3-D Mesh Plots
-4
-3
-2
-1
0
1
2
3
4
-4
-3
-2
-1
0
1
2
3
4
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
5/15/2019
26
Cont‟d
 A complete list of elementary functions can
be obtained with the command:
>> help elfun
Note: All function names are in lower case.
 MATLAB functions may have single or
multiple arguments
5/15/2019
27
User Defined Functions
 MATLAB allows users to define new
functions by constructing an M-file in the
M-file Editor/Debugger
function y = function name(input arguments)
y = ………………………..
5/15/2019
28
Example:
Define
function y=f(x)
y=2*x.^2+1
Once a function is saved as an M-file (as f.m),
we can use MATLAB command window to
compute. For example,
>> f(2)
ans =
9
2
( ) 2 1
f x x
 
5/15/2019
29
Relations and Loops
 Relational operators in MATLAB:
= =, < =, ~ =, < , >
 Logical Operators:
& = and ! = or ~ = not
5/15/2019
30
Loops-Flow control statements
(if, for & while statements)
1. if
General Form :
if (expression)
statements
else if (expression)
statements
else
statements
end
5/15/2019
31
Cont‟d
2. for:
for(loop variable=loop exp)
statements
end
3. while:
while(while expression)
condition statement
end
5/15/2019
32
Example:
Write a MATLAB program which displays the sum of
the first 100 positive integers.
Solution:
>> sum=0;
>> for i=1:100
sum = sum +i;
end
>> sum
sum =
5050
5/15/2019
33
Last
Extra !!!!!!!!!!!!
5/15/2019 34
35
Polynomial
a. derivative:
>>k = polyder(p)
b. the derivative of the product of the
polynomials a and b:
>>k = polyder(a,b)
Example: a = [3 6 9]; b = [1 2 0];
>>k = polyder(a,b)
k =
12 36 42 18
5/15/2019
36
5/15/2019
37
c. Integration
A polynomial representing the integral of polynomial p,
>> polyint(p) assumes a constant of integration c=0.
Example: p = [1 -6 -72 -27]
ans =
0.2500 -2.0000 -36.0000 -27.0000
D . To find a zero of fun near x0, if x0 is a scalar, we use
>> x = fzero((@myfun, x0 )
 fun is the function whose zero is to be computed.
5/15/2019
38
Example 1: Calculate π by finding the zero of the sine
function near 3.
>> x = fzero(@sin,3)
ans. x = 3.1416
Example 2: To find the zero of cosine between 1 and 2
>> x = fzero(@cos,[1 2])
ans. x = 1.5708
Note that cos(1) and cos(2) differ in sign.
5/15/2019
Example 3
To find a zero of the function f(x) = x3 – 2x – 5, write
an anonymous function f: f = @(x)x.^3-2*x-4; Then
find the zero near :
>>z = fzero(f,1)
z = 2.0000
Example 4: f=@(x)-11-22*x+17*x^2-2.5*x^3;
>> x = fzero(f,3) x = 2.4269
or >>x = fzero(@(x)-11-22*x+17*x^2-2.5*x^3,3)
or x = fzero(@(x)-11-22*x+17*x^2-2.5*x^3,[2 3])
5/15/2019 39
40
Polynomial Interpolation
Example: Here are two vectors representing the census years
from 1900 to 1990 and the corresponding United States
population in millions of people.
t = 1900:10:1990;
p = [75.995 91.972 105.711 123.203 131.669 150.697
179.323 203.212 226.505 249.633];
>>interp1(t,p,1975)
The expression interpolates within the census data to estimate the
population in 1975. The result is
ans = 214.8585
5/15/2019
.
Graphs
5/15/2019 41
Sphere
42
 a = 2;
 u = linspace(0, 2*pi, 41);
 v = linspace(-pi/2, pi/2, 31);
 [U,V] = meshgrid(u,v);
 X = a*cos(V).*cos(U);
 Y = a*cos(V).*sin(U);
 Z = a*sin(V);
 surf(X,Y,Z); colormap(gray); shading flat
5/15/2019
Cylindrer
43
 z = linspace(0,1,41);
 cylinder(3*(z-1/3).^2)
-2
-1
0
1
2
-2
-1
0
1
2
0
0.2
0.4
0.6
0.8
1
5/15/2019
 >> [x,y]=meshgrid(-4:0.1:4,-4:0.1:4);
 >> z=sqrt(x.^2+y.^2);
 >> mesh(x,y,z);
 >> hold on;
 >> w=-sqrt(x.^2+y.^2);
 >> mesh(x,y,w);
 >> hold off
5/15/2019 44
-4
-2
0
2
4
-4
-3
-2
-1
0
1
2
3
4
-6
-4
-2
0
2
4
6
5/15/2019 45
Cont…
46
 syms s t
 % vertical cylinder of radius 1
 x = cos(s);
 y = sin(s);
 z = t;
 ezsurf(x,y,z, [0 2*pi -2 2])
 hold on
5/15/2019
vertical cylinder of radius 1
.
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
-2
-1
0
1
2
x
x = cos(s), y = sin(s), z = t
y
z
5/15/2019 47
Two intersecting cylinders
5/15/2019 48
 u = linspace(0,2*pi,41);
 v = linspace(-2,2,41)
 [U,V] = meshgrid(u,v);
 % vertical cylinder of radius 1
 surf(cos(U), sin(U), V);
 hold on
 % horizontal cylinder of radius .5
 surf(.5*cos(U), V, .5*sin(U))
 hold off
5/15/2019
-1
-0.5
0
0.5
1
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
y
x = cos(s), y = sin(s), z = t
x
z
49
Torus
50
 a = .5; r = 2;
 u = linspace(0, 2*pi, 41); v = u;
 [U,V] = meshgrid(u,v);
 X = cos(U).*(r + a*cos(V));
 Y = sin(U).*(r + a*cos(V));
 Z = a*sin(V);
 surf(X,Y,Z);
5/15/2019
.
 Torus
 END!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-2.5
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
2.5
-2.5
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
2.5
-0.5
-0.4
-0.3
-0.2
-0.1
0
0.1
0.2
0.3
0.4
0.5
5/15/2019 51

computational brrtyuuufdddfgggxxzzcv.pdf

  • 1.
  • 2.
    Matrices and MatrixComputations  MATLAB treats all variables as matrices  Matrices are assigned to variables with an ’=’ sign  Names are case sensitive Example: Enter 1 2 3 4 5 6 7 8 9 A               5/15/2019 2
  • 3.
    Cont‟d >> A= [1-2 3;-4 5 6; 7 8 -9] A = 1 -2 3 -4 5 6 7 8 -9  New rows are indicated by a new line or a semicolon. 5/15/2019 3
  • 4.
    Cont‟d Example: Enter thecolumn vector >> B=[1;-4;7] Or >> B= [1 -4 7]' B = B = 1 1 -4 -4 7 7 1 4 7 B             5/15/2019 4
  • 5.
    Cont‟d  Elements ofa matrix can be expressions: >> C= [1 1+1 9/3 2^3] C = 1 2 3 8 5/15/2019 5
  • 6.
    Definition of anarray where a = first value, h= step size & b= last value Examples: >> 0:2:10 ans = 0 2 4 6 8 10 >> 10:-2:1 ans = 10 8 6 4 2 a:h:b 5/15/2019 6
  • 7.
    The “linspace” Function wherea = first value, b= last value & n = # of elements to be considered >> linspace(1,9,5) ans = 1 3 5 7 9 linspace (a, b, n) 5/15/2019 7
  • 8.
    Some special matricesgenerated by built-in functions >> ones (2) % a 2x2 matrix of 1s ans = 1 1 1 1 >> zeros(2,4)%a 2x4 matrix of 0s ans = 0 0 0 0 0 0 0 0 5/15/2019 8
  • 9.
    Cont‟d >> eye(3)%a 3x3identity matrix ans = 1 0 0 0 1 0 0 0 1 5/15/2019 9
  • 10.
    The „diag‟ Function 1.Creates a matrix with specified values on the (main) diagonal >> d=[1 2 3]; >> D=diag(d) D = 1 0 0 0 2 0 0 0 3 5/15/2019 10
  • 11.
    Cont‟d Note: the’;’ in“d=[1 2 3];” is used to suppress the echoing of the input (intermediate results) 2. Extracts the diagonal entries >> C=[1 -2 3;-4 5 6;7 8 -9]; >> c=diag(C) c = 1 5 -9 5/15/2019 11
  • 12.
    Some Useful MatrixFunctions  inv - inverse  rank - rank  eig - eigenvalues & eigenvectors  lu - LU decomposition( in partial pivoting sense)  qr - QR factorization 5/15/2019 12
  • 13.
    Cont‟d For example,  >>[U, D]=eig(A) generates a matrix U whose columns are eigenvctors of A & a diagonal matrix D with the eigenvalues of on its diagonal.  [L U P]=lu(A) returns unit lower triangular matrix L, upper triangular matrix U, and permutation matrix P so that P*A = L*U. 5/15/2019 13
  • 14.
    Matrix Operations 1. Transpose 2.Scalar Multiplication 3. Addition 4. Multiplication & Dot product of vectors 5. Division 6. Power 7. Component by component Multiplication, Division and Power 5/15/2019 14
  • 15.
    Polynomials  Polynomials arespecified by a vector whose entries are the coefficients of the polynomial Example: is specified by [1 2 -10 1]. >> f=[1 2 -10 1] ; 1 10 2 2 3     x x x y 5/15/2019 15
  • 16.
    Cont‟d >> polyval(f,-2)%evaluates thepolynomial at x=-2 ans = 21 >> roots(f)%finds the roots of a polynomial ans = -4.3511 2.2489 0.1022 5/15/2019 16
  • 17.
    Output Formats  Allcalculations in MATLAB are performed in double precision. But the format of the displayed output can be controlled by the following commands:  format short = Fixed point with 4 decimal places (is the default format)  format long = Fixed point with 15 decimal places  format short e = Scientific notation with4 decimal places 5/15/2019 17
  • 18.
    Cont‟d  format longe = Scientific notation with 15 decimal places  format rat = approximation by ratio of small integers Once initiated, the chosen format remains in effect until changed. 5/15/2019 18
  • 19.
    Function Files  Someelementary mathematical MATLAB functions: Trigonometric: sin, cos, tan Exponential: exp - Natural exp log - Natural log log10 -Common log sqrt -square root abs - absolute value 5/15/2019 19
  • 20.
    Lab Session 2: Introductionto MATLAB - Continued
  • 21.
    2-D Plots  Twodimensional graphs of functions are generated by the command plot. Example: Plot over the interval [-2, 2]. >> x = -2 : 0.25 : 2; >> y = x.^2+1; >> plot(x,y)  The command plot( x, y, 'r+:') results in red color with + for points and dotted lines. 1 2   x y 5/15/2019 21
  • 22.
    Cont‟d  Use helpplot for more options.  Other features can be added with commands such as grid, xlabel, ylable, title, etc.  Plots of parametrically defined curves can also be made. For example:  >> t = 0 : 0.1 :4*pi;  >> x = cos(2*t);  >>y = sin(3*t);  >> plot(x,y) 5/15/2019 22
  • 23.
    Multiple plots ona single graph Example: >> x=-2:0.1:2; >> y1=x.^2+1; >> y2=x.^2-1; >> plot(x,y1,x,y2) 5/15/2019 23
  • 24.
    Multiple plots ona single graph -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 -1 0 1 2 3 4 5 5/15/2019 24
  • 25.
    3-D Mesh Plots 3-D mesh surface plots are drawn with the function mesh. Example: Graph of over the square [-4, 4] X [-4, 4]. >> [x y]=meshgrid(-4:0.25:4,-4:0.25:4); >> z=exp(-x.^2-y.^2); >> mesh(x,y,z)  Other commands related to 3-D graphs are plot3 and surf. 2 2 y x e z    5/15/2019 25
  • 26.
  • 27.
    Cont‟d  A completelist of elementary functions can be obtained with the command: >> help elfun Note: All function names are in lower case.  MATLAB functions may have single or multiple arguments 5/15/2019 27
  • 28.
    User Defined Functions MATLAB allows users to define new functions by constructing an M-file in the M-file Editor/Debugger function y = function name(input arguments) y = ……………………….. 5/15/2019 28
  • 29.
    Example: Define function y=f(x) y=2*x.^2+1 Once afunction is saved as an M-file (as f.m), we can use MATLAB command window to compute. For example, >> f(2) ans = 9 2 ( ) 2 1 f x x   5/15/2019 29
  • 30.
    Relations and Loops Relational operators in MATLAB: = =, < =, ~ =, < , >  Logical Operators: & = and ! = or ~ = not 5/15/2019 30
  • 31.
    Loops-Flow control statements (if,for & while statements) 1. if General Form : if (expression) statements else if (expression) statements else statements end 5/15/2019 31
  • 32.
    Cont‟d 2. for: for(loop variable=loopexp) statements end 3. while: while(while expression) condition statement end 5/15/2019 32
  • 33.
    Example: Write a MATLABprogram which displays the sum of the first 100 positive integers. Solution: >> sum=0; >> for i=1:100 sum = sum +i; end >> sum sum = 5050 5/15/2019 33
  • 34.
  • 35.
    35 Polynomial a. derivative: >>k =polyder(p) b. the derivative of the product of the polynomials a and b: >>k = polyder(a,b) Example: a = [3 6 9]; b = [1 2 0]; >>k = polyder(a,b) k = 12 36 42 18 5/15/2019
  • 36.
  • 37.
    37 c. Integration A polynomialrepresenting the integral of polynomial p, >> polyint(p) assumes a constant of integration c=0. Example: p = [1 -6 -72 -27] ans = 0.2500 -2.0000 -36.0000 -27.0000 D . To find a zero of fun near x0, if x0 is a scalar, we use >> x = fzero((@myfun, x0 )  fun is the function whose zero is to be computed. 5/15/2019
  • 38.
    38 Example 1: Calculateπ by finding the zero of the sine function near 3. >> x = fzero(@sin,3) ans. x = 3.1416 Example 2: To find the zero of cosine between 1 and 2 >> x = fzero(@cos,[1 2]) ans. x = 1.5708 Note that cos(1) and cos(2) differ in sign. 5/15/2019
  • 39.
    Example 3 To finda zero of the function f(x) = x3 – 2x – 5, write an anonymous function f: f = @(x)x.^3-2*x-4; Then find the zero near : >>z = fzero(f,1) z = 2.0000 Example 4: f=@(x)-11-22*x+17*x^2-2.5*x^3; >> x = fzero(f,3) x = 2.4269 or >>x = fzero(@(x)-11-22*x+17*x^2-2.5*x^3,3) or x = fzero(@(x)-11-22*x+17*x^2-2.5*x^3,[2 3]) 5/15/2019 39
  • 40.
    40 Polynomial Interpolation Example: Hereare two vectors representing the census years from 1900 to 1990 and the corresponding United States population in millions of people. t = 1900:10:1990; p = [75.995 91.972 105.711 123.203 131.669 150.697 179.323 203.212 226.505 249.633]; >>interp1(t,p,1975) The expression interpolates within the census data to estimate the population in 1975. The result is ans = 214.8585 5/15/2019
  • 41.
  • 42.
    Sphere 42  a =2;  u = linspace(0, 2*pi, 41);  v = linspace(-pi/2, pi/2, 31);  [U,V] = meshgrid(u,v);  X = a*cos(V).*cos(U);  Y = a*cos(V).*sin(U);  Z = a*sin(V);  surf(X,Y,Z); colormap(gray); shading flat 5/15/2019
  • 43.
    Cylindrer 43  z =linspace(0,1,41);  cylinder(3*(z-1/3).^2) -2 -1 0 1 2 -2 -1 0 1 2 0 0.2 0.4 0.6 0.8 1 5/15/2019
  • 44.
     >> [x,y]=meshgrid(-4:0.1:4,-4:0.1:4); >> z=sqrt(x.^2+y.^2);  >> mesh(x,y,z);  >> hold on;  >> w=-sqrt(x.^2+y.^2);  >> mesh(x,y,w);  >> hold off 5/15/2019 44
  • 45.
  • 46.
    Cont… 46  syms st  % vertical cylinder of radius 1  x = cos(s);  y = sin(s);  z = t;  ezsurf(x,y,z, [0 2*pi -2 2])  hold on 5/15/2019
  • 47.
    vertical cylinder ofradius 1 . -1 -0.5 0 0.5 1 -1 -0.5 0 0.5 1 -2 -1 0 1 2 x x = cos(s), y = sin(s), z = t y z 5/15/2019 47
  • 48.
    Two intersecting cylinders 5/15/201948  u = linspace(0,2*pi,41);  v = linspace(-2,2,41)  [U,V] = meshgrid(u,v);  % vertical cylinder of radius 1  surf(cos(U), sin(U), V);  hold on  % horizontal cylinder of radius .5  surf(.5*cos(U), V, .5*sin(U))  hold off
  • 49.
  • 50.
    Torus 50  a =.5; r = 2;  u = linspace(0, 2*pi, 41); v = u;  [U,V] = meshgrid(u,v);  X = cos(U).*(r + a*cos(V));  Y = sin(U).*(r + a*cos(V));  Z = a*sin(V);  surf(X,Y,Z); 5/15/2019
  • 51.