You are on page 1of 29

A BRIEF MATLAB TUTORIAL

Prepared by Uğur Karakurt


The aim of this tutorial is to give some basic information about usage of MATLAB.
Firstly, you should install MATLAB into your computer. After installing MATLAB to your
computer, you are ready to create your own codes.

1 MATLAB GRAPHICAL USER INTERFACE

To start creating a script, run the program and choose “New” from upper toolbox and
select “Script”. Now, you can start to create your script.

Run the script

Working
directory Editor
panel

Command
window
Workspace

 Working directory is the address of the folder which you work on it.
 Editor panel is used to create and edit scripts, functions, etc.
 Workspace contains your variables, matrices, etc. You can access the value assigned
variables from here.
 Command window is used to call scripts, functions, variables etc. Moreover, you can
assign values to variables by using this window. You can also print or scan variable
values from here.
2 MATLAB BASICS

Some basic MATLAB operations are going to be shown in here.

2.1 Creating Variables and Arrays

2.1.1 Creating a variable


>> x=3.4

x =
3.4000

>> y=1E6

y =
1000000

>> z='hello'

z =
hello

2.1.2 Creating an array

 Row vector (1x3)


>> A=[1 2 3]

A =
1 2 3

 Column vector (3x1)


>> B=[1; 2; 3]

B =
1
2
3

 3x3 matrix
>> C=[1,2,3 ; 4,5,6 ; 7,8,9]

C =
1 2 3
4 5 6
7 8 9

 Empty matrix (5x1)


>> A = zeros(1,5)

A =

0 0 0 0 0

Page 2 of 29
2.2 Arithmetic Operators
>> var1=10
var1 =

10

>> var2=5
var2 =

 Addition:
>> var3 = var1 + var2
var3 =

15

 Subtraction:
>> var3 = var1 - var2
var3 =

 Multiplication:
>> var3 = var1 * var2
var3 =

50

 Division:
>> var3 = var1 / var2
var3 =

 Power:
>> var3 = var1^2
var3 =

100

>> var3 = var1^0.5


var3 =

3.1623

Page 3 of 29
2.3 Relational Operators

Comparison operators are shown in table below.

Example:
>> x=2;
>> y=3;
>> x>y

ans =

0
Note: 0 means that is False

>> x<y

ans =

1
Note: 1 means that is True

>> x==y
ans =

>> x~=y
ans =

Page 4 of 29
2.4 Logical Operators

Logical operators are shown in table below.

Example:
Find the logical AND of two matrices. The result contains logical 1 (true) only where
both matrices contain nonzero values.
>> A = [5 7 0; 0 2 9; 5 0 0]
A = 3×3

5 7 0
0 2 9
5 0 0

>> B = [6 6 0; 1 3 5; -1 0 0]
B = 3×3

6 6 0
1 3 5
-1 0 0

>> A & B
ans = 3x3 logical array

1 1 0
0 1 1
1 0 0

Page 5 of 29
Example:
Find the logical OR of two matrices. The result contains logical 1 (true) where either
matrix contains a nonzero value. The zeros in the result indicate spots where both arrays have
a value of zero.

>> A = [5 7 0; 0 2 9; 5 0 0]
A = 3×3

5 7 0
0 2 9
5 0 0

>> B = [6 6 0; 1 3 5; -1 0 0]
B = 3×3

6 6 0
1 3 5
-1 0 0

>> A | B
ans = 3x3 logical array

1 1 0
1 1 1
1 0 0

2.5 Basic Matrix Operations

Some basic matrix operators are shown in tables below.

Page 6 of 29
Example:
As a simple example, you can add two vectors with the same size.

>> A = [1 1 1]
A =

1 1 1

>> B = [1 2 3]
B =

1 2 3

>> A+B
ans =

2 3 4

Example:
You can compute the element-wise product of a scalar and a matrix.

>> A = [1 2 3; 1 2 3]
A =

1 2 3
1 2 3

