You are on page 1of 32

Computer Programming (MATLAB)

Sudhanwa Kulkarni / Dr. Samir Kale


Lab Session 1: 31/01/2021
Time: 12.30 pm – 02:30 pm
BITS Pilani
Pilani | Dubai | Goa | Hyderabad
Lab Calendar
Session Start End Remarks

Practice session 1 01-Feb-2021 30-March-2021 Practice

Lab lecture 1 31-Jan-2021 Tutorials + On ramp

Lab lecture 2 07-Feb-2021 Tutorials

Lab lecture 3 14-Feb-2021 Tutorials

Lab lecture 4 21-Feb-2021 Tutorials

Practice session 2 09-March-2021 14-March-2021 Practice

Practice session 3 22-March-2021 02-April-2021 Practice

Review session 28-March-2021

Lab exam - regular 05-April-2021 11-April-2021 Remote proctoring (1 slot)

Lab exam - makeup 14-April-2021 20-April-2021 Remote proctoring (1 slot)

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


Content of Lab Sessions

Lab Session Content


I. Basics of MATLAB (a quick review)
Lab Session 01 (31st Jan. 2021) II. Optimization Problems
III. Roots of non-linear equations

I. Program to calculate efficiency of


various cycles like Carnot Cycle.
Lab Session 02 (7th Feb. 2021)
II. Solving linear simultaneous equations
III. Interpolation and Regression

I. User-defined function for numerical


integration (by using trapezoidal rule)
Lab Session 03 (14th Feb. 2021)
II. Numerical Integration and
Differentiation

Lab Session 04 (21st Feb. 2021) I. Excel file handling by MATLAB

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


Lab Evaluation and Grading

Total Marks for this Lab : 30

1)On–Ramp Tutorial : 5 Marks


2)Quizzes : 8 Marks
3)Assignment : 5 Marks
4)Lab Exam : 12 Marks

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


Link for Telegram Group

  
https://t.me/MATLAB_S2_2020
 
This group will be strictly used for Course related issues,
difficulty solving purposes only.

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


Accessing Virtual Lab

A presentation has already been uploaded in e-learn portal.

This presentation contains detailed step-by-step procedure regarding


how to access the virtual lab.

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


BITS Pilani
Hyderabad Campus

MATLAB Basics
MATLAB Environment: Various Windows and their use
Operators
Basic Matrix Operations
MATLAB Environment

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


Operators
NOTATION DESCRIPTION MATHEMATICAL EXAMPLES
+ ADDITION x = 10 + 2 = 12
- SUBTRACTION x = 10 – 2 =8
* MULTIPLICATION x = 10 * 2 = 20
/ x = 10 / 2 =5
\ DIVISION X = 10\2 = 0.2
^ POWER x = 10 ^ 2 = 100
( ) EVALUATION ORDER x = (10 + 2) * 3 = 36
sin( ) = 0.7071
Trigonometric Functions sin(pi/4)
cos( ) = 6.1232e-017
Angles must be in radians cos(pi/2)
tan( ) ≈0
sind( )
cosd( ) Trigonometric Functions
sind(45) = 0.7071
tand( ) Angles must be in degress

asin( )
Inverse Trigonometric
acos( ) asin(0.7071) = 0.7854
Functions
atan( )
9 BITS Pilani, Deemed to be University under Section 3, UGC Act
Operators

NOTATION DESCRIPTION MATHEMATICAL EXAMPLES


log( ) log(2) = 0.6931
Logarithmic Function
Log10( ) log10(2) = 0.3010
exp( ) Exponential Function (ex) exp(2) = 7.3891
Sqrt(A) SQUARE ROOT of A A sqrt(4) =2
nthroot(A, n) nth root of A n A nthroot(27,3) =3
pi Value of π pi * 2 = 6.2832
Inf INFINITY ( ∞ ) 3/0 = Inf

NaN Not A Number (UNDEFINED) 0/0 = NaN

abs(2) = 2
abs(z) ABSOLUTE VALUE of “z”
abs(-3) = 3

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


Matrices in MATLAB

Suppose we want to enter


following matrix A in
MATLAB

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


BITS Pilani
Hyderabad Campus

Lab Session 01
I.Optimization Problems
II. Roots of non-linear equations
Optimization
Optimization is determining the best solution out of all possible
solutions for the problem by satisfying restrictions/limitations to it.

e.g. (Non–technical example – Purchasing A Bike)


A person wants to buy a bike.
But he can spend at maximum Rs. 1,50,000/- only. At the same,
time he wants the average of the bike at least 55 km/liter.

