Engineering Mathematics
Week 3
Input & Output
Procedures
PRESENTED BY:
Muhammad Fadhil Abbas
Zahra Salsabila Mulyadi
Page 02
Array Example
An array is a collection of items of
same data type stored at contiguous
memory locations.
To create an array in a single row,
usually we use brackets [ ] to call the
array function in Matlab and
separate the elements with either a
comma (,) or a space
Page 03
Matrix
To create a matrix with
multiple rows, seperate the
rows with semicolons (;)
Zeros Matrix
To declare a new matrix, we
can create a matrix that
contain 0 number as the
elements for all columns and
rows. To call zeros matrix, use
function zeros(row,collumn)
Page 04
Array and Matrik Indexing
Min and Max Function.
To refer a particular element in To find minimum and maximum value on the
an array or matrix, use array
MatrixName(element number)
or MatrixName(Row,Column)
Specific Row Specific Element All Elements in
and Column Number Specific
b = a(2,3) b = a(3) Row/Column
b = a(3, :)
Page 05
Changing Creating Linspace
Array’s Element Random Number Function
To change on of the Linspace (x1,x2) used
element in array, use Use rand(row,column) to creat a row vector of
indexing by declaring to create a matrix with 100 evenly between x1
the specific row and random elements and x2
column of the element between 0 and 1. Linspace (x1,x2,n)
that you want to used to generates “n”
replace points
Page 06
Array and Matrix Operation Matrix Multiplication Elementwise Multiplication
(a*b) (a.*b)
We can perform standard matrix operation by
using standard operator such as (*), (^), etc. or
element-wise operation by using (.) before
standar operator.
* is matrix multiplication while .* is elementwise
multiplication.
In order to use the first operator, the operands
should obey matrix multiplication rules in terms
of size.
For the second operator vector lengths (vertical
or horizontal directions may differ) or matrix
sizes should be equal for elementwise
multiplication
Page 07
Transpose and
Inverse Matrix
To do a transpose use
(Matrix’) and for
inverse use inv(Matrix)
Page 08
Tables Converting array to table
Use array2table(Array,’VariableNames’,Variable Names)
Table can contain different types of
data, though all elements in a column
must be the same. A matrix/array must
have all elements the same, even in
different columns. except for a cell array
which can have different kinds of
variables at an element no matter where
it is (no requirement that all elements in
a column of a cell array contain all the
same type of variable like is the case
with tables and matrices).
PLOTTING
WEEK 3 ENGINEERING MATHEMATICS PRACTICUM
Page 10
Plotting
Syntax:
Make sure that the matrices/vectors you
are trying to use are of equal dimensions.
Labeling Axes:
To give the plot a title and axis labels.
Legends:
To distinguish multiple dependent
vectors on the same plot
Additional commands:
grid on; %to put grid lines on the graph
axis equal; %to generate the plot with the same scale
factors and the spaces on both axes
Page 11
Plotting
Subplots: 3-D Plots:
To display multiple plots on the same figure for comparison. u to generate some kind of x-y plane and
then apply a 3rd function as the z dimension.
Page 12
Plotting
Contour, pcolor, surf functions:
Using Matrix: Using Linspace:
EXCEL
WEEK 3 ENGINEERING MATHEMATICS PRACTICUM
Page 13
Importing Data from Excel to Matlab
We can import data using the Import Tool, readtable, or xlsread.
A = readtable(‘Week3.xls’,’Range’,’B2:D10’)
B = xlsread(‘Week3.xls’,’B2:D7’)
%reads the first worksheet and returns the
numeric data as matrix.
Click to import the
excel file to the
Workbook
Page 14
Importing Data from Excel to Matlab
When we imported data from excel, it will show up in Workspace. If we use “clear” or “clear all” and run the
program, the data we’ve imported will be cleared from Workspace. We have to save the data as
(data_name.mat) and use “load data_name.mat” if we want to call the data.
vvv
Saving the file in “.mat’ format
Page 15
Exporting Data from Matlab
Create a sample table of column-oriented data and Write matrix A to the second sheet in a file
display the first two rows. named “Contoh.xls” starting in cell A2.
Write table T to the second sheet in a file named vvv
“Contoh.xls” starting at cell E5.
Use xlswrite to write matrix A.
Note
Both excel file and matlab file
should be in the same folder.
QUESTIONS
WEEK 3 ENGINEERING MATHEMATICS PRACTICUM
Page 17
Question 1
To model river flow, an analysis of the river
cross-section is required. To obtain this, it is
necessary to measure several points of depth
along the river cross-section.
Create a program that can receive input for
the river's width, the number of measurement
segments, and the measurement results
conducted. Then display the obtained river
cross-section.
Page 18
Question 1
Page 19
Question 2
Create a program that prints a
purchase receipt by entering the
price of the item and the purchase
amount of each item.
*The Output must be shown in a
table form
Page 20
Question 3
You are tasked to analyze air pollution data collected from a monitoring
station in Jakarta. You want to create line plots to visualize how the
concentrations of two different pollutants (PM2.5 and NO2) vary over time.
Option 2
NOTE
To customise plot
markers, you can visit
the link below:
Create Line Plot with
Markers
Page 21
Question 3 Option 1
1. Allows users to input data. This tells
matlab to
2. Provide an option to either stop asking
for input
generate two line plots (one for
each parameter) in one graph or in
two subplots within the same
figure.
3. To terminate data entry, user can
Don‘t forget to
enter “-1“ for PM2.5 concentration add the legend.
or you may set some other value.
Page 22
Question 4
Create an Excel file containing information
about 3 pipes. The Excel file has one column:
'Water Depth (m)'. You need to calculate the
Hydraulic depth for each pipe using the formula:
where ‘y’ is the water depth.
Page 23
Question 4
1. User must import Excel file.
2. Check the data for validity:
- Check for non-integer or negative
values in the data.
3. Calculate the hydraulic depth (D)
for each pipe using the formula.
4. Display the input data and the
calculated hydraulic depth.
Thank You
HAVE AN A++ DAY!
bit.ly/Week3Matrek