>> 3.*A
ans =

3 6 9
3 6 9

Example:
You can calculate the product of two matrices.
>> A = [1 3;2 4]
A =

1 3
2 4

>> B = [3 0;1 5]
B =

3 0
1 5

Page 7 of 29
>> A*B
ans =

6 15
10 20

The previous matrix product is not equal to the following element-wise product.
>> A.*B
ans =

3 0
2 20

2.6 Array Indexing

Let’s create a 4x4 matrix.


>> A= [ 3 5 7 8; 9 6 4 3; 4 5 7 7; 10 20 45 12]

Output:
A =

3 5 7 8
9 6 4 3
4 5 7 7
10 20 45 12

To access a particular element in an array, you can specify row and column subscripts,
such as
>> A(2,3)

Output:
ans =

4
If you have a column or row vector, you can access a particular element in vector by
using only one subscript. For example,
>> A= [3 5 7 8 9 6 4 3]

A =

3 5 7 8 9 6 4 3

>> A(1,5)

ans =

9
Page 8 of 29
OR

>> A(5)

ans =

Example:
Let’s create a row vector.

>> A= [ 30 -55 75 -84 49 -46 24 23 ]


A =

30 -55 75 -84 49 -46 24 23

Finding minimum and maximum values in this array.


>> min(A)

ans =

-84

>> max(A)

ans =

75

Finding negative and positive values in this array.


>> positiveValues =A(A>0)
positiveValues =

30 75 49 24 23

>> negativeValues =A(A<0)


negativeValues =

-55 -84 -46

Page 9 of 29
Finding index of a value in array. For example, index of 75 is found as follows.
>> indexOfValue=find(A==75)
indexOfValue =

3
You can check it, output of A(3) is 75.

Use the relational less than operator, <, to determine which elements of A are less than
30. Store the result in B.

>> B = A < 30
B =

0 1 0 1 0 1 1 1
Although B contains information about which elements in A are less than 30, it doesn’t
tell you what their values are. Rather than comparing the two matrices element by element,
you can use B to index into A.
>> A(B)
ans =

-55 -84 -46 24 23


Some problems require information about the locations of the array elements that meet a
condition rather than their actual values. In this example, you can use the find function to
locate all of the elements in A less than 30.
>> I=find(A<30)
I=

2 4 6 7 8
You can check that a matrix contains a particular value by using ismember function.
This function returns an array containing logical 1 (true) where the particular value is found
in matrix. For example,
>> ismember(30,A)
ans =

1
The matrix A contains 30.
>> ismember(10,A)
ans =

0
The matrix A does not contain 10.
Page 10 of 29
Determine which elements of C are also in A.
>> A= [ 30 -55 75 -84 49 -46 24 23]
>> C=[10 20 30 24]
>> L=ismember(C,A)

ans =

0 0 1 1

>> C(L)

ans =

30 24
C(3)=30 and C(4)=24 are found in A.

2.7 Displaying Value of a Variable

Example 1:
>> x=5;
>> disp(x);

5
>>

Example 2:
>> A=[ 1 2 3];
>> disp(A);

1 2 3
>>

Example 3:
>> z='hello';
>> disp(z);

hello
>>

Page 11 of 29
2.8 Printing Value of a Variable

Example :
a=3;
b=2.525;
c=a+b;
fprintf('%f \n',c)

5.525000
>>

Note : \n is a control character that starts new line

For two digits after decimal point:


fprintf('%.2f \n',c)
5.53
>>

You can use %d for integers:


fprintf('%d \n',a)
3
>>

Printing a string and 3 variables together


fprintf('sum of a=%d and b=%f is c=%f \n',a,b,c)
sum of a=3 and b=2.525000 is c=5.525000
>>

2.9 Requesting User Input

Use “input” function to request input from command window.


Example:
x = input('Enter the value of x = ');
y = x^2;
fprintf('y = %f \n',y)
When you run the code, you must enter a value for x from keyboard.
Enter the value of x = 5
y = 25.000000

