Document 5632250

advertisement
MATLAB FUNDAMENTALS:
MATRIX/ARRAY FUNCTIONS
THE COLON
MATRIX/ARRAY MANIPULATION
INPUT/OUTPUT
HP 100 – MATLAB
Wednesday, 9/3/2014
www.clarkson.edu/class/honorsmatlab
Before We Begin:

Any Questions? Comments? Concerns?
 Feel
free to contact Joe or Jim
 We
can set up small group tutoring or one-on-one
 You can email us with any questions or concerns
 We are here for you! Even if it isn't about MATLAB
Quote/Video of the Week

“English is ambiguous. If someone said, ‘The horse
flies like the devil,’ they could either be advising
me on a horse race, or merely commenting on the
rising tide of Satanism among some insects.”
- Professor Felland
Foundations of Mathematics
https://www.youtube.com/watch?v=I15bDqhkxwE
Matrix/Array Functions

A = [1 1 1; 1 1 1]
A =
1
1

1
1
1
1
B = [0 0; 0 0; 0 0]
B =

0
0
0
0
0
0
1
1
1
1
C = [1 1; 1 1]
C =
Matrix/Array Functions

A = ones(2,3)
A =
1
1

1
1
B = zeros(3,2)
B =

1
1
0
0
0
0
0
0
1
1
1
1
C = ones(2)
C =
Matrix/Array Functions

Built in Commands/Functions: See Tables 3.5,6,7
 max
 min
 mean
 median
 sum
 prod
: Maximum Value
: Minimum Value
: Mean Value
: Median Value
: Sum of Vector
: Product of Vector
Matrix/Array Functions

Sorting Functions
Table 3.8
 sort
 sortrows

Size Functions
Table 3.9
 size
Dimensions of Array
Largest Dimension
 length
Special Values / Misc.

The following have special meanings:
- The constant 3.141592 …
 i,j
- Imaginary Number
 Inf
- Infinty, or overflow
 NaN
- Not a number, Undefined (0/0)
 clock - [year month day hour minute seconds]
 date
 pi
The Colon Operator

Used for:
 Creating
Vectors
 Referencing arrays
 Future applications [loops]
Creating Vectors

A = [2 4 6 8 10 12]
A =
2

4
6
8
10
12
6
8
10
12
B = [2:2:12]
B =
2

4
C = [4:6:30]
C =
4
10
16
22
28
The Colon Operator

Let:
A =
2
-4




A(2,3) =
A(1, :) =
A(:, 3) =
A(:, 1:2:4) =
9
13
-3
1
10
6
The Colon Operator

Built-in function –
end
A =
4
5
1



7
2
9
13
6
8
A(:,end)= [13; 6; 8]
A(end,end) = 8
diag(A) = [4; 2; 8]
Matrix/Array Manipulation

You can define new arrays or matrices in terms of
other arrays or matrices.
 This
can be tricky, but always try to say it out loud and
visualize what is happening.
I/O – Input / Output

Definition:
 Hardcoding:
Setting variables equal to particular
numbers in the code.
 Example:

 The
Calculate the square root of a number.
 number = 100;
 sqrt_of_number = sqrt(number);
code snippet always calculates the square root of 100,
unless you manually change the code.
 What if we want to do it for the number the user chooses
(whomever is using your program/code)?
I/O – Input / Output

Methods:
 Ask
the user for input through the command window.
 Load data from files.
 Function inputs (We will get to this in a few weeks.)

Input Command:

number = input('Please Specify a Number: ');
I/O – Input / Output

Loading data from files:
 Many
different ways, depending on what type of file it
is. We do this in the future.
 Use
the load command.
I/O – Input / Output

Calculations, Manipulation, Calculations…
 We

Still need to display our Results
Methods:
 Display
in the command window
 Good
for quick solutions, small amount of data.
 Commands:
disp
fprintf
 Write
the results to a file.
 Great
for processing and saving lots of information.
 A bit harder to do, can be highly customized.
 Commands:
fprintf
save
I/O – Input / Output

Command:
 Example:
disp
Converts a
number to a
string.

x = 5;

disp(x);

disp(['The value of x is ' num2str(x) ‘. Cool Ehh?’]);
Tells MATLAB to combine everything
inside together into an array, in this
case, a character array
Things inside of single quotation marks are
strings, or just simply text (stored as plain text)
I/O – Input / Output

Command: fprintf
 This
can be used to either print out to the command
window or write to a file.
 This is saved for your own reading/learning.
 It’s another way to display, also. It allows for more
formatting and pretty outputs
Example Code Time

The Golf Ball Example
 Please
take note of lots of little things that are done,
they add to the readability and to the end results
being pretty
 Problem Description:
 Calculate
the X-Position and Y-Position of a golf ball hit with
an initial speed and angle. Assume constant acceleration
from gravity and no drag. Also find the maximum height
and display the results.
Homework

Please review/read:
 Chapter
3, Chapter 4
 It is very important to review the tables indicated and
go through the example problems.

Please do:
 3.4,
4.1, 4.6
Before you go…

Do Problem 4.1 in the book
Download