Basics

Site: MSubbu Academy
Course: MATLAB for Chemical Engineers
Book: Basics
Printed by: Guest user
Date: Sunday, 17 November 2024, 2:22 PM

1. Arithmetic Operations

The list of basic arithmetic operations in MATLAB includes six operations:

+   :    addition

-   :    subtraction

*   :   multiplication

/   :   right division

\   :   left division

^   :   power

2. Simple Math

One can use MATLAB like a calculator without specifying variables. MATLAB has all the standard mathematical operations.

>> 2+2.5+106
ans = 110.5000 >> 4*25 + 2^3 ans = 108

3. MATLAB Variables

>> D = 2
D =
     2
>> v = 3
v =    
     3

To recall the variable use arrow keys up ↑ and down ↓ for scrolling through previous commands.

>> D
D =
     2
>> 
>> rho = 1000;
>> mu = 0.001;
>> Re = D*v*rho/mu
Re =
     6000000
>>

MATLAB does not require any type declarations or dimension statements. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. The variables "A" and "a" are not the same variable. Variable name can contain up to 63 characters. Variable names start with a letter, followed by letters, numbers or underscores. (e.g.: NRe_2_the_power2by3)

4. M files

In addition to executing commands entered through the keyboard, you can also use commands stored as files. These files all end in the extension .m and are called M-files because of the file name extension. Long programs are usually created in a text editor and stored as an M-file that can be executed in a MATLAB session. Short programs are usually created and run directly on-line.

M files are of two types: script m file and function m file. Function m file starts with the keyword function.

5. Script M file

A script file is an external file that contains a sequence of MATLAB statements. By typing the filename, subsequent MATLAB input is obtained from the file.

% frictionfactor.m
% Done on 16-Dec-2001
% Re-Done on 28-Jan-2011
D = input('Dia in meter = ');
v = input('Velocity in m/s = ');
rho = 1000; % Density of water in kg/m3
mu = 0.001; % Viscosity of water in kg/m.s

Re = D*v*rho/mu
f = 0.079*Re^(-0.25);
disp(f);
This script file saved as frictionfactor.m shall be called from the command prompt as follows. The code therein will be executed.
>> frictionfactor 
Dia in meter = .1
Velocity in m/s = 2
Re =
      200000
    0.0037
The input command is to input the data from the user during the run time. The disp is to display the value of a variable in the command window.

6. List of Variables in the Workspace

The command who is used to get to know the variables in the current workspace.
>> who
Your variables are:
D         Re       mu        rho       v         ans
>>

7. Clearing Variables

>> clear D
>> who
Your variables are:
NRe       ans       mu        rho       v         
>>

>> clear   
>> who
>>
clear Clears all items from memory. Items could be variables or functions.
clear variable1, variable2... will clear the variables specified.

8. Equally Spaced Arrays

Typing every entry is time consuming for long arrays. If the points are equally spaced this isn't necessary. An alternative method is to use the colon notation [first_value : interval : last_value]
>> a = 1:2:10
a =
     1     3     5     7     9
>>
If the interval is left out MATLAB uses a default interval of 1.

 Another way of specifying an equally-spaced array is to use the linspace command. This allows you to specify the number of data points, but not the interval to be used. The notation is:
linspace(first_value, last_value, number_of_values)
If the data for number_of_values is not specified, it is taken as 100.

9. Semicolon

The semicolon (;) can be used to end rows of vectors or matrices. It is used very often, however, to suppress the displaying of the variable in the command window. The semicolon can also be used to separate statements on the same line such as:
>> Num_of_Students=50; Num_of_Teachers=12;

10. Colon

The colon(:) is one of the most useful operators in MATLAB. It can create vectors, subscripts, and specify for loop iterations. The colon operator uses the following rules to create regularly spaced vectors: j:k is the same as [j,j+1,...,k] Below are the definitions that govern the use of the colon to pick out selected rows, columns, and elements of vectors, matrices, and higher-dimensional arrays:

A(:,j)
is the j-th column of A
A(i,:) is the i-th row of A

11. Array Addressing

>> x = 1:10;
>> y = sin(x)
y =
  Columns 1 through 7 
    0.8415  0.9093  0.1411 -0.7568 -0.9589  -0.2794    0.6570
  Columns 8 through 10 
    0.9894    0.4121   -0.5440
>>

>> y(3)
ans =
    0.1411

>> y(1:5)
ans =
    0.8415    0.9093    0.1411   -0.7568   -0.9589

>>

12. Relational Operators

A < B Less than
A > B Greater than
A <= B Less than or equal
A >= B Greater than or equal
A == B Equal
A ~= B Not equal

13. Logical Operators

&      a < b & b >= c
| or
~ not

13.1. Example

% Ex_LogicalOperators.m
% Re <= 2100 laminar
% Re >= 4000 Turbulent
% 2100 < Re < 4000 transition

Re = input('Reynolds Number = ');

if Re <= 2100
    disp('Laminar');
elseif Re< 4000
    disp('Transition');
else
    disp('Turbulent');
end
    
if Re > 2100 && Re < 4000 
    disp('Modify the flow conditions please,')
    disp('as the flow regime is uncertain')
end

14. Matrix

We can input a matrix elements by leaving a space between elements, or separating the elements with a comma as below. We can end a row by typing the elements in the next row by pressing enter key. Row can also be ended by using semicolon.
>> A = [1 2 -2
2 -1 0]
 
A =

     1     2    -2
     2    -1     0

>> A = [1,2,-2
2,-1,0]
 
A =

     1     2    -2
     2    -1     0

>> A = [1 2 -2; 2 -1 0]
 
A =

     1     2    -2
     2    -1     0