Page 12 of 29
2.10 Importing a Data File

There are different ways to load data files(.txt, .dat, etc. ).


2.10.1 Using “importdata” function

You can use “importdata” function.


Example:
The content of “MyDataFile.txt” file is shown below.

Temp. Press. v_f v_fg v_g u_f u_fg u_g


(◦C) (kPa) (m^3/kg) (m^3/kg) (m^3/kg) (kJ/kg) (kJ/kg) (kJ/kg)
------------------------------------------------------------------------------------
0.01 0.6113 0.001000 206.131 206.132 0 2375.33 2375.33
5 0.8721 0.001000 147.117 147.118 20.97 2361.27 2382.24
10 1.2276 0.001000 106.376 106.377 41.99 2347.16 2389.15
15 1.705 0.001001 77.924 77.925 62.98 2333.06 2396.04
20 2.339 0.001002 57.7887 57.7897 83.94 2318.98 2402.91
25 3.169 0.001003 43.3583 43.3593 104.86 2304.90 2409.76
30 4.246 0.001004 32.8922 32.8932 125.77 2290.81 2416.58
35 5.628 0.001006 25.2148 25.2158 146.65 2276.71 2423.36
40 7.384 0.001008 19.5219 19.5229 167.53 2262.57 2430.11
45 9.593 0.001010 15.2571 15.2581 188.41 2248.40 2436.81
50 12.350 0.001012 12.0308 12.0318 209.30 2234.17 2443.47

This data file is imported as follows.

MyTable = importdata('MyDataFile.txt');

Now data file is imported and assigned a variable named MyTable.


If you interest only numerical values in table. You must use “.data” command as
follows.
OnlyValues= MyTable.data;

Now, numerical values in table assigned a matrix named OnlyValues. You can display
this variable or you can open from Workspace panel.

disp(OnlyValues)
Then, the output of command is

Page 13 of 29
1.0e+03 *

0.0000 0.0006 0.0000 0.2061 0.2061 0 2.3753 2.3753


0.0050 0.0009 0.0000 0.1471 0.1471 0.0210 2.3613 2.3822
0.0100 0.0012 0.0000 0.1064 0.1064 0.0420 2.3472 2.3891
0.0150 0.0017 0.0000 0.0779 0.0779 0.0630 2.3331 2.3960
0.0200 0.0023 0.0000 0.0578 0.0578 0.0839 2.3190 2.4029
0.0250 0.0032 0.0000 0.0434 0.0434 0.1049 2.3049 2.4098
0.0300 0.0042 0.0000 0.0329 0.0329 0.1258 2.2908 2.4166
0.0350 0.0056 0.0000 0.0252 0.0252 0.1467 2.2767 2.4234
0.0400 0.0074 0.0000 0.0195 0.0195 0.1675 2.2626 2.4301
0.0450 0.0096 0.0000 0.0153 0.0153 0.1884 2.2484 2.4368
0.0500 0.0124 0.0000 0.0120 0.0120 0.2093 2.2342 2.4435

If you want to assign header of the table to a variable, use “.textdata” command as
follows.
Header = MyTable.textdata;

Now, text in the table was assigned a variable named Header.You can display this
variable or you can open from Workspace panel.

disp(Header)

Then, the output of this command is


'Temp. Press. v_f v_fg v_g u_f u_fg u_g '
'(â—¦C) (kPa) (m^3/kg) (m^3/kg) (m^3/kg) (kJ/kg) (kJ/kg) (kJ/kg)'
'------------------------------------------------------------------------------------'

2.10.2 Using “load” function

You can use “load” function, but be careful while using. If you use “load” function,
your data file must contain only numerical values, therefore you must delete header lines from
your data file.

Example:
The content of “MyDataFile.txt” file is shown below. As you can see below, there is no
header line. When you use load function, if you have header lines like in previous example,
you will receive an error message.

