0% found this document useful (0 votes)
76 views

Ce Matlab Report

This document discusses an introduction to MATLAB programming. It provides objectives to learn MATLAB fundamentals, matrices, and basic operations. It describes MATLAB software and commands like clear, clc, help, and edit. It explains how to enter matrices and perform arithmetic operations like addition, subtraction, multiplication, and division on matrices. It includes exercises to practice basic operations, calculate fluid flow parameters, and perform matrix operations like multiplication, element-wise multiplication, transpose, and inverse. The conclusions discuss learning MATLAB interface and programming through hands-on exercises.

Uploaded by

Sheikh Saad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Ce Matlab Report

This document discusses an introduction to MATLAB programming. It provides objectives to learn MATLAB fundamentals, matrices, and basic operations. It describes MATLAB software and commands like clear, clc, help, and edit. It explains how to enter matrices and perform arithmetic operations like addition, subtraction, multiplication, and division on matrices. It includes exercises to practice basic operations, calculate fluid flow parameters, and perform matrix operations like multiplication, element-wise multiplication, transpose, and inverse. The conclusions discuss learning MATLAB interface and programming through hands-on exercises.

Uploaded by

Sheikh Saad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

NAME: Muhammad Saad Saleem

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

Operation Purpose Description

+ Addition A+B adds A and B.

+ Unary plus +A returns A.

- Subtraction A-B subtracts B from A


- Unary minus -A negates the elements of A.
.* Element-wise A.*B is the element-by-element product of A and B.
multiplication
.^ Element-wise A.^B is the matrix with elements A(i,j) to
power the B(i,j) power.
./ Right array A./B is the matrix with elements A(i,j)/B(i,j).
division
.\ Left array A.\B is the matrix with elements B(i,j)/A(i,j).
division
.' Array A.' is the array transpose of A. For complex matrices, this
transpose does not involve conjugation.
NAME: Muhammad Saad Saleem
SECTION: 12B
CMS ID: 336893

Table 2: Matrix arithmetic operators in MATLAB

Operator Purpose Description

* Matrix C = A*B is the linear algebraic product of the


multiplication matrices A and B. The number of columns of A must
equal the number of rows of B.
\ Matrix left x = A\B is the solution to the equation Ax = B.
division Matrices A and B must have the same number of rows.

/ Matrix right x = B/A is the solution to the equation xA = B.


division Matrices A and B must have the same number of columns.
In terms of the left division operator, B/A = (A'\B')'.
^ Matrix power A^B is A to the power B, if B is a scalar. For other values
of B, the calculation involves eigenvalues and
eigenvectors.
' Complex A' is the linear algebraic transpose of A. For complex
conjugate matrices, this is the complex conjugate transpose.
transpose

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:

CONCLUSIONS AND DISCUSSION:


During this lab we were introduced to the interface of MATLAB and introduction to MATLAB
programming was given which is relatively easy as compared to the other programming languages
and it can be used to perform different mathematical operations which could be very hard to find
manually. We were given task to use certain functions of MATLAB taking input from the user
performing different operations on the MATLAB and then displaying the operations on the output
screen. The way we write matrixes and performed operations on that.

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.

You might also like