You are on page 1of 134

GNU OCTAVE

MATLAB
1
INTRODUCTION
• Octave and MATLAB are high-level languages, primarily
intended for numerical computations.
• They provide a convenient command line interface for solving
linear and nonlinear problems numerically.
• They can also be used for prototyping and performing other
numerical experiments.
• Octave is freely redistributable software. You may redistribute
it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation.
• This document corresponds to Octave version 4.0.0.

2
About GNU Octave
• GNU Octave is a high-level language, primarily intended for
numerical computations. It provides a convenient command
line interface for solving linear and nonlinear problems
numerically, and for performing other numerical experiments
using a language that is mostly compatible with Matlab
• Octave was originally conceived (in about 1988) to be
companion software for an undergraduate-level textbook on
chemical reactor design being written by James B. Rawlings of
the University of Wisconsin-Madison and John G. Ekerdt of the
University of Texas.
• Octave is now much more than just another courseware
package with limited utility beyond the classroom.
3
• Today, thousands of people worldwide are using Octave in
teaching, research, and commercial applications.
Installing Octave (Windows)
• Download from gnu.org
https://www.gnu.org/software/octave/download.html
https://ftp.gnu.org/gnu/octave/windows/
Select  octave-4.0.0_0-installer.exe
• Download Python – Version 2.7.10
https://www.python.org/downloads/
• Download SymPy – Version 0.7.6.1
https://github.com/sympy/sympy/releases
sympy-0.7.6.1.win32.exe
• Install Python 2.7 and SymPy in C:\Python27\
• Install Octave in C:\Octave\ 4
Launch Octave (Packages)
• Launch octave
• Locate the folder C:\Octave\Octave-4.0.0\src
• All the packages are in this folder
• Download the latest packages from octave.sourceforge.net
http://octave.sourceforge.net/packages.php
• Copy these packages into C:\Octave\Octave-4.0.0\src
• There is a script called build_packages.m
• Edit this script to include the latest packages
• Run the script by typing build_packages in the console
windows
5
Install Packages using
build_packages.m

6
Install Packages using
build_packages.m
• % install the packages • try_install fl-core-1.0.0.tar.gz
• %try_install general-1.3.4.tar.gz • %try_install tsa-4.2.9.tar.gz
• try_install general-2.0.0.tar.gz • try_install tsa-4.3.2.tar.gz
• try_install miscellaneous-1.2.1.tar.gz • try_install dicom-0.1.1.tar.gz
• try_install struct-1.0.11.tar.gz • %try_install netcdf-1.0.6.tar.gz
• try_install optim-1.4.1.tar.gz • try_install netcdf-1.0.7.tar.gz
• try_install specfun-1.1.0.tar.gz • try_install ltfat-2.1.0.tar.gz
• %try_install control-2.8.1.tar.gz • try_install database-2.3.2.tar.gz
• try_install control-2.8.5.tar.gz • try_install instrument-control-0.2.1.tar.gz
• %try_install signal-1.3.1.tar.gz • %try_install generate_html-0.1.8.tar.gz
• try_install signal-1.3.2.tar.gz • try_install generate_html-0.1.9.tar.gz
• try_install communications-1.2.1.tar.gz • try_install financial-0.4.0.tar.gz
• %try_install image-2.4.0.tar.gz • %try_install stk-2.3.0.tar.gz
• try_install image-2.4.1.tar.gz • try_install stk-2.3.2.tar.gz
• %try_install io-2.2.7.tar.gz • try_install splines-1.2.8.tar.gz
• try_install io-2.2.10.tar.gz • try_install dataframe-1.1.0.tar.gz
• try_install statistics-1.2.4.tar.gz • try_install lssa-0.1.2.tar.gz
• try_install geometry-2.0.0.tar.gz • try_install queueing-1.2.3.tar.gz
• try_install windows-1.2.1.tar.gz • %try_install nurbs-1.3.9.tar.gz
• try_install odepkg-0.8.5.tar.gz • try_install nurbs-1.3.10.tar.gz
• try_install linear-algebra-2.2.2.tar.gz • try_install octcdf-1.1.8.tar.gz
• try_install sockets-1.2.0.tar.gz • %try_install strings-1.1.0.tar.gz
• try_install zenity-0.5.7.tar.gz • try_install strings-1.2.0.tar.gz
• try_install data-smoothing-1.3.0.tar.gz • try_install ga-0.10.0.tar.gz
• %added extra