Page 14 of 29
0.01 0.6113 0.001000 206.131 206.132 0 2375.33 2375.33
5 0.8721 0.001000 147.117 147.118 20.97 2361.27 2382.24
10 1.2276 0.001000 106.376 106.377 41.99 2347.16 2389.15
15 1.705 0.001001 77.924 77.925 62.98 2333.06 2396.04
20 2.339 0.001002 57.7887 57.7897 83.94 2318.98 2402.91
25 3.169 0.001003 43.3583 43.3593 104.86 2304.90 2409.76
30 4.246 0.001004 32.8922 32.8932 125.77 2290.81 2416.58
35 5.628 0.001006 25.2148 25.2158 146.65 2276.71 2423.36
40 7.384 0.001008 19.5219 19.5229 167.53 2262.57 2430.11
45 9.593 0.001010 15.2571 15.2581 188.41 2248.40 2436.81
50 12.350 0.001012 12.0308 12.0318 209.30 2234.17 2443.47

This data file is imported as follows.

MyTable = load ('MyDataFile.txt');

Now data file is imported and assigned a variable named MyTable. You can display
this variable or you can open from Workspace panel.

disp(MyTable)

Then, the output of this command is


1.0e+03 *

0.0000 0.0006 0.0000 0.2061 0.2061 0 2.3753 2.3753


0.0050 0.0009 0.0000 0.1471 0.1471 0.0210 2.3613 2.3822
0.0100 0.0012 0.0000 0.1064 0.1064 0.0420 2.3472 2.3891
0.0150 0.0017 0.0000 0.0779 0.0779 0.0630 2.3331 2.3960
0.0200 0.0023 0.0000 0.0578 0.0578 0.0839 2.3190 2.4029
0.0250 0.0032 0.0000 0.0434 0.0434 0.1049 2.3049 2.4098
0.0300 0.0042 0.0000 0.0329 0.0329 0.1258 2.2908 2.4166
0.0350 0.0056 0.0000 0.0252 0.0252 0.1467 2.2767 2.4234
0.0400 0.0074 0.0000 0.0195 0.0195 0.1675 2.2626 2.4301
0.0450 0.0096 0.0000 0.0153 0.0153 0.1884 2.2484 2.4368
0.0500 0.0124 0.0000 0.0120 0.0120 0.2093 2.2342 2.4435

Page 15 of 29
After importing data file, you can create vectors for each column as follows. For
example,
First column of MyTable matrix is temperature. Then,
Temperature = MyTable(:,1)
------------------------------------------------------------------------
Note:
MyTable(:,1) means all elements of column 1 of “MyTable” matrix.
MyTable(2,:) means all elements of row 2 of ”MyTable” matrix.
------------------------------------------------------------------------

Second column of MyTable matrix is pressure. Then,


Pressure = MyTable(:,2)

Third column of MyTable matrix is 𝑣𝑓 Then,


vf=MyTable(:,3)

Now, you column vectors which contain data for temperature, pressure and 𝑣𝑓 respectively.

2.11 Writing Data to a Text File

You can write your data to a text file.


Example:
myfile = fopen('DataFile.txt','w');
fprintf(myfile,'This is header of my data file \n')
firstrow= [1.5 2.0 3.5];
secondrow= [4.1 5.4 6.7];
fprintf(myfile,'%f %f %f \n',firstrow);
fprintf(myfile,'%f %f %f \n',secondrow);
fclose(myfile)

Let's analyze the code above.

myfile = fopen('DataFile.txt','w');

This command open a text file named DataFile.txt in writing mode (‘w’).

fprintf(myfile,'This is header of my data file')

This command was used to write “This is header of my data” to file


DataFile.txt.

Note: \n provides to pass next line.

firstrow= [1.5 2.0 3.5];


secondrow= [4.1 5.4 6.7];
These are two row vectors.

fprintf(myfile,'%f %f %f \n',firstrow);

This command was used to write elements of vector firstrow to the second line of
DataFile.txt file.

