Professional Documents
Culture Documents
111
Numerical Methods for
Chemical Engineering Analysis
WEEK 3
Introduction to MATLAB Programming
Maya K Endoh
Algorithms
An algorithm is the sequence of steps needed to
solve a problem
Modular program: Top-down design approach
to programming
break a soluRon into steps, then further rene each
one
Source code
Interpret
high-level language
(English, etc)
Executed File
compiler
machine language
(FORTRAN, C, etc)
Translate from the source
code to object code
interpreter
Translate each command
line-by-line
Object Code
command and
operation
Script File
Scripts
Scripts are les in MATLAB that contain a sequence of
MATLAB instrucRons, implemenRng an algorithm
Scripts are interpreted (rather than compiled), and are
stored in M-les (les with the extension .m)
To create a script, click on New Script under the HOME
tab; this opens the Editor
Once a script has been created and saved, it is executed
by entering its name at the prompt
>>script1
DocumentaRon
Scripts should always be documented using
comments, %
Comments are used to describe what the script does,
and how it accomplishes its task
Comments are ignored by MATLAB
Comments are anything from a % to the end of that
line; longer comment blocks are contained in
between %{ and %}
In parRcular, the rst comment line in a script is
called the H1 line; it is what is displayed with help
Help command Rp
If you comment in 2 lines,
block comment with %{ and %} will be displayed in
help command
If there is a blank line between the 2 lines, the only 1st
line will be displayed as H1 line
Script A
Script B
1
%This script is to demonstrate
2
%How the script file is executed
3 - radius=5
4 - area=pi*(radius^2)
1
%This script is to demonstrate
2
3
%How the script file is executed
4 - radius=5
5 - area=pi*(radius^2)
EX) H1 line
Script C
1 %{
2 This script is to demonstrate
3 How the script file is
executed
4 %}
5- radius=5
6- area=pi*(radius^2)
Input FuncRon
The input funcRon does two things: prompts
the user, and reads in a value
General form for reading in a number:
variablename = input(prompt string:_)
EX)
Try it!
Input matrix
Output FuncRon
There are two basic output funcRons:
disp, which is a quick way to display things
fprin4, which allows formalng
The fprin4 funcRon uses format strings which include place
holders; these have conversion characters:
%d integers
Species where the value of the
%f oats (real numbers)
expression that is aper the string is to be
printed
%c single characters
%s strings
Use %#x where # is an integer and x is the conversion character
to specify the eld width of #
%#.#f species a eld width of # and the number of decimal
places of #
%.#x species just the number of decimal places (or characters in
a string); the eld width will be expanded as necessary
Formalng Output
Other formalng:
\n : newline character
(the output which follows aper \n moves down to the next line)
\t : tab character
lep jusRfy with - e.g. %-5d
to print one slash: \\
to print one single quote: (two single quotes)
Examples of fprin4
Expressions aper the format string ll in for the place
holders, in sequence
>> fprintf('The numbers are %4d and %.1f\n', 3, 24.59)
The numbers are
3 and 24.6
would print:
Hello and how
are you?
>>
In Command Window
1.4189
4.2176
9.1574
>> mat
mat =
9.6489
1.5761
9.7059
1.4189
4.2176
9.1574
9.5717
4.8538
8.0028
Example SoluRon
thrscript.m
Simple Plots
Simple plots of data points can be created using plot
To start, create variables to store the data (can store one or more point
but must be the same length); vectors named x and y would be common
or, if x is to be 1,2,3,etc. it can be omired
plot(x,y) or just plot(y)
The default is that the individual points are plored with straight line
segments between them, but other opRons can be specied in an
addiRonal argument which is a string
opRons can include color (e.g. b for blue, g for greeen, k for black,
r for red, etc.)
can include plot symbols or markers (e.g. o for circle, +, *)
can also include line types (e.g. -- for dashed)
For example, plot(x,y, g*--)
color
symbol (maker)
line type
blue
point
solid
green
circle
dored
red
x-mark
-.
dashdot
cyan
plus
--
dashed
magenta
star
(none)
no line
yellow
square
black
diamond
white
triangle(down)
triangle(up)
<
triangle(lep)
>
triangle(right)
pentagram
hexagram
EX)
Plot the following (x, y) points in one graph
(1,1) (2,5) (3,3) (4,9) (5, 11) (6, 8)
In the Command Window, you may type
>> x=1:6;
>> y=[1 5 3 9 11 8];
>> plot(x,y)
>> plot(x,y,'r*')
>> plot(x,y,'r*-')
>> xlabel('x')
>> ylabel('y')
>> title('x and y plot')
Exercise
Write the script that creates two separate Figure Windows
and plot two separate y1, y2 vector values of y1=[2 11 6 9 3]
and y2=[4 5 8 6 2] against x=[1 2 3 4 5 6].
First, it clears the Figure Window
Then it creates x, y1, and y2 vectors.
In the rst Figure Window, just plot x vs y1 in bar graph
and add label for x, y axis and Rtle of x, y bar plot.
In the second Figure Window, plot y1 values as black lines
and plot y2 values as red circles. Add label for x, y axis,
legend and grid. Add the Rtle of x vs y1 & y2.
Exercise
Write a script that will calculate the volume of
a hollow sphere,
4
V
=
(r r )
3
Where ri is the inner radius and ro is the outer
radius. Assign a value to a variable for the inner
and outer radius
3
o