try_install fuzzy-logic-toolkit-0.4.5.tar.gz
try_install quaternion-2.4.0.tar.gz • %try_install symbolic-2.2.2.tar.gz 7
• try_install fits-1.0.5.tar.gz • try_install vrml-1.0.13.tar.gz
Installing and removing
packages
• You can show the list of currently installed packages
pkg list
• You can add named packages to the path
pkg load all
pkg load image
pkg load control
• You can remove named packages from the path
pkg unload all
pkg unload image
pkg unload control
• Check installed Octave-Forge packages against repository and
update any outdated items. This requires an internet connection and
8
the cURL library.
pkg update
Problem Solving in
Engineering and
Science
Recommended book
“MATLAB for Engineers”, 4E by Holly Moore

9
Problem Solving in
Engineering and Science
1. State the Problem
2. Describe the input and output
3. Develop an algorithm
4. Solve the problem
5. Test the solution

10
State the Problem
• If you don’t have a clear understanding of the problem, it’s
unlikely that you’ll be able to solve it
• Drawing a picture often helps you understand the system
better

11
Describe the Input and
Output
• Be careful to include units
• Identify constants
• Label your sketch
• Group information into tables

12
Develop an Algorithm
• Identify any equations relating the knowns and unknowns
• Work through a simplified version of the problem by hand or
with a calculator
• Developing a flow chart is often useful for complicated
problems

13
Solve the problem
• Create a MATLAB solution
• Be generous with comments, so that others can follow your
work

14
Test the Solution
• Compare to the hand solution
• Do your answers make sense physically?
• Is your answer really what was asked for?
• Graphs are often useful ways to check your calculations for
reasonableness

If you use a consistent problem solving strategy you increase


the chance that your result is correct.

Here’s an example….

15
Example 1
• Albert Einstein
• E=mc2
• The sun is fueled by
the conversion of
matter to energy
• How much matter
does the sun
consume every day?

16
State the Problem
• Find the amount of matter necessary to
produce the amount of energy radiated by
the sun everyday

17
Describe the Input and
Output
• Input
• Rate of energy radiation
• E = 385*1024 Joules/second
• Speed of light
• c = 3.0*108 meters/second
• Output
• Mass in kilograms

18
Develop an Algorithm
Hand Example
• The energy radiated in one day is:

J sec hours
385  1024
 3600  24 * 1day  3.33  10 31 J
sec hour day

• Rearrange E=mc2 and solve for m


• m=E/c2
1 J = 1 kg m2/sec2
3.33  10 31 J J
m  3.7  10 14

3.0 10 m / sec 


8 2
m 2 / sec 2
2 2
kg m / sec 19
 3.7  10 2
14
2
 3 . 7  10 14
kg
m / sec
Develop a OCTAVE/MATLAB
Solution to Solve the Problem
• You can see from the following
demonstration just how easy it is to
use the command window

20
OCTAVE/MATLAB Solution

21
Test your Solution
• Matches the hand solution
• Is it reasonable?
• Consider…
• Mass of the sun = 2*1020 kg
• How long would it take to consume all
that mass?

22
Test your Solution
time = (mass of the sun)/(rate of consumption)

2  10 30 kg year
time  *  1. 5  10 13
years
3.7  10 kg / day 365days
14

That’s 15 trillion years!!


Yes – this is a reasonable
result