Page 16 of 29
fprintf(myfile,'%f %f %f \n',secondrow);

This command was used to write elements of vector secondrow to the third line of
DataFile.txt file.

fclose(myfile)
This command was used to close the data file. Do NOT forget close the data file.

After running the code, it creates a file named DataFile.txt in working directory. The
content of file is
This is header of my data file
1.500000 2.000000 3.500000
4.100000 5.400000 6.700000

2.12 If Statement

Execute statements if condition is true.


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.
The syntax of an if statement in MATLAB is as following:

if expression
statements
elseif expression
statements
else
statements
end

Example:
a=1;
b=5;
if a<b
fprintf('a is less than b \n')
end

When the code above is executed, the result will be:

a is less than b
a=10;
b=5;
if a<b
fprintf('a is less than b \n')
end
When the code above is executed, it prints nothing because condition is false.

Page 17 of 29
Example:
a=7;
b=5;
if a<b
fprintf('a is less than b \n')
else
fprintf('a is greater than b \n')
end

When the code above is executed, the result will be:

a is greater than b

Example:
a=5;
b=5;
if a<b
fprintf('a is less than b \n')
elseif a>b
fprintf('a is greater than b \n')
else
fprintf('a is equal to b \n')
end

When the code above is executed, the result will be:

a is equal to b

Example:

x = 10;
minVal = 2;
maxVal = 6;

if (x >= minVal) && (x <= maxVal)


disp('Value within specified range.')
elseif (x > maxVal)
disp('Value exceeds maximum value.')
else
disp('Value is below minimum value.')
end

--------------------------------------------------------------
Note: Remember && is logical AND.
--------------------------------------------------------------
When the code above is executed, the result will be:

Value exceeds maximum value.

Page 18 of 29
2.13 Loops

A loop statement allows us to execute a statement or group of statements multiple times.


The drawing shows the general form of a loop statement for most programming languages.

2.13.1 While Loop

The while loop repeatedly executes statements while a specified condition is true.
The syntax of a while loop in MATLAB is as following:

while <expression>
<statements>
end

The while loop repeatedly executes a program statement(s) as long as the expression
remains true.

Example:

a=10
% while loop execution
while( a < 20 )
fprintf('value of a: %d\n', a);
a = a + 1;
end
--------------------------------------------------------------
Note: % sign is used to write comments in code. This section
will not be executed.
--------------------------------------------------------------

Page 19 of 29
When the code above is executed, the result will be:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

Example:
% ln(x)=1
% finding x value satisfying equation above iteratively
% RHS : right-hand side of equation
% LHS : left-hand side of equation
RHS = 1;
% initial value of absolute error
absolute_error=1;
% initial value of x
x=0;
% increment size
dx=0.001;
% The while loop repeatedly executes
% until absolute_error becomes less than 1e-3
while absolute_error>1e-3
% adding dx for each iteration
x=x+dx;
% calculate LHS of equation
LHS = log(x) ;
% calculate absolute error
absolute_error=abs(RHS-LHS)
end
disp(x)

When the code above is executed, the result will be:

x=2.716000

Page 20 of 29
2.13.2 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.
The syntax of a for loop in MATLAB is as following:

for index = values


<program statements>
...
end
values can be one of the following forms:
initialvalue : endvalue --- increments the index variable from initialvalue to endvalue by
1, and repeats execution of program statements until index is greater than endvalue.
initvalue : step : endvalue --- increments index by the value step on each iteration, or
decrements when step is negative.
valArray --- creates a column vector index from subsequent columns of array valArray
on each iteration. For example, on the first iteration, index = valArray(:,1). The loop executes
for a maximum of n times, where n is the number of columns of valArray, given by
numel(valArray, 1, :). The input valArray can be of any MATLAB data type, including a
string, cell array, or struct.

Example:

for x = 1:1:10
fprintf('value of x: %d\n', x);
end

When the code above is executed, the result will be:


