Computer Programming (MATLAB)
Dr. Samir Kale
Lecture 1
BITS Pilani Contact Session: 10.30-12.30
Pilani|Dubai|Goa|Hyderabad
BITS Pilani
Hyderabad Campus
Module: Introduction to MATLAB features
Outcomes for the course
1. Students will be able to understand the MATLAB applications in numeric
computation and algorithm development
2. Students will be able to write MATLAB code for application in Control
Systems, Statistics Finite Element Method
3. Students will be able to write MATLAB script to understand the response of
a process with respect to the given input
Textbook and Reference Book
T1 Stephen J. Chapman Matlab Programming for Engineers, 4th Ed. Cengage
Learning.
T2 Stormy Attaway Matlab: A Practical Introduction to Programming and
Problem Solving
R1 Amos Gilat MATLAB An Introduction with Applications, 5th Edition Wiley
Publications
TA ZC -164 L-1 BITS Pilani, Pilani Campus
BASICS OF MATLAB
1st Contact Hour : 10.30-11.30
Function
Scalars Arrays
MATLAB MATLAB s and
and
Features Desktop Program
Matrices
s
TA ZC -164 L-1 BITS Pilani, Pilani Campus
MATLAB Free Version Installation
1. https://in.mathworks.com/products/matlab.html
2. https://in.mathworks.com/campaigns/products/trials.html?
prodcode=ML
3. Put your Email and Download the trial license, use BITS email only
4. Ensure you choose 64 bit
5. Download on the system and Start working
TA ZC -164 L-1 BITS Pilani, Pilani Campus
MATLAB Features and Significance
1. Ease of use, Platform Independence, Predefined functions, GUI, Plotting, MATLAB
Compiler
2. Applied Fields:
– Comprises of Mathematical Functions
– Ability to solve Differential Equations
– Ability for better interface with other software
– Gaussian Waves
– Wavelet Analysis
– Statistics
– Earth sciences & Neurosciences
– Modeling and Simulation
– And Many More
TA ZC -164 L-1 BITS Pilani, Pilani Campus
MATLAB Desktop
TA ZC -164 L-1 BITS Pilani, Pilani Campus
1. MATLAB-Basic Syntax
Operator Purpose
+ Plus; addition operator.
- Minus; subtraction operator.
* Scalar and matrix multiplication operator.
.* Array multiplication operator.
^ Scalar and matrix exponentiation operator.
.^ Array exponentiation operator.
\ Left-division operator.
/ Right-division operator.
.\ Array left-division operator.
8 TA ZC -164 L-1 BITS Pilani, Pilani Campus
MATLAB-Basic Syntax
Operator Purpose
./ Array right-division operator.
: Colon; generates regularly spaced elements and represents an entire row or column.
() Parentheses; encloses function arguments and array indices; overrides precedence.
[] Brackets; enclosures array elements.
. Decimal point.
… Ellipsis; line-continuation operator
, Comma; separates statements and elements in a row
; Semicolon; separates columns and suppresses display.
% Percent sign; designates a comment and specifies formatting.
= Assignment operator.
9 BITS Pilani, Deemed to be University under Section 3, UGC Act
2. MATLAB-Hierachy of Operations
In MATLAB, and many other programming languages, operations are performed in the
following order:
•expressions in brackets: ( ) ;
•powers: ^ ;
•multiplication and division: * , / ;
•addition and subtraction: + , - .
• Operations of the same precedence, for example multiplication and division,
are evaluated from left to right.
In ordinary written mathematics we sometimes leave out brackets and rely on the fact that
an intelligent human will understand where they ought to go. In contrast, computers obey
exactly the rules for evaluating expressions. If you are unsure, adding some extra brackets
(parentheses) will not hurt
10 BITS Pilani, Deemed to be University under Section 3, UGC Act
2. MATLAB-Arithmatic Expressions
MATLAB Window Result
>> 5+6 ans=11 { ans is default variable name}
>> a=5 c=11
>> b=6
>>c=a+b Or >>plus(a,b)
>> 1+2*3 ans =7
>>(1+2)*3 >>? solve
>> 4/2+1 >> ans=3
>> 1+4/2 >> ? solve
>> 8^2/3 >> ans=21.33
>> 8^(2/3)
>> 12/2/3 >> ? Solve
>> 12/(2*3) >> 2
>> 4^3^2 >> ans=4096
>> 4^(3^2) >> ans=262144
11 BITS Pilani, Deemed to be University under Section 3, UGC Act
3. MATLAB-Variables
Variable names are an example of identifier names. We will see other examples
The name must begin with a letter of the alphabet. After that, the name can contain
letters, digits, and the underscore character (e.g., value_1), but it cannot have a space.
There is a limit to the length of the name; the built-in function Name length max tells how many
characters this is.
MATLAB is case-sensitive. That means that there is a difference Between upper- and lowercase
letters. So, variables called mynum , MYNUM, and Mynum are all different.
There are certain words called reserved words that cannot be used as variable
names. Names of built-in functions can, but should not, be used as variable names.
pi =3.14159….
i= 1
j= 1
inf =infinity ∞
NaN= stands for “not a number”; e.g., the result of
0/0
12 BITS Pilani, Deemed to be University under Section 3, UGC Act
3. MATLAB-Commands
>> whos
>>clc
>> doc
>> rand
>>disp
>>input
>>fprintf
Sample Script
% This script calculates the area of a circle
% It prompts the user for the radius
% Prompt the user for the radius and calculate
% the area based on that radius
radius = input(‘Please enter the radius: ’);
area = pi * (radius^2);
% Print all variables in a sentence format
fprintf(‘For a circle with a radius of %.2f,’,radius)
fprintf(‘the area is %.2f\n’,area)
13 BITS Pilani, Deemed to be University under Section 3, UGC Act
3. MATLAB-First Program
#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if( n1>=n2 && n1>=n3 )
printf("%.2f is the largest number.", n1);
MATLAB CODE
if( n2>=n1 && n2>=n3 )
printf("%.2f is the largest number.", n2);
a = 20;
If( n3>=n1 && n3>=n2 ) b = 10;
printf("%.2f is the largest number.", n3); return 0; c = 30;
data = [a,b,c];
}
largest = data(1);
for i = 1:length(data) I
f data(i) > largest;
largest = data(i);
end
end
14 BITS Pilani, Deemed to be University under Section 3, UGC Act
MATLAB-First Program
Physicists tell us that altitude h in feet of a projectile t seconds after firing is
h= -16t2 +vot +h0
where vo is the initial velocity in ft/s and ho is altitude in from which it is fired. If a
rocket is launched from a hilltop 2400 ft above the desert with an initial upward
velocity of 400 ft/s, then when will it land on the desert
15 BITS Pilani, Deemed to be University under Section 3, UGC Act
Practice Problems and Examples-
Homework
16 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)
17 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
18 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:
19 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
20 BITS Pilani, Deemed to be University under Section 3, UGC Act