23
EXAMPLE 2
• STATE THE PROBLEM
If Mercury, Venus and the sun are aligned in a right triangle, as
shown, then calculate the vector sum of the forces on Venus due
to both Mercury and the Sun. What is the direction and
magnitude of the resulting force? (Sun-Venus distance rv =
108×109 meters, Sun-Mercury distance rm = 57.6×109 meters,
mass of Sun Ms = 1.99×1030 kilograms, mass of Mercury Mm =
3.3×1023 kilograms, mass of Venus Mv = 4.87×1024 kilograms).

24
Alignment of Mercury, Venus and the Sun.
Describe the Input and
Output
• Input
• Sun-Venus distance
• Sun-Mercury distance
• Sun Mass
• Mercury Mass
• Venus Mass
• Output
• Direction and magnitude of the gravitational
forces 25
Develop an Algorithm
Hand Example
• The magnitude of the force on Venues due to the sun is given
by: M sMv
Fs  G 2
 5.54  10 22
[N]
rv
• The distance between Mercury and Venus is given by

rmv  rv2  rm2  1.224 1011 [m]


• The magnitude of the force from Mercury, then, is:
M mM v
Fm  G 2
 7.155  1015
[N]
rmv
The directions of these forces are along the lines connecting the planets.
In this case, the force due to the sun is more than a million times greater than the
force due to Mercury, and so the net force is very well approximated by the 26
magnitude and direction of the force due to the sun.
OCTAVE/MATLAB Solution

27
MATLAB/OCTAVE
Environment
28
Objectives
After studying this chapter you should be able to
• Start the MATLAB program and solve simple problems in the
command window
• Understand MATLAB’s use of matrices
• Identify and use the various MATLAB windows
• Define and use simple matrices
• Name and use variables
• Understand the order of operations in MATLAB
• Understand the difference between scalar, array and matrix
calculations in MATLAB
• Express numbers in either floating-point or scientific notation
• Adjust the format used to display numbers in the command window
• Save the value of variables used in a MATLAB session 29
• Save a series of commands in an M-file
Getting Started
• Launch MATLAB or OCTAVE by clicking the icon on your
desktop.
• The default window configuration opens up.

30
Exit
Help

Toolstrip

Workspace

MATLAB
Command Window
Current
folder/directory

31
Command
history
Current Folder Window
Exit
Lists files stored in the current directory

File Browser
Command Window

OCTAVE

Workspace

Command
history
32
Let’s look at the windows one
at a time

Command Window

33
Let’s look at the windows one
at a time

Command Window
Workspace Window If you double click on any variable in the
workspace window MATLAB launches a
document window containing the array
editor. In OCTAVE this feature is not
Command History 34
available
You can edit variables in the array editor
Let’s look at the windows one at
a time

Scalar

Vector

2-D 35
Matrix
Let’s look at the windows one
at a time

The Document
Window displays
the variable editor

36
Command History
• You can transfer commands from the command
history to the command window
• Double click on a command
• It executes immediately
• Click and drag into the command window
• You can edit the command before executing

• When you define variables in the


command window, they are listed in the
workspace window 37
File Browser (Document
Window)

38
Figure Window

Octave is not as flexible as MATLAB


when modifying Figures

39

Note: The semicolon suppresses the output from each command


Figure Window

Matlab makes it easy to modify graphs by adding


•Titles
•Axis labels
•Legends
•Other types of annotations 40
Editing Window
• This window allows you to type and save a series of
commands without executing them
• There are several ways to open an editing window
• From the file menu
• With the new file icon

41
New file icon
Press CTRL+N

42
Save and Run

The % sign identifies comments


You need one on each line

Write your code in the editing window,


then run it using the Save and Run icon

43
In Octave : File New New Script (CTRL+N)

44
45
Comments
• Be sure to comment your code
• Add your name
• Date
• Section #
• Assignment #
• Descriptions of what you are doing and why