value of x: 1
value of x: 2
value of x: 3
value of x: 4
value of x: 5
value of x: 6
value of x: 7
value of x: 8
value of x: 9
value of x: 10

Page 21 of 29
Example:

for a = 1.0: -0.1: 0.0


disp(a)
end

When the code above is executed, the result is:


1

0.9000

0.8000

0.7000

0.6000

0.5000

0.4000

0.3000

0.2000

0.1000

Example:
for a = [24,18,17,23,28]
disp(a)
end

When the code above is executed, the result will be:


24

18

17

23

28
Example:
% x is a vector
x=[1 2 3 4 5 6 7 8 9 10];
% length(x) is the number of elements in vector x
% Calculating y = x^2 + x +1 for different x values
for i=1:length(x)
% Calculating and storing y values
y(i)=x(i)^2 + x(i) + 1;
% printing
fprintf(' x=%d --> y=%f \n',x(i),y(i))
end

Page 22 of 29
When the code above is executed, the result will be:

for x=1 , y=3.000000


for x=2 , y=7.000000
for x=3 , y=13.000000
for x=4 , y=21.000000
for x=5 , y=31.000000
for x=6 , y=43.000000
for x=7 , y=57.000000
for x=8 , y=73.000000
for x=9 , y=91.000000
for x=10 , y=111.000000

2.13.3 Loop Control Statements

Loop control statements change execution from its normal sequence. When execution
leaves a scope, all automatic objects that were created in that scope are destroyed. The scope
defines where the variables can be valid in MATLAB, typically a scope within a loop body is
from the beginning of conditional code to the end of conditional code. It tells MATLAB what
to do when the conditional code fails in the loop. MATLAB supports both break statement
and continue statement.
 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.

Page 23 of 29
Example:

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

When the code above is executed, the result is:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15

 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.

Page 24 of 29
Example:
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

When the code above is executed, the result is:


value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

2.14 Creating Functions

You can create your own function in MATLAB as follows.


Syntax:

function [y1,...,yN] = myfun(x1,...,xM)


function [y1,...,yN] = myfun(x1,...,xM) declares a function named
myfun that accepts inputs x1,...,xM and returns outputs y1,...,yN. This declaration
statement must be the first executable line of the function. Valid function names begin with an
alphabetic character, and can contain letters, numbers, or underscores.

Example:
Creating a function that calculates circular area.
% Circular area function
% R is input parameter
% area is output parameter
% Name of function : CircleArea
% R is radius of circle, unit: cm
function area = CircleArea(R)
area = pi*R^2;
fprintf('area of circle is : %f cm^2 \n',area)
end

Page 25 of 29
First, you must save this script by using the function’s name like “CircleArea.m”.
Afterwards, go to command window and call your function as follows
>> CircleArea(1);

When the code above is executed, the result is:

area of circle is : 3.141593 cm^2

--------------------------------------------------------------
Note: You can also call your function within other scripts.
--------------------------------------------------------------

Example 2:
Using multiple inputs and output parameters.
There is a function below which is used to calculate average and standard deviation of a
data set.

% Average and Standard Deviation of a Data Set


% x : input parameter
% ave,sigma : output parameters
% x : data
% N : size of data set
% ave : average of x
% sigma : standard deviation of x
function [ave,sigma] = stat(x)
N = length(x);
ave = sum(x)/N;
sigma = sqrt(sum((x-ave).^2/(N-1)));
end

First, you must save this script by using the function’s name like “stat.m”.
Afterwards, go to “editor panel” and create a new script which use stat.m function
and call your function within this script as follows.

data = [5, 10, 15, 10, 10];


[average,standard_deviation] = stat(data)

When the code above is executed, the result is:


average =

10

standard_deviation =

3.5355

Page 26 of 29
3 EXAMPLES FOR ASSIGNMENT-2

You are given a thermodynamic property table for water which includes undefined
values. By creating a computer code, fill in the missing parts.

