Ce Matlab Report
Ce Matlab Report
SECTION: 12B
CMS ID: 336893
LAB NO 06
TITLE:
Introduction to MATLAB programing
OBJECTIVES:
• To learn the fundamentals of MATLAB programming language.
• To learn how to make matrices and arrays in MATLAB.
• To learn how to perform basic operations on matrices.
SOFTWARE used:
1. MATLAB.
THEORETICAL BACKGROUND:
Matlab:
Introduction:
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. The name
MATLAB stands for matrix laboratory. MATLAB was originally written to provide easy
access to matrix.
Desktop Tools:
1. Command Window: Use the Command Window to enter variables and run functions and
M-files.
2. Command History: Statements you enter in the Command Window are logged in the
command history. In the command history, you can view previously run statements, and
copy and execute selected statements.
3. Current Directory Browser: MATLAB file operations use the current directory
reference point. Any file you want to run must be in the current directory or on the search
path.
4. Workspace: The MATLAB workspace consists of the set of variables (named arrays)
built up during a MATLAB session and stored in memory.
NAME: Muhammad Saad Saleem
SECTION: 12B
CMS ID: 336893
Figure 1:
Basic Commands:
• clear Command: Removes all variables from workspace.
• clc Command: Clears the Command window and homes the cursor.
• help Command: help <Topic> displays help about that Topic if it exist.
• lookfor Command: Provides help by searching through all the first lines of MATLAB
help topics and returning those that contains a key word you specify.
• edit Command: Enable you to edit (open) any M-file in Editor Window. This command
doesn’t open built-in function like, sqrt. See also type Command.
Entering Matrix:
The best way for you to get started with MATLAB is to learn how to handle matrices.
You only have to follow a few basic conventions:
• Separate the elements of a row with blanks or commas.
• Use a semicolon (;) to indicate the end of each row.
• Surround the entire list of elements with square brackets, [ ].
NAME: Muhammad Saad Saleem
SECTION: 12B
CMS ID: 336893
For Example
>> A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
MATLAB displays the matrix you just entered.
A=
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Once you have entered the matrix, it is automatically remembered in the MATLAB
workspace. You can refer to it simply as A. Also you can enter and change the values of
matrix elements by using workspace window.
Table 1: Array Arithmetic operations
Practice exercises:
Exercise No 01:
Define two variables x and y and ask user to input their random values and perform four
basic arithmetic operation i.e. addition, subtraction, multiplication and division and store their
values in the following variables i.e. add_1, sub_1, mult_1, and div_1. And print the values
of variables on screen using fprintf function.
CODE:
x=input("Enter value for x"); %defining variable x
y=input("Enter value for y"); %defining variable y
add_1=x+y; %performing arithmetic operation addition between x and y
sub_1=x-y; %performing arithmetic operation subtraction between x and y
mult_1=x*y; %performing arithmetic operation multiplication between x and y
div_1=x/y; %performing arithmetic operation division between x and y
fprintf("Summation of two inputs = %.2f\n",add_1); %printing addition
fprintf("Subtraction of two inputs (x-y)=%.2f\n",sub_1); %printing subtraction
fprintf("Multiplication of two inputs=%.2f\n",mult_1); %printing multiplication
fprintf("Division of two inputs (x/y)=%.2f\n",div_1); %printing division
NAME: Muhammad Saad Saleem
SECTION: 12B
CMS ID: 336893
OUTPUT:
Exercise No 02:
Write the program that ask user to input the value of velocity and calculate the Reynolds number,
volumetric flow rate and mass flow rate. Print the results on ouput screen using fprintf function.
Take the following parameters for calculation other than velocity: Density = 998.2, Dynamic-
viscosity= 8.9E-04, Dia of circular pipe = 0.2 m. Note all units are in SI unit.
CODE:
v=input("Enter the value of velocity: "); %defining variable v, velocity
p=998.2; %Density of water
u=8.9E-04; %Dynamic Viscosity
D=0.2; %Diameter of the pipe
fprintf("Reynolds number :%.3f\n",p*v*D/u); %Reynolds number=p*v*D/u
fprintf("Volumetric flow rate :%.3f\n",D*D*pi*0.25*v); %Volumetric flow
rate=(pi*D^2*v)/4
fprintf("Mass flow rate :%.3f\n",D*D*pi*0.25*v*p); %Mass flow rate=Vol.flow
rate*density
NAME: Muhammad Saad Saleem
SECTION: 12B
CMS ID: 336893
OUTPUT:
EXERCISE NO 03:
Take first 3x3 matrix A of random non-zero entries and make second 3x3 identity matrix B.
Perform the following operations: (i) Matrix multiplication of two matrices A and B, (ii)
Element wise multiplication of two matrices A and B, (iii) Transpose of Matrix A, (iv)
Multiplicative inverse of Matrix A. Display the results of all operations on command window
using “disp” function.
CODE:
A=[1 4 3; 5 4 8; 8 7 1]; %defining matrix A
B=[1 0 0; 0 1 0; 0 0 1]; %definig identity matrix B
disp("Multiplication of Matrix A and Matrix B"); %multiplication of matrix A and B
disp(A*B); %displaying result of multiplication of A and B
disp("Element wise Multiplication of A and B "); %Element wise multiplication of A
and B
disp(A.*B); %displaying result of element wise multiplication
disp("Transpose of A"); %Transpose of Matrix A
disp(A.'); %To display result of transpose of A
disp("Multiplicative inverse of A"); %Multiplicative inverse of A
disp(A^-1) %To display multiplicative inverse of A
NAME: Muhammad Saad Saleem
SECTION: 12B
CMS ID: 336893
OUTPUT:
If we get stuck to any thing or to confirm anything we can get help by right clicking on the thing and
we would be provided with all the functions of that particular thing.