You are on page 1of 9

Computer Programming (MATLAB)

Dr. Samir Kale


Lecture 3
BITS Pilani Contact Session: 10.30-12.30
Pilani|Dubai|Goa|Hyderabad
BITS Pilani
Hyderabad Campus

Module: MATLAB features (contd)


3. MATLAB-First Program

MATLAB CODE

a = 20;
b = 10;
c = 30;

data = [a,b,c];

largest = data(1);

for i = 1:length(data) I

f data(i) > largest;

largest = data(i);
end
end

3 BITS Pilani, Deemed to be University under Section 3, UGC Act


Practice Problems and Examples-
Homework

2) Change the Current directory to mynewdir. Then open an Edit Window and add the
following lines

% Create an input array from -2*pi to 2*pi

t=-2*pi:pi/10:2*pi
% Calculate |sin(t)|
x=abs(sin(t));
% Plot result
plot(t,x)

4 BITS Pilani, Deemed to be University under Section 3, UGC Act


Practice Problems and Examples-
Homework

3) Think about what the results would be for the following expressions, and then type
them to verify your answers.

• 25 / 4 * 4
• 3+4^2
• 4 \ 12 + 4
• 3 ^ 2 (5 – 2) * 3

4) Using the colon operator, create the following vectors

• 3456
• 1.0000 1.5000 2.0000 2.5000 3.0000
• 5432

5 BITS Pilani, Deemed to be University under Section 3, UGC Act


Vectors and Matrices

Vectors and matrices are used to store sets of values, all of which are the same type. A vector
can be either a row vector or a column vector. A matrix can be visualized as a table of
values. The dimensions of a matrix are r × c, where r is the number of rows and c is the
number of columns. This is pronounced “r by c.” If a vector has n elements, a row vector
would have the dimensions 1 × n, and a column vector would have the dimensions n × 1.
A scalar (one value) has the dimensions 1 × 1. Therefore, vectors and scalars are actually
just subsets of matrices. Here are some diagrams showing, from left to right, a scalar, a
column vector, a row vector, and a matrix:

6 BITS Pilani, Deemed to be University under Section 3, UGC Act


Vectors and Matrices

A particular element in a vector is accessed using the name of the vector variable and the
element number (or index, or subscript) in parentheses. In MATLAB, the indices start
at 1. Normally, diagrams of vectors and matrices show the indices; for example, for
the variable newvec created earlier the indices 1–10

7 BITS Pilani, Deemed to be University under Section 3, UGC Act


Vectors and Matrices

Enter a row in MATLAB : Use ; a=[1,2,3;4,5,6]


Enter a column in MATLAB : Use , a=[1,2,3,4,5,6]

Use : if numbers are in series >> a=1:5

Use : to indicate all rows for example a(:,1) will display all rows of first column

Use : to indicate all columns for example a(1,:) will display all columns of first column

You can use as per convenience , Use of end statement

8 BITS Pilani, Deemed to be University under Section 3, UGC Act


Discussion Points –Contact Session 2

Display Options
Matlab Command ratio Comments
format short 50.8333 4 decimal digits
format long 50.83333333333334 14 decimal digits
format short e 5.0833e+001 4 decimal digits plus exponent
format long e 5.083333333333334e+001 14 decimal digits plus exponent
format short g 50.8333 better of format short or
format short e (default), switching for ans > 1000
format long g 5.083333333333334e+001 better of format long or format
long e
format bank 50.83 2 decimal digits
format + positive, negative, or zero

9 BITS Pilani, Deemed to be University under Section 3, UGC Act

You might also like