𝑇(℃) 𝑃 (𝑘𝑃𝑎) 𝑥 𝑣 (𝑚3 /𝑘𝑔) 𝑣𝑓 (𝑚3 /𝑘𝑔) 𝑣𝑔 (𝑚3 /𝑘𝑔) 𝑢 (𝑘𝐽/𝑘𝑔)

240 1485.3
5713.4 1273.0

Now, we create a script that calculates missing parts in table above.


To start creating a script, run the MATLAB program and choose “New” from upper
toolbox and select “Script”. Now, you can start to create your script.
Afterwards, we need saturated water and superheated water tables. The file
saturated_table.txt contains saturated water properties.
Importing saturated water data to MATLAB.
table = importdata('saturated_table.txt');
saturatedWater = table.data;

Now, we can create vectors for properties in table as follows.


Temperature=saturatedWater(:,1);
Pressure=saturatedWater(:,2);
specificVolume_f=saturatedWater(:,3);
specificVolume_fg=saturatedWater(:,4);
specificVolume_g=saturatedWater(:,5);
internalEnergy_f=saturatedWater(:,6);
internalEnergy_fg=saturatedWater(:,7);
internalEnergy_g=saturatedWater(:,8);

For first case in table,


𝑇 = 240 ℃
𝑢 = 1485.3 𝑘𝐽/𝑘𝑔
Then, create variables and assign these values.

T = 240; % Celsius-degree
u = 1485.3; % kJ/kg

Checking that T = 240 is a member of Temperature vector.


% if T is member of Temperature vector value returns 1, else returns 0
value=ismember(T,Temperature);

Page 27 of 29
Checking that is necessary making interpolation.

% Checking that is necessary making interpolation


if value == 1
fprintf('No need to make interpolation \n')
% write your statements in here to find values needed.
else
fprintf('Make interpolation \n')
% write your statements in here to find values needed.
end

If there is no need to make interpolation you can find index of T = 240.

% i: index of T=240 in Temperature vector


i=find(Temperature==T);

Now, you can find all properties as follows.

P=Pressure(i);
vf = specificVolume_f(i);
vg = specificVolume_g(i);
uf = internalEnergy_f(i);
ug = internalEnergy_g(i);

After this, you must check the phase of water. It can be superheated. If it is super-heated
you must import superheated water table to MATLAB. In this case it is saturated.
Now you can calculate x and v. Then, you can print all properties as follows.
x = (u-uf)/(ug-uf);
v = vf + x * (vg-vf);

fprintf('%.2f %.2f %.6f %.6f %.6f %.6f %.6f


\n',T,P,x,v,vf,vg,u)

For second case in table,


𝑃 = 5713.4 𝑘𝑃𝑎
𝑢 = 1273.0 𝑘𝐽/𝑘𝑔
Then, create variables and assign values above.

P = 5713.4; % kPa
u = 1273.0 % kJ/kg

First check 𝑃 is a member of Pressure vector. In this case, it is not a member of


Pressure vector. Therefore, you must make interpolation as follows.

A=find(Pressure<P);
j=A(end);
j is index of previous pressure value than P = 5713.4 kPa
B=find(Pressure>P);
k=B(1);

Page 28 of 29
k is index of next pressure value than P = 5713.4 kPa

Then you can find previous and next pressure values as follows.
P1 = Pressure(j);
P2 = Pressure(k);

Moreover, you can find corresponding properties to these pressure values as follows.

T1=Temperature(j);
T2=Temperature(k);
vf1=specificVolume_f(j);
vf2=specificVolume_f(k);
...

Now, you can make interpolation to find corresponding property values to P = 5713.4
kPa. After finding corresponding property values to P = 5713.4 kPa, you must check the
phase of water. It can be superheated. If it is superheated you must import superheated water
table to MATLAB.
You must do it on your own after this…

You can visit link below to learn more about MATLAB.

https://www.mathworks.com/help/matlab/

Page 29 of 29

You might also like