You are on page 1of 41

Computational Methods in Process

Engineering(CHE3001)
Lab Component
Dr. Bandaru Kiran
Assistant Professor Sr.
SCHEME
VIT Vellore
Mob: 7981343089
Getting Started With Matlab

• MATLAB is a high-performance language for technical computing. It integrates


computation, visualization, and programming in an easy-to-use environment
where problems and solutions are expressed in familiar mathematical notation.
Typical uses include:
•Math and computation
•Algorithm development
•Modeling, simulation, and prototyping
•Data analysis, exploration, and visualization
•Scientific and engineering graphics
•Application development, including graphical user interface building
MATLAB Desktop
Desktop Tools
This section provides an introduction to MATLAB’s desktop tools. You can also use
MATLAB functions to perform most of the features found in the desktop tools.
•“Command Window”
•“Command History”
•“Launch Pad”
•“Help Browser”
•“Current Directory Browser”
•“Workspace Browser”
•“Array Editor”
•“Editor/Debugger”
Command Window

• Use the Command Window to enter variables and run


functions and M-files.
• For more information on controlling input and output
Editor/debugger
Entering Matrices
Enter the elements row wise:
• A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1];
• Display
A=
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Matrice Operations

• Sum
• transpose
• diag
• Transpose
• Inverse
• Product
Operators

• Colon operator “:”


• 1:10
• Output: 1 2 3 4 5 6 7 8 9 10
Operators
• + Addition
• - Subtraction
• * Multiplication
• / Division
• \ Left division (described in “Matrices and Linear Algebra” in
Using MATLAB)
• ^ Power
• ' Complex conjugate transpose
• ( ) Specify evaluation order
Special Functions
• pi 3.14159265…
• i Imaginary unit, Ö-1
• j Same as i
• eps Floating-point relative precision, 2^52
• realmin Smallest floating-point number, 2^1022
• realmax Largest floating-point number, (2-e)2^1023
• Inf Infinity
• NaN Not-a-number
Working With Matrices

• zeros All zeros


• Ones All ones
• rand Uniformly distributed
• randn Normally distributed random elements
Deleting Rows and Columns
You can delete rows and columns from a matrix using just a pair of squarebrackets. Start
with
• X = A;
• Then, to delete the second column of X, use
• X(:,2) = []
• This changes X to A=16.0 3. 0 2.0 13.0
5.0 10.0 11.0 8.0
• X=
9.0 6.0 7.0 12.0
16 2 13
4.0 15.0 14.0 1.0
5 11 8
9 7 12
4 14 1
Matrix calculations

Determinant : det(A)
Inverse: inv(A)
Eigen values: eig(A)
Arrays

• Arrays a kind of data structure that can store a fixed-


size sequential collection of elements of the same
type
• Arithmetic operations on arrays are done element-by-
element
• Addition and subtraction are the same for arrays and
matrices, but that multiplicative operations are
different
Arrays
Creating Plots

• The plot function has different forms, depending on


the input arguments
• If y is a vector, plot(y) produces a piecewise linear
graph of the elements of y versus the index of the
elements of y
• If you specify two vectors as arguments, plot(x,y)
produces a graph of y versus x.
Example

x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the Sine Function','FontSize',12)
Plot
Multiple Data set in Single Graph

x = 0:pi/100:2*pi;
y1=sin(x);
y2 = sin(x-.25);
y3 = sin(x-.5);
plot(x,y1,x,y2,x,y3)
legend('sin(x)','sin(x-.25)','sin(x-.5)') % Legend
Multiple Plots in one Figure

• The subplot command enables you to display


multiple plots in the same window or print
• Command: subplot(m,n,p) m, n stands for grid & p
stands for position
subplot(2,2,1)
x = linspace(0,10);
y1 = sin(x);
plot(x,y1) subplot(2,2,3)
y3 = sin(4*x);
title('Subplot 1: sin(x)') plot(x,y3)
subplot(2,2,2) title('Subplot 3: sin(4x)')
subplot(2,2,4)
y2 = sin(2*x);
y4 = sin(8*x);
plot(x,y2) plot(x,y4)
title('Subplot 2: sin(2x)') title('Subplot 4: sin(8x)')
Loops

MATLAB has several flow control constructs:


•if statements
•switch statements
•for loops
•while loops
•continue statements
•break statements
If
if expression, statements, end evaluates an expression, and executes a
group of statements when the expression is true
An expression is true when its result is nonempty and contains only
nonzero elements (logical or real numeric). Otherwise, the expression is
false

The elseif and else blocks are optional. The statements execute only if
previous expressions in the if...end block are false. An if block can include
multiple elseif blocks.
Syntax: if

If()
expression statements
elseif
expression statements
else
statements
end
Exaxmple Code

• Create a matrix of 1s
• nrows = 4; ncols = 6;
• A = ones(nrows,ncols);
• Loop through the matrix and assign each element a
new value. Assign 2 on the main diagonal, -1 on the
adjacent diagonals, and 0 everywhere else.
for c = 1:ncols
for r = 1:nrows
if r == c
A(r,c) = 2;
elseif abs(r-c) == 1
A(r,c) = -1;
else
A(r,c) = 0;
end
end
end
While Loop

• The while loop repeatedly executes statements while


a specified condition is true
• Syntax:
while (expression)
statements
end
Output
a = 10;
% while loop execution
while( a < 20 )
fprintf('value of a: %d\n', a);
a = a + 1;
end
For loop

• A for loop is a repetition control structure that allows


you to efficiently write a loop that needs to execute a
specific number of times
for index = values
...
end
Example Code

for a = 10:20 Output

fprintf('value of a: %d\n', a);


end
Output

for a = 1.0: -0.1: 0.0


disp(a)
end
Break statement

• The break statement terminates execution of for or while


loops
• Statements in the loop that appear after the break
statement are not executed
• In nested loops, break exits only from the loop in which
it occurs
• Control passes to the statement following the end of that
loop
a = 10;
% while loop execution
while (a < 20 )
fprintf('value of a: %d\n', a);
a = a+1;
if( a > 15) % terminate the loop using break statement
break;
end
end
Continue Statement

• The continue statement is used for passing control to the


next iteration of a for or while loop.
• The continue statement in MATLAB works somewhat
like the break statement. Instead of forcing termination,
however, 'continue' forces the next iteration of the loop
to take place, skipping any code in between
Example code
Output
a = 10;
%while loop execution
while a < 20
if a == 15 % skip the iteration
a = a + 1;
continue;
end
fprintf('value of a: %d\n', a);
a = a + 1;
end
Switch Statements

• The switch statement syntax is a means of conditionally


executing code
• In particular, switch executes one set of statements selected
from an arbitrary number of alternatives, called case groups.
Each case group consists of:
• A case statement, consisting of a case label and one or more
conditional expressions
• One or more statements, where a statement can be
another switch statement
Switch Statements
switch n
case -1
disp('negative one')
case 0
disp('zero')
case 1
disp('positive one')
otherwise
disp('other value')
end

You might also like