46
Solving Problems with
MATLAB
• We’ve already solved some simple
problems
• We need to understand how MATLAB
works to solve more complicated
problems

47
Variables
• MATLAB allows you to assign a value
to a variable
A=3
• Should be read as A is assigned a
value of 3
• Use the variables in subsequent
calculations
48
Naming Variables
• All names must start with a letter
• They may contain letters, numbers
and the underscore ( _ )
• Names are case sensitive
• There are certain keywords you can’t
use
49
Keywords
'function'
'global'
>> iskeyword
'if'
'otherwise'
ans = 'parfor'
'persistent'
'break' 'return'
'case' 'spmd'
'catch' 'switch'
'classdef' 'try'
'continue' 'while'
'else'
'elseif'
Keywords are not acceptable variable names
'end' 50
'for'
Use the iskeyword function for a list of keywords
You can reassign function
names
• MATLAB will let you use built-in function names
as variables – but it’s a really bad idea

sin = 3 changes sin from a function to a


variable name
clear sin resets sin back to a function

51
Which of these names are
allowed in MATLAB?
• test
• Test
• ifx X
• my-book
x X
• my_book
• Thisisoneverylongnamebutisitstillallowed?x X
• 1stgroup
x X
• group_one
• zzaAbc
• z34wAwy?12#
x x X
• sin
bad
• log idea 52
Matrices in MATLAB
The basic data type
• Group of numbers arranged into rows and
columns
• Single Value (Scalar)
• Matrix with one row and one column
• Vector (One dimensional matrix)
• One row or one column
• Matrix (Two dimensional)

53
Scalar Calculations
• You can use MATLAB like you’d use a calculator

Command
Prompt
>> 9 + 10
ans=19
Result

54
Assignment Operator
• To define a variable a we might type
a=1+2
which should be read as:
“a is assigned a value of 1+2“

55
How is the assignment operator
different from an equality?
• In algebra the equation
x=3+5
means that both sides are the same
• In computers when we say
x=3+5
we are telling the machine to store the
value on the right hand side of the
equation in a memory location, and to
name that location x
56
Is that really different?
• Yes!!!
• In algebra this is not a true statement
x=x+1
• In computers (assignment statements) it means replace
the value in the memory location named x, with a new
value equal to x+1

57
Order of Operation
• Same as you’ve learned in math class
• Same as your calculator
• Parentheses first
• Exponentiation
• Multiplication / division
• Addition / subtraction

58
Order of Operation
• 5*(3+6) = 45
• 5*3+6= 21
White space does not matter!!!

• 5*3 + 6 = 21
• Adding a space around + and – signs makes the
expression more readable

59
Parentheses
• Use only ( )
• { } and [ ] mean something different
• MATLAB does not assume operators

5 * (3+4) not 5(3+4)


• Compute from left to right
5*6/6*5 = 25
5*6/(6*5) = 1
60
Example : Find the surface
area of a cylinder
r = radius h = height
r=5 h = 10

π r2
2π r * h

π r2

61
SA  2r  2rh  2r (r  h)
2
Array Operations
• Using MATLAB as a glorified calculator is OK, but
its real strength is in matrix manipulations

• x=[1 2 3 4]

To create a row vector, enclose a list of values


in brackets

You may use either a space or a


comma as a “delimiter” in a row 62
vector
Array Operations

Use a semicolon as a delimiter to create a


new row

The transpose of a row vector is a column


63
vector
Array Operations

Hint: It’s easier to keep track of how many


values you’ve entered into a matrix, if you
enter each row on a separate line. The
semicolons are optional

64
Shortcuts
• While a complicated matrix might have to be entered by
hand, evenly spaced matrices can be entered much more
readily. The command
b= 1:5
or the command
b = [1:5]
both return a row matrix

65
Shortcuts

Initial Final value


value Increment

66
To calculate spacing between
elements use…
• linspace
• logspace

number of elements in the


array

Initial value in the Final value in the


array array