In this problem, aim or objective is to buy a bike. But


the person can not buy any bike in the market because he
is having some restriction/limitations in terms of money
and average of bike.

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


Optimization
e.g. (A more technical example – Coca-Cola Can)
Determine dimensions of coca-cola can, as shown in the figure, having
minimum surface area so that it can contain at least 350 ml of coca-cola.
From previous experience it can be noted that diameter of can should be
in between 3.5 to 8 cm and height should be in between 8 to 18 cm.
Surface_Area = Cylindrical_Area + Top_Circular_Area +
Bottom_Circular_Area
SA = πDH + (2*(π/4)*D2)…… minimized (Aim) D
Volume=(π/4)*D2*H ……at least 350 (lim.)
D in between 3.5 cm to 8 cm (lim.)
H in between 8 cm to 18 cm (lim.) H
min. f = (πDH + (2*(π/4)*D2))
subjected to, ((π/4)*D2*H) ≥ 350
3.5 ≤ D ≤ 8; 8 ≤ H ≤ 18
14 BITS Pilani, Deemed to be University under Section 3, UGC Act
Optimization

A more formal definition of optimization 


Optimization is a process to determine values of decision
variables which will optimize (i.e. minimize/maximize) the
objective function and at the same time will satisfy all the
constraints.

Decision Variables: variables involved in the problem


Objective function: the function of decision variables
which is to be minimized or maximized
Constraints: the function of decision variables which
are limitations or restrictions to the problem
Bounds: these are restrictions on decision variables
from taking any value (from -∞ to +∞)

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


Types of Optimization
Problems
A. Based on No. of Decision Variables:
– Single Variable Problems
– Multi Variable Problems
B. Based on nature of Objective Function and Constraints
– Linear Problems (Linear Programming Problems)
– Non Linear Problems (NLP)
C. Based on Constraints
– Unconstrained Optimization Problem
– Constrained Optimization Problem
D. Based on No. of Objective Function
– Single Objective Optimization Problem
– Multi Objective Optimization Problem
16 BITS Pilani, Deemed to be University under Section 3, UGC Act
Linear Programming Problem
A furniture manufacturing company plans to 1) Variables
make two product chairs and tables from its Let,
x1 = No. of chairs produced
available resources, which consists of 400 board
x2=No. of tables produced
feet of mahogany timber and 450 man hours of 2) Objective Function
labour, It knows that to make a chair it requires 5 Maximize : 45x1+80x2
board feet and 10 man hours and yields a profit 3) Constraints
of Rs 45, while each table uses 20 board feet 5x1+20x2<=400
and 15 man-hours and has a profit of Rs 80. The 10x1+15x2<=450
problem is to determine how many chairs and 4) Non Negetivity Constraints
tables can be make company keeping within its X1, X2 >=0
resource constraints so that it maximizes profit Problem
Maximize : 45x1+80x2
Man Subjected to,
Wood Profit
Hours 5x1+20x2<=400
(in feet) (Rs.)
(in hr) 10x1+15x2<=450
Chair 5 10 45 X1, X2 >=0
In Matrix Form
Table 20 15 80
Maximum
Available 400 450
Resources
BITS Pilani, Deemed to be University under Section 3, UGC Act
Two Approaches

Problem Based Approach Solver Based Approach

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


