You are on page 1of 5

Prof. Dr. K.

nl; ENVE 404; METU

Computer Lab Exercise-2


Introduction to Basics of Matlab Part 1
NOTE: % is the comment (i.e., explanation) and non-executable statement sign for
MATLAB. Anything after it on the same line is neglected by MATLAB compiler.
Rules for defining variables:
- The name of the variables must not include special characters (%,&,*,...) exception:
underscore (_)
- The first character has to be an alphabetic character (m3: is ok; but 3m: is not ok.)
- MATLAB distinguishes between capitals and lower case letters
CREATING VARIABLES
For example, the following statement creates a variable x that contains values ranging
from -1 to 1 in increments of 0.1:

x = -1:.1:1; % Define the range of x


increments of 0.1
>> x=-1:.1:1
Now define a variable y that contains values ranging from 0 to 20 in increments of 5:
y=0:5:20;
or you could also use the linspace function to generate data for x
>> help linspace

% read how it is used

Create an x-array of 100 samples between 0 and 4.


>> x=linspace(0,4*pi,100)

MATRICES AND VECTORS:

2 1
A= 5 4
7 9

% A is a 3 by 2 matrix; i.e., 3 rows and 2 columns.

Matlab input command is:


>> A=[2 1;5 4;7 9]

Prof. Dr. K. nl; ENVE 404; METU

Special Cases: vectors and scalars


A vector is a special case of a matrix, with just one row or one column. It is entered
the same way as a matrix.
Examples: u=[1 3 9] produces a row vector,
v=[1;3;9] produces a column vector.

A scalar does not need brackets.

Example: g=9.81
Indexing (or Subscripting)
A(i,j) in MATLAB refers to the element aij of matrix A, i.e. the element in the ith row and jth
column.
A(m:n,k:l) specifies rows m to n and columns k to l of matrix A.
A(:,5:20) refers to the elements in column 5 through 20 of all the rows of the matrix A.
Example:
>> A=[1 2 3;4 5 6;7 8 9]
A=
1 2 3
4 5 6
7 8 9
>> A(2,3)
ans =
6
>> A(3,3)=10
A=
1 2 3
4 5 6
7 8 10
>> B=A(2:3,1:3)
B=
4 5 6
7 8 10
>> B=A(2:3,:)
B=
4 5 6
7 8 10

Prof. Dr. K. nl; ENVE 404; METU

>> B(:,2)=[]
B=
4 6
7 10

% a row or a column of a matrix is deleted by setting it to a null vector []

Transpose
The transpose of a matrix A is obtained by typing A.
>> A'
ans =
1 4
2 5
3 6

7
8
10

>> u=1:2:9
% This tell MATLAB to create a vector of values running from 1 to 9 in
increments of 2.
u=
1 3 5 7 9
>> v=u(2:4)'
v=
3
5
7
Inverse
The inverse of a square matrix C is obtained by typing inv(C) in the command window.
>> C=[2 4 6;10 15 21;0 3 1];
>> D=inv(C)
D=
-1.0909 0.3182 -0.1364
-0.2273 0.0455 0.4091
0.6818 -0.1364 -0.2273
>> I=C*D

% multiplication of matrix and its inverse gives the identity matrix.

I=
1.0000 0.0000
0
-0.0000 1.0000 -0.0000
0 0.0000 1.0000
Concatenation
Concatenation is the process of joining small matrices to make bigger ones.

Prof. Dr. K. nl; ENVE 404; METU

>> x=[1 2];


>> y=[8 9];
>> z=[x y]
z=
1 2 8
>> h=[x;y]
h=
1 2
8 9

MATHEMATICAL OPERATIONS ON VECTORS:


You can perform mathematical operations on vectors. For example, to square the elements
of the vector v type:
>> v=[3;4;5;6];
>> v^2
??? Error using ==> mpower
Inputs must be a scalar and a square matrix.
% Typing v^2 would tell MATLAB to use matrix multiplication to multiply v by itself and
would produce an error message. You must type .* or ./ if you want to multiply or divide
vectors element by element.
> v.^2
ans =
9
16
25
36
SYMBOLIC COMPUTATIONS
Using MATLABs Symbolic Math Toolbox, you can carry out the symbolic calculations related
to calculus (differentiation, integration, etc), linear algebra (matrix inversion, determinant
calculation, etc), simplifications (expanding, factoring polynomials, etc) solving equations
(algebraic, differential equations, etc), special functions (gamma, error functions, etc),
trigonometric functions (sin, cos, etc). In order to list the available math tools and to see
how they are used type the following in the command window:
>> help symbolic
To perform symbolic computations, you must use the syms command to declare the
variables you plan to use to be symbolic variables. Following are some examples related to
simplification of polynomial expansion or factoring:
>> syms x y

Prof. Dr. K. nl; ENVE 404; METU

>> (x+y)*(x+y)^2
ans =
(x+y)^3
>> expand(ans)
ans =
x^3+3*x^2*y+3*x*y^2+y^3
>> factor(ans)
ans =
(x+y)^3
>> simplify((x^2-y^2)/(x-y))
ans =
x+y
We will try later solve and dsolve math tools for symbolic solutions of algebraic and
differential equations.
GRAPHING WITH MATLAB
>> help plot % read the documentation
graph the function y = x3 over the x domain -1 to 1. The first step is to generate the data to
plot.
>> x=-1:.1:1; % Define the range of x

Note the semicolon(;)


>> y=x.^3;
% Raise each element in x to the third power
>> plot(x,y)
>> close
% closes figures
>> plot(x,y,'-r',x,y,'ob')
% plots y vs x with a solid black line interpolating blue circle at
the data point.
At the bottom of the documentation of plot click title and read
>> title('my figure')
Click xlabel and ylabel and read
>> xlabel('x axis')
>> ylabel('y axis (x.^3)')
You can also use MATLAB plotting tools for graphing
>> plottools
NOTE: Read the documentations from Getting Started->Graphics->Owerview of Plotting->
Plotting Process (Creating a Graph)->Examples -- Using MATLAB Plotting Tools

You might also like