67
Initial value in the array
expressed as a power of 10 Final value in the array
expressed as a power of 10
Common mistake

It is a common mistake to enter the initial and


final values into the logspace command,
instead of entering the corresponding power
of 10

68
Hint
• You can include mathematical operations inside a matrix
definition statement.
• For example
a = [0: pi/10: pi]

69
Mixed calculations between
scalars and arrays
• Matrices can be used in many calculations with scalars
• There is no confusion when we perform addition and
subtraction
• Multiplication and division are a little different
• In matrix mathematics the multiplication operator (*) has
a very specific meaning

Addition between arrays is 70


performed on
corresponding elements
Mixed calculations between
scalars and arrays

71
Multiplication between arrays is Division between arrays is performed
performed on corresponding on corresponding elements if the ./
elements if the .* operator is used operator is used
Array Operations
Element by element operators (same array size)
Array multiplication .*
Array division ./
Array exponentiation .^

In each case the size of the arrays must match

MATLAB interprets * to mean matrix


multiplication. The arrays a and b are
not the correct size for matrix 72
multiplication in this example
The matrix capability of MATLAB makes
it easy to do repetitive calculations
• For example, assume you have a list of angles in
degrees that you would like to convert to
radians.
• First put the values into a matrix.
• Perform the calculation
Either the * or the .* operator can be
used for this problem, because it is
composed of scalars and a single
matrix
The value of pi is built into MATLAB as
a floating point number, called pi
73
Transpose
• The transpose operator changes rows to columns or vice
versa.

The transpose operator


makes it easy to create
tables
The transpose operator works on both one
dimensional and two dimensional arrays

table =[degrees;radians]’ would have given 74


the same result
Number Display
• Scientific Notation
• Although you can enter any number in decimal
notation, it isn’t always the best way to
represent very large or very small numbers
• In MATLAB, values in scientific notation are
designated with an e between the decimal
number and exponent. (Your calculator
probably uses similar notation.)

75
Number Display & Display Format
It is important to omit blanks between
the decimal number and the exponent.
For example, MATLAB will interpret
6.022 e23
as two values (6.022 and 1023 )

DISPLAY FORMAT
• Multiple display formats are available
• No matter what display format you choose, MATLAB uses
double precision floating point numbers in its
calculations
76
• MATLAB handles both integers and decimal numbers as
floating point numbers
Default Display Format
• The default format is called short
• If an integer is entered it is displayed without trailing zeros
• If a floating point number is entered four decimal digits are displayed

• Changing the format affects all subsequent displays


• format long results in 14 decimal digits
• format bank results in 2 decimal digits
• format short returns the display to the default 4 decimal digits

Really Big and Really Small


• When numbers become too large or too small for MATLAB to display using
the default format, it automatically expresses them in scientific notation
• You can force scientific notation with
• format short e
• format long e

77
• format +
• format rat
Display Format

78
Display Format

The format command also allows us to control how


tightly information is spaced in the command window
format compact
format loose – (default)
Most of the examples in this presentation use format
compact 79
Notice that the value of A is still being displayed
using the rat format, because we haven’t
changed it back to format short
Common Scale Factor
• For long and short formats, a common scale factor is applied
to the entire matrix if some of the elements become very
large, or very small. This scale factor is printed along with the
scaled values.

Common Scale
Factor

80
Saving Your Work
• If you save a MATLAB session performed in the command
window, all that is saved are the values of the variables you
have named

81
Saving Your Work

Variables are saved,


not the commands in
the command window

82
Saving Your Work

Save with a command in the


command window 83
MATLAB automatically saves
to a .mat file
• If you want to save to another format, such
as .dat, you need to explicitly tell the program
save <file_name> <variable_list> -ascii

Again – Remember that the only things being


saved are the values stored in the workspace
window – not the commands from the
command window

• In Octave FileSave Workspace As


