Upload
maud-ward
View
229
Download
4
Tags:
Embed Size (px)
Citation preview
MATLAB FUNDAMENTALS:MATRIX/ARRAY FUNCTIONS THE COLONMATRIX/ARRAY MANIPULATIONINPUT/OUTPUT
HP 100 – MATLABWednesday, 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]
B = [0 0; 0 0; 0 0]
C = [1 1; 1 1]
A =A = 1 1 11 1 1 1 1 11 1 1
B =B = 0 00 0 0 00 0 0 00 0
C =C = 1 1 1 1 1 1 1 1
Matrix/Array Functions
A = ones(2,3)
B = zeros(3,2)
C = ones(2)
A =A = 1 1 11 1 1 1 1 11 1 1
B =B = 0 00 0 0 00 0 0 00 0
C =C = 1 1 1 1 1 1 1 1
Matrix/Array Functions
Built in Commands/Functions: See Tables 3.5,6,7 max : Maximum Value min : Minimum Value mean : Mean Value median : Median Value sum : Sum of Vector prod : Product of Vector
Matrix/Array Functions
Sorting Functions Table 3.8 sort sortrows
Size Functions Table 3.9 size Dimensions of Array length Largest Dimension
Special Values / Misc.
The following have special meanings: pi - 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
The Colon Operator
Used for: Creating Vectors Referencing arrays Future applications [loops]
Creating Vectors
A = [2 4 6 8 10 12]
B = [2:2:12]
C = [4:6:30]
A =A = 2 4 6 8 10 122 4 6 8 10 12
B =B = 2 4 6 8 10 122 4 6 8 10 12
C =C = 4 10 16 22 284 10 16 22 28
The Colon Operator
Let:
A(2,3) = A(1, :) = A(:, 3) = A(:, 1:2:4) =
A =A = 2 9 -3 102 9 -3 10 -4 13 1 6 -4 13 1 6
The Colon Operator
Built-in function – end
A(:,end)= [13; 6; 8] A(end,end) = 8 diag(A) = [4; 2; 8]
A =A = 4 4 7 7 1313 5 5 22 66 1 9 1 9 88
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:
Calculate the square root of a number. number = 100; sqrt_of_number = sqrt(number);
The 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: disp Example:
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)
Converts a number to a string.
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