You are on page 1of 33

Introduction to MATLAB

AASTU

AASTU, Adem G.
Matrices and Matrix Computations
 MATLAB treats all variables as matrices
 Matrices are assigned to expressions with an ’=’ sign

 Names are case sensitive

Example: To create:  1 2 3 
A   4 5 6 
 7 8 9
Enter the following:
>> A= [1 -2 3;-4 5 6; 7 8 -9]

February 2021 AASTU, Adem G.


Cont’d
A=
1 -2 3
-4 5 6
7 8 -9
 New rows are indicated by a new line or a semicolon.

 Elements of a matrix can be expressions:

>> C= [1 1+1 9/3 2^3 2\8]


C=
1 2 3 8 4

February 2021 AASTU, Adem G.


Definition of an array

x=a: step: b
Examples:
>> x=0:2:10
x=
0 2 4 6 8 10
>> y=10:-2:1
y=
10 8 6 4 2
February 2021 AASTU, Adem G.
Some special matrices generated by
built-in functions

>> w=ones (2) % a 2x2 matrix of 1s


w=
1 1
1 1
>> x=zeros(2,4)%a 2x4 matrix of 0s
x=
0 0 0 0
0 0 0 0
February 2021 AASTU, Adem G.
Cont’d

>> z=eye(2)%a 2x2 identity matrix


z=
1 0
0 1

February 2021 AASTU, Adem G.


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
February 2021 AASTU, Adem G.
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
February 2021 AASTU, Adem G.
The “linspace” Function

linspace (first value, last value, num values)


first value = Starting
last value = Ending
num values= No. of elements to be considered
>> odds=linspace(1,9,5)
odds =
1 3 5 7 9

February 2021 AASTU, Adem G.


Some Useful Matrix Functions

 det(A) - determinant
 eig(A) - eigenvalues & eigenvectors
 inv(A) - inverse
 rank - rank
 lu - LU decomposition
 qr - QR factorization

February 2021 AASTU, Adem G.


Matrix Operations

1. Transpose
2. Scalar Multiplication
3. Addition (Component by component)
4. Multiplication
5. Division
6. Power
7. Component by component Multiplication,
Division and Power
February 2021 AASTU, Adem G.
Polynomials

 Polynomials are specified by a vector whose


entries are the coefficients of the polynomial
Example: The polynomial f ( x) = x3+2x 2− 10 x+ 1
is specified by [1 2 -10 1].
>> f=[1 2 -10 1] ;

February 2021 AASTU, Adem G.


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
February 2021 AASTU, Adem G.
The ‘disp’ and ‘fprintf’ Functions

1. disp
disp(A) displays the array, without printing the
name
If A is a string, the text is displayed
Example:
>> disp('Newton Interpolation Formula')
Newton Interpolation Formula

February 2021 AASTU, Adem G.


Cont’d

 disp also allows us to print numerical


values placed in text strings
Example:
>> itr=15;
>> disp(['Newtons Method doesnot converge
in',num2str(itr),'iterations']) displays
Newtons Method doesnot converge in 15
iterations

February 2021 AASTU, Adem G.


Cont’d

2. fprintf
(‘filename’,’format’,list)
where file name is optional
format=format control string containing
conversion specifications or any optional text
conversion specifications control the output of
array elements.

February 2021 AASTU, Adem G.


Common conversion specifications

 %P.Qe for exponential


 %P.Qf for fixed point
 %P.Qg to automatically select the shorter of
%P.Qe or %P.Qf
where P and Q are integers that set the field
width and the number of decimal places
respectively.
 list= a list of variable names separated by
commas.
February 2021 AASTU, Adem G.
Cont’d

 ‘\n’ produces a new line


>> x=4/3; y=5/6;
>> fprintf('\n x=%1.3f\n y=%1.5f\n',x,y)
x=1.333
y=0.83333

February 2021 AASTU, Adem G.


2-D Plots

 Two dimensional graphs of functions are


generated by the command plot.
2
Example: Plot y = x + 1 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.

February 2021 AASTU, Adem G.


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)

February 2021 AASTU, Adem G.


Cont’d

 Multiple plots on a single graph:


>> x=-2:0.1:2;
>> y1=x.^2+1;
>> y2=x.^2-1;
>> plot(x,y1,x,y2)

February 2021 AASTU, Adem G.


3-D Mesh Plots

 3-D mesh surface plots are drawn with the


function mesh.
2 2
Example: Graph of z= e − y over the square
− x

[-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.


February 2021 AASTU, Adem G.
Function Files

 Some elementary mathematical MATLAB


functions:
Trigonometric: sin, cos, tan
sind, cosd, tand, …
Exponential: exp (natural exp)
log (natural log)
log10 (common log)
sqrt (square root)
Complex: abs (absolute value)
February 2021 AASTU, Adem G.
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

February 2021 AASTU, Adem G.


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.

February 2021 AASTU, Adem G.


Polynomial
(i) derivative:
>>polyder(p)
(ii) the derivative of the product of the
polynomials A and B:
>>polyder(A,B)
Example: A = [3 6 9]; B = [1 2 0];
>>polyder(A, B)
ans =
12 36 42 18
AASTU, Adem G. 26
AASTU, Adem G. 27
c. Integration
A polynomial representing the integral of polynomial p,
>> polyint(p) assumes a constant of integration c=0.

Example: p = [1 3 -4 5]
ans =
0.2500 1.0000 -2.0000 5.0000 0.0000

AASTU, Adem G. 28
Fzero
To find a zero of fun near x0, if x0 is a scalar, we use
fzero((@fun, x0 )

fun is the function whose zero is to be computed.

AASTU, Adem G. 29
Example 1: Calculate π by finding the zero of the sine
function near 3.
>> fzero(@sin,3)
ans = 3.1416
Example 2: To find the zero of cosine between 1 and 2
>> fzero(@cos,[1 2])
ans = 1.5708
Note that cos(1) and cos(2) differ in sign.

AASTU, Adem G. 30
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-5; Then
find the zero near :
>> fzero(f,1)
ans = 2.0946
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])
AASTU, Adem G. 31
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
AASTU, Adem G. 32
Last

Thank you!

February 2021 AASTU, Adem G.

You might also like