84
Script M-files
• If you want to save your work,
(the commands you entered)
you need to create an M-file
• File->New->M-file
• Type your commands in the edit window that opens
• The file can be saved into the current folder/directory
• It runs in the command window
• The file can be saved into the current folder/directory
• It runs in the command window

85
Script M-files

You can dock the editing


Save the file using window with the
the save icon, or the MATLAB desktop, by
file menu pressing
CTRL+SHIFT+D 86
Script M-files

This arrangement is
often easier to use

87
Script M-files

When I
execute the The figure
file, the figure window can also
appears on be docked onto
top of the the MATLAB
MATLAB desktop, using 88
desktop the docking arrow
Cell Mode

89
MATLAB/OCTAVE
Functions
90
Using Built-in Functions
• MATLAB uses function names consistent with most major
programming languages
For example
• sqrt
• sin
• cos
• log
• MATLAB contains a wide array of predefined functions
• Elementary Math Functions
• Trigonometric Functions
• Data Analysis Functions
• Random Numbers
91
• Complex Numbers
Using Predefined Functions
• Functions consist of
• Name
• Input argument(s)
• Output

92
Some functions require
multiple inputs
• Remainder function returns the remainder in a division
problem
• For example the remainder of 10/3, is 1

• size function determines the number of rows and columns

The variable names are


arbitrary – choose something
that makes sense in the
context of your problem
93
Nesting Functions
• It is a function defined inside another function

94
Using the Help Feature
• There are functions for almost anything you
want to do
• Use the help feature to find out what they are
and how to use them
• From the command window
• From the help selection on the menu bar

95
From the Command Window

From the Help Menu

96
The windowed help function can also be accessed using the doc command
97
>> doc sqrt

98
Elementary Math Functions
• log(x) natural log
• log10(x) log base 10

Rounding Functions
• round(x)
• fix(x)
• floor(x)
• ceil(x)

99
100
Elementary Math Functions

101
Discrete Mathematics
• factor(x)
• gcd(x,y) greatest common denominator
• lcm(x) lowest common multiple
• rats(x) represent x as a fraction
• factorial(x)
• nchoosek(n,k)
Binomial coefficient or all combinations.
• primes(x)
• isprime(x)
102
Discrete Mathematics

103
Trigonometric Functions
• sin(x) sine
• cos(x) cosine
• tan(x) tangent
• asin(x) inverse sine
• sinh(x) hyperbolic sine
• asinh(x) inverse hyperbolic sine
• sind(x) sine with degree input
• asind(x) inverse sin with degree
output

104
Trigonometric Functions

105
Data Analysis Functions
When the max function is used with a vector
• max(x) (either a row or a column), it returns the
• min(x) maximum value in the vector

• mean(x) When x is a matrix, the max


is found for each column
• median(x)
• sum(x)
• prod(x)
• sort(x)
• sortrows(x)
• std(x)
• var(x)
106

The max function can also be used to


determine where the maximum occurs
Data Analysis Functions

max value

index number where the


max value occurs

Vector of maximums

107

Vector of row numbers


Data Analysis Functions
Sorting Values

It’s easy to sort data in MATLAB, using


the sort function
The default is to sort in ascending order

To sort in descending order, just add the


word ‘descend’ in the second input field

108
Data Analysis Functions
The sortrows function
allows you to sort
entire rows, based on
the value in a
specified column.
The default sorting
column is #1

MATLAB is column dominant, so when sort is


used with a 2-D matrix, each column is
sorted in ascending order

109
Data Analysis Functions

In this example the matrix is sorted in ascending


order, based on the second column

Notice that this is a different


strategy than that used by the
sort function!

To sort based on descending order, place a


negative sign in front of the column number

110
Data Analysis Functions I

111
Data Analysis Functions II

112
Data Analysis Functions III

113
Data Analysis Functions IV

114
Data Analysis Functions V

115
Data Analysis Functions VI

116
Data Analysis Functions VII

117
Data Analysis Functions VIII

