Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

NUMERICAL METHODS AND

COMPUTER PROGRAMMING

BY: GIRMA B. (M.SC.)


FOR POST GRADUATE STUDENTS IN ENG. HYDROLOGY AND SOIL
AND WATER CONSERVATION ENG. STUDENTS ONLY

NOVEMBER 2021
COURSE OBJECTIVES

❑relevant numerical techniques useful in hydraulic


computations/hydrology/soil and water conservation engineering and
Irrigation and drainage engineering. This course will also introduce
programming languages.

❑ The student should be able to program numerical methods


applicable to water resources problems.
2

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
INTRODUCTION: PROGRAMMING LANGUAGES

❑First high-level language: FORTRAN (1954)


o To make programming easier, especially to solve numerical problems
❑Today’s languages: 1970s
o For commercial, “shrink-wrapped”, large, complex programs: C, C++,
Java, etc
o To make programming easier, especially to solve numerical problems:
MATLAB
o Same purpose as FORTRAN, but FAR easier to program
3

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
MATLAB HISTORY

❑ Invented by Prof. Cleve Moler to make programming easy for his students
o Late 1970
o University of New Mexico
❑The MathWorks, Inc was formed in 1984
o By Moler and Jack Little
o One product: Matlab
❑Today
o 100 products
o Over 1,000,000 users
4

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
GETTING INTO MATLAB

Definition:
oMATLAB is a mathematical and graphical software package with
numerical, graphical, and programming capabilities.
o has built-in functions: operations, toolboxes.

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
MATLAB 2013 SYSTEM REQUIREMENT
• Operating System: Windows XP, Vista, 7 and 8
• Processor: Pentium III
• RAM: 1GB
• Hard Disk Space: 1GB for MATLAB only, 3-4GB for typical installation.
• Technical Setup Details For MATLAB 2013:
• Setup Full Size: 5.63 GB

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
MATLAB COMMAND WINDOW
o The >> in
command window
is called the
prompt.

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B 7
GETTING HELP

o demo will bring up MATLAB examples in the Help Browser


o help will explain any function; explain how help works
o lookfor searches through the help for a specific word or phrase (note: this
can take a long time)
o doc will bring up a documentation page in the Help Browser.
o quit/exit: to quit the Matlab session

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
THE MATLAB DESKTOP ENVIRONMENT
o The current folder window
o The workspace
o The editor window
o The command window
Desktop Toolstrips tabs: HOME, PLOTS, APPS, EDITORS, PUBLISH, VIEW
Under these tabs: various Functional Sections are included.

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
FUNCTIONAL CATEGORICAL LIST
o Arrays and Matrices
a)“Basic Information”
b)“Operators”
c)“Operations and Manipulation”
d)“Elementary Matrices and Arrays”
e)“Specialized Matrices”

10

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
“BASIC INFORMATION”
islogical True for logical array

disp Display array isnumeric True for numeric arrays

display Display array isscalar True for scalars

isempty True for empty matrix issparse True for sparse matrix

isequal True if arrays are identical isvector True for vectors

isfloat True for floating-point arrays length Length of vector

isinteger True for integer arrays numel Number of elements

ndims Number of dimensions size Size of matrix


11

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
Examples:
A = [1 2 3; 4 5 6; 7 8 9] B = [2 4 6; 8 10 12];
Practice with those “basic functions”

C= 5.5 - 3.5i;
See what will happen to this function
isinteger (A);
a = Int8(C);
isinteger(a);

12

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
OPERATORS
+ Addition ' Transpose

+ Unary plus .' Nonconjugated transpose

- Subtraction .* Array multiplication (element-wise)

- Unary minus .^ Array power (element-wise)

* Matrix multiplication .\ Left array divide (element-wise)

^ Matrix power ./ Right array divide (element-wise)

\ Backslash or left matrix divide / Slash or right matrix divide


13

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
❑See other information on functional categorical list on “Matlab
Book_V2”

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B 14
FILE I/O: OPENING, LOADING AND SAVING FILE

❑ Functions to read and write data to files of different format types.


“Filename Construction” Get path, directory, filename
information; construct filenames
“Opening, Loading, Saving Files” Open files; transfer data between files
and MATLAB workspace
“Low-Level File I/O” Low-level operations that use a file
identifier (e.g., fopen, fseek, fread)
“Text Files” Delimited or formatted I/O to text files
“Scientific Data” CDF, FITS, HDF formats

“Images” Graphics files


15

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
OPENING, LOADING, SAVING FILES

importdata Load data from various types of files

load Load all or specific data from MAT or ASCII file

open Open files of various types using appropriate editor or


program
save Save all or specific data to MAT or ASCII file
uiimport Open Import Wizard, the graphical user interface to import
data
winopen Open file in appropriate application (Windows only)

16

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
LOW-LEVEL FILE I/O
fclose Close one or more open files

ferror Query MATLAB about errors in file input or output

fopen Open file or obtain information about open files


fprintf Write formatted data to file
fread Read binary data from file

fscanf Read formatted data from file

fseek Set file position indicator

ftell Get file position indicator

fwrite Write binary data to file 17

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
TEXT FILES

csvread Read numeric data from text file, using comma delimiter

csvwrite Write numeric data to text file, using comma delimiter

dlmread Read numeric data from text file, specifying your own
delimiter
dlmwrite Write numeric data to text file, specifying your own delimiter
textread Read data from text file, write to multiple outputs

textscan Read data from text file, convert and write to cell array

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B 18
SPREADSHEETS
Microsoft Excel Functions

xlsfinfo Determine if file contains Microsoft Excel (.xls) spreadsheet

xlsread Read Microsoft Excel spreadsheet file (.xls)


xlswrite Write Microsoft Excel spreadsheet file (.xls)
Scientific Data (E.g. Common Data Format (CDF) and Hierarchical Data
Format (HDF))
cdfepoch Convert MATLAB date number or date string into CDF epoch

cdfread Read CDF file

cdfwrite Write CDF file

hdfread Read HDF4 file

hdf5write Write data to file in HDF5 format, etc 19

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
GRAPHICS: BASIC PLOTS AND GRAPHS; ANNOTATING PLOTS;
SPECIALIZED PLOTS SYNTAXES

• 2-D graphs, specialized plots (e.g., pie charts, histograms, and contour plots),
function plotters, and Handle Graphics functions.

20

PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B
BASIC PLOTS AND GRAPHS

box Axis box for 2-D and 3-D plots

errorbar Plot graph with error bars


hold Hold current graph
LineSpec Line specification syntax

loglog Plot using log-log scales

semilogx Semi-log scale plot

semilogy Semi-log scale plot

subplot Create axes in tiled positions


21
Plot Plot vectors or matrices
PREPARED ONLY FOR ENG HYDROLOGY AND SWC ENGINEERING STUDENTS; PREPARED BY: GIRMA B

You might also like