Solving by Problem Based
Approach
Step 1: Define decision variables
In the current problem, x1 and x2 are the two decision variables
>> x = optimvar('x',2,'LowerBound‘,[0 0],'UpperBound‘,[inf inf])
Step 2: Define objective function
In the current problem, objective function is Maximize : 45x1+80x2
>> obj = (45 * x(1) + 80 * x(2));
Step 3: Define constraints (if any)
In the current problem, constraints are: 5x1+20x2 <= 400 and 10x1+15x2 <= 450
>> A = 5*x(1) + 20*x(2) <= 400;
>> B = 10*x(1) + 15*x(2) <= 450;
Step 4: Define Optimization Problem
>> prob = optimproblem('Objective',obj,'ObjectiveSense‘,'maximize')
>> prob.Constraints.con_1 = A;
>> prob.Constraints.con_2 = B;
Step 5: Solve the problem
>> x0.x = [0 0];  Defining initial starting point
>> [sol, fval, exitflag, A] = solve(prob,x0)

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


Solving by Solver Based
Approach

IMPORTANT NOTE
Objective function should be minimization while
following this approach. If it is a maximization
function then convert it in to a minimization
function by multiplying objective function by -1.

All the inequality constraints should be of kind


“less than or equal to (<=)”. If any of the
inequality constraint is of kind “greater than or
equal to (>=)” then convert it in to “less than or
equal to (<=)” by multiplying it by -1.
BITS Pilani, Deemed to be University under Section 3, UGC Act
Differences In Two
Approaches
Problem Based Solver Based
Parameters
Approach Approach

Automatic by
Selection of Solver By User
MATLAB

Form of equations Symbolic Matrix

In-equality Can be of any form Should be in the


Constraints ‘<=‘ or ‘>=‘ form of ‘<=’
Can not be defined
Sense of Objective
Can be defined and has to be
Function
minimize

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


Non-Linear Problem

Consider the nonlinear program

Minimize Z = X12 + 2X22

Subject to
X12 + X22 <= 5
2X1 – 2X2 = 1

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


Solving by Problem Based
Approach
Step 1: Define decision variables
In the current problem, x1 and x2 are the two decision variables
>> x =optimvar('x',2,'LowerBound‘,[-inf -inf],'UpperBound‘,[inf inf])
Step 2: Define objective function
In the current problem, objective function is Maximize : 45x1+80x2
>> obj = (x(1)^2 + 2 * x(2)^2);
Step 3: Define constraints (if any)
In the current problem, constraints are: 5x1+20x2 <= 400 and 10x1+15x2 <= 450
>> A = x(1)^2 + x(2)^2 <= 5;
>> B = 2*x(1) - 2*x(2) == 1;
Step 4: Define objective problem
>> prob = optimproblem('Objective',obj,'ObjectiveSense‘,'maximize')
>> prob.Constraints.con_1 = A;
>> prob.Constraints.con_2 = B;
Step 5: Solve the problem
>> x0.x = [0 0];  Defining initial starting point
>> [sol, fval, exitflag, A] = solve(prob,x0)

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


One more LPP

Minimize Z = (–1)*(X1 – 2X2 + 3X3)


Subjected to,
X1 + X2 + X3 ≤ 7
–X1 + X2 – X3 ≤ –2
3X1 – X2 – 2X3 = –5
X1, X2 ≥ 0

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


To be Solved by Student

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


Problem to be solved

A firm manufactures two types of products P1 and P2 and sells


them on a profit of Rs. 3 on type P1 and Rs. 4 on type P2. Each
product is processed on two machines A and B. Type P1
requires 2 minutes of processing time on A and one minute on
B; type P2 requires 3 minutes on A and 2 minutes on B. The
machine A is available for not more than 7 hours 30 minutes
while machine B is available for 12 hours during any working
day. Formulate the problem as an LPP.

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


To be Solved by Student

A automobile Manufacturing Company Products Moped and Scooters.


Each of which must be processed to two departments manufacturing
(M) and Assembly(A). The weekly availability of the processing time in
the M and A departments respectively 180 hours and 120 hours
producing moped requires 4 hours in department M and 6 hours in
department A while the scooter requires 10 hours in department M and
3 hours in department A. If the per unit profit for the Moped is 250 Rs
and for the scooter is 550 Rs . Using the simplex method determine
the optimal number of mopeds and scooters to be manufactured.

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


BITS Pilani
Hyderabad Campus

Lab Session 01
Roots of non-linear equations
Writing Non Linear
Equations in MATLAB
To following write polynomial equation in MATLAB

x3 + 2x2 - 4x + 3 [1 2 -4 3]

x3 + 2x2 + 3  x3 + 2x2 + 0x + 3 [1 2 0 3]

x5 + 2x2 + 3  x5+0x4+0x3+2x2+0x+3 [1 0 0 2 0 3]

In general if the degree of equation is n then there has to


be (n+1) terms in the row vector defining the equation in
MATLAB.

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


x3 + 2x2 - 4x + 3

>> p = [1 2 -4 3]
>> roots(p)

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


Non Linear Equations

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

0 = -16 t2 + 400 t + 2400  Non Linear Equation with One Variable


Define equation in MATLAB
>> P = [-16 400 2400];
>> roots(P)
ans =
30
31 -5 BITS Pilani, Deemed to be University under Section 3, UGC Act
Non Linear Equations

Determine root of following equation whose root lies in between


1 and 4

>> fzero(‘2*x-log10(x)-7’ , [4 1])


ans =
3.7893

Instead of range [4 1], just one initial guess can be given like
>> fzero(‘2*x-log10(x)-7’ , 4)
ans =
3.7893
32 BITS Pilani, Deemed to be University under Section 3, UGC Act

You might also like