118
Determining Matrix Size
• size(x) number of rows and columns
• length(x) biggest dimension
• numel(x) total number of elements

119
Size functions

120
Variance and Standard Deviation
• std(x) 

• var(x)  2

 x  
2
k
  2 k 1
121
N 1
Variance and Standard
Deviation

122
Random Numbers
• rand(x)
• Returns an x by x matrix of random numbers between
0 and 1
• rand(n,m)
• Returns an n by m matrix of random numbers
• These random numbers are evenly distributed

If you create a very large matrix of


random numbers using the rand
function, the average value will be
0.5
Notice that we created a 1 by 107 matrix, which
required 2 inputs (rand(1,10e6)). If we had 123
entered a single value (rand(10e6)) the result
would have been a 1x107 by 1x107 matrix.
Gaussian Random numbers
• randn(n)
• Also called a normal distribution
• Generates numbers with a mean of 0 and a standard deviation
of 1
First generate an array of 10 million gaussian
random numbers

Use MATLAB to take the mean, and notice that it is


very close to 0
Use MATLAB to find the standard deviation, and
6

notice that it is very close to 1


x 10
4

3.5

The hist function creates a 3

2.5

histogram of the input data 2 124


1.5

0.5

0
-6 -4 -2 0 2 4 6
Gaussian Random numbers
• To generate random numbers between other bounds…

x  b  a   r  a
a and b are the upper and lower bounds
r is the array of random numbers

125
Random-Number Generators

126
Gaussian Random numbers
clear all; figure(3)
close all;
clc; plot(1:length(scores_2),scores_2
)
scores_1=random('Normal',50,4,1,1000); xlabel('Student Number')
scores_2=random('Normal',50,8,1,1000);
ylabel('Score')
figure(1) title('Test Scores')
plot(1:length(scores_1),scores_1)
text(500,90,'Average = 50')
xlabel('Student Number')
ylabel('Score') axis([0 1000 0 100])
title('Test Scores')
text(500,90,'Average = 50')
Mean, standard deviation,
figure(4)
axis([0 1000 0 100])
array size (row vector of 1000
hist(scores_2,linspace(0,100,50))
figure(2) elements)
axis([0 100 0 200])
hist(scores_1,linspace(0,100,50))
axis([0 100 0 200]) text(70,150,'Average = 50') 127
text(70,150,'Average = 50')
Test Scores
100 200

90 Average = 50 180

80 160
Average = 50
70 140

60 120
Score

50 100

40 80

30 60

20 40

10 20

0 0
0 100 200 300 400 500 600 700 800 900 1000 0 10 20 30 40 50 60 70 80 90 100
Student Number

Test Scores
100 200

90 Average = 50 180

80 160
Average = 50
70 140

60 120
Score

50 100

40 80

30 60

20 40

10 20

0 0
0 100 200 300 400 500 600
Student Number
700 800 900 1000 0 10 20 30 40 50 60 70 80 90 100
128
Although the average is the same for each of these data sets,
they have a different standard deviation
More about Manipulating
Matrices
• M(:)
• Converts a two dimensional matrix to a single column. The matrix
is read by columns

129
Complex Numbers
• complex(x,y)
• real(A) used if A is a
complex number
• imag(A)
• isreal(A)
• conj(A)
• abs(A) imaginary
• angle(A) real

130
Complex numbers

131
Computational Limits
• MATLAB’s computational range on most computers is:
• 10-308
• 10308
• When you divide by 0, the computer returns Inf

• Check the limits on your computer with these commands


• realmax
• realmin
• intmax
• intmin

132
Results may depend on the
order of operation
• When using very large or very small numbers the result
may depend on the order of operation

133
Special Values and
Miscellaneous Functions
• pi
• i,j Hint: The function i is the
• Inf
most common of these
• NaN
• clock functions to be
• date unintentionally renamed
• eps by MATLAB users.
• ans
134

You might also like