Unformatted text preview: CS1050004
Day 3, (Where do we go from here?) Review Thursday’s Stuff This is what we tried to do on Thursday last week New Stuff Extracting values from a Matrix A value from inside a matrix is specified with the matrix name, and then the value location in side parentheses A(1,2) Create a smaller vector by extracting it from a larger vector A = [1 2 3 4; 5 6 7 8; 3 4 5 6]; B=A(1,:) how to make a vector of just one column from A? Are we having fun yet? Transpose
The transpose operator basically changes rows to columns or vice versa
D’ Can be used to create tables
table = [D’,R’] Scientific Notation Large and small numbers expressed in scientific notation, with a value as a number between 1 and 10 (the mantissa), multiplied by a power of 10 (the exponent). An e is placed between the mantissa and exponent, without any white spaces MATLAB will interpret this as two different numbers 6.2e24 not 6.2 e24 Display Format Integers printed as integers Decimal fraction default to 4 decimal digits To get 14 decimal digits, use: A=5 A=5.1000 format long To return to 4 decimal digits Changing the display format does not change the accuracy of your calculations Large and small numbers automatically displayed in SN More in the Textbook. See Table 2.3 for commands and examples A=5.10000000000000 format short Saving Our Work save myspace
This will save the variables in the workspace load myspace window in the current directory as a .mat file myspace is user defined
This will restore a workspace I will demonstrate these commands save myspace variable_list Script Mfiles MATLAB Mfile Editor/ Debugger
Text editor built into MATLAB You can use a different text editor, just make sure you save the files as ASCII format. Such editors are:
Notepad Word WordPerfect Mfiles, part II Same naming rules as variables, what are they? Two type of Mfiles Script list of MATLAB commands saved in a file, .m extension Executed by Save and Run Icon, Debug pull down menu then Run, or by hitting F5. Can from the command window by: Type the file name run myscript run(‘myscript’) Mfiles are our friend Comments, lots and lots of comments % This is how to comment Let’s make a Decision We can either continue with MATLAB. This is how Everybody get a copy of the Student Version of MATALB Those who can’t afford it can go to the Redwood Campus to do homework We Lecture for two hours on Tuesday and meet at Redwood Lab SI075 for Labs on Thursday Option 2 Change the syllabus
We could stop the MATLAB and start C++ This would require a 2 Final Exam during Finals weeks rather than during Midterms More work for me, I would have to alter homework that include both C++ and MATLAB, (which the C++ Final Project is one) Answer To Questions that I had to look up, sorry. To Clear the Command History Window Edit> Clear Command History .* is not needed for a scalar operation, it is require for multiplying two matrices of equal size, element by element Chapter 3 Predefined MATLAB Functins What we will learn from Chapter 3
Using Predefined Functions Manipulating Matrices Computational Limitations Special Values and Functions Using Predefined Functions Arithmetic operations usually require computations other than the basics, add, subtract, multiply, divide, exponentials. We need things like logarithms, exponentials and trigonometric functions. MATLAB Functions Example of a builtinfunctions X=9; Y=sqrt(X) Ans= 3 sqrt(number) Functions, What are they? All MATLAB Functions can be thought of as having three components:
A name An input, also called an argument
This goes inside the parentheses Rem(10,3) Some require more than one input value Examples: sqrt, sin, tan, cos, rand An Output Calculated value or values More Functions size(d) Return two outputs, the number of rows and columns in a matrix Output can be assigned to varaibles [x,y]=side(d) You can nest functions, aka composition of functions g=sqrt(sin(x)) Each function should be enclosed in its own parentheses HELP, I’m so confused Help is available to those of us that can’t 3 ways to get help: help remember the proper syntax for functions
Commandline help function help topic helpwin Separate Windowed Help function HTMLbased document set Select Help > MATLAB Help Elementary Math Functions abs(x) sqrt(x) round(x) fix(x) floor(x) ceil(x) More examples sign(x) rem(x) exp(x) log(x) log10(x) not e^x Trigonometric Functions Trigonometric Functions in MATLAB to convert to Radians expect all arguments to be in Radians
angle_degrees=angle_radian*(180/pi) See the help files to see a complete list of Trigonometric functions available in MATLAB Data Analysis Functions Maximum
max(x)
Returns the largest value in a vector Return the largest value in each column of a matrix Assigns ‘a’ the largest value in a vector and ‘b’ its [a,b]=max(x) location Assign ‘a’ as a vector with the largest value in each column and ‘b’ as a vector with the column it’s located in Last of the max()
max(x,y)
x and y must be of the same size This function creates a matrix of the largest elements of each matrix for each location in the matrix. We do a sample Minimum min(x)
Returns the smallest value in a vector Returns the smallest value in each column in [a,b]=min(x) a matrix Assigns ‘a’ the smallest value in a vector and ‘b’ its location Assign ‘a’ as a vector with the smallest value in each column and ‘b’ as a vector with the column it’s located in Minimum Done
min(x,y)
x and y must be of the same size This function creates a matrix of the smallest elements of each matrix for each location in the matrix. We do a sample here, too Mean, but not too mean Mean is the average of a group of numbers mean(x) Returns a single value for a vector argument Returns a vector containing the mean value from each column of a matrix Median The center number of a group of numbers when arranged in numerical order If an odd number of values, the median is the center number If an even number of values, the median is the mean of the center two values. Arguments do not need to be input in numerical order. Median Rare median(x)
Returns a scalar value for a vector Returns a row vector of the median from each column Sums and Products sum(x)
Computes the sum of all elements in a vector prod(x) input Returns a rowvector containing the sum of each column in a matrix input Returns the product of all elements in a vector Returns a row vector containing the product of each column Cumulative Adding and Multiplying cumsum(x)
Returns a vector of the same size containing cumprod(x) a cumulative sum, from left to right, of a vector Returns a matrix of the same size containing a cumulative sum of the columns, from top to bottom, of a matrix
Same as cumsum, only we are multiplying Sorting sort(x)
Will sort a vector elements in ascending order Will sort the elements in each column of a matrix into ascending order. How Big am I? size(x)
discussed early today, on slide #19 length(x) Determines the largest dimension of a matrix Variance and Standard Deviation Two of the most important statistical measurements of a set of data. These help us to understand how the data points fluctuate from the mean value.
The variance is the average squared deviation of the data from the mean The standard deviation is defined as the square root of the variance More on µ & σ
68% of the data falls within one sigma variation of the mean, and 95% falls within two sigma. 99% of the data should fall within three sigma. MATLAB does not have a builtinfunction for the variance Standard Deviation std(x)
Returns a value for a vector argument Returns a vector containing sigma for each column of the matrix Random Numbers Section 3.1.5 covers random numbers for when engineers need to generate random numbers for development of a solution. Please see the textbook for details on how to generate random numbers. Manipulating Matrices Too much to enter on one line, use a ellipsis f=[1,43,43,45,75,2,4,… 56,56,43,2]; Can use a matrix to defien a matrix
B=[1,2,5] c=[1,2,B] I’m getting tired You can reassign elements in a matrix by referencing a specific location
w(2)=4; Can extend a matrix in the same fashion
q=[1,2,3]; q(4)=6; q=1 2 3 6 Making something out of nothing Can assign to any location in a matrix, if it’s not the last element, MATLAB will add zeroes
a=[1,2,3] a(6)=8; a=1 2 3 0 0 8 Colon Operator Create evenly spaced matrices
a=1:6 a = 1 2 3 4 5 6 a=1:.5:3 Create a specified spacing
a = 1.0000 1.5000 2.0000 2.5000 3.0000 Getting the Whole Thing Use the colon to get the entire row or column x = m(:,1) x = m(:,4) x = m(2,:) Getting just part of it Colon operator can be used to get only part of a matrix
a=m(2:3,:) a=m(2:3,4:5) Empty Matrix
a= I don’t know about you, but I’m tired Computational Limitations Maximum and minimum values for MATLAB
10^308 10^308 Still can’t divide by zero Special Values and Functions These scalar constants do not need an argument to be input to be used pi i,j Inf Nan clock date eps ans π imaginary numbers Infinity Not a number current time current date epsilon floatingpoint precision value computed by expression and not assigned to a variable Final Slide These can be reassign new values, but why would you? If you assign them new values, you can clear them by clearing the workspace WAKE UP!! It’s over! ...
View
Full
Document
This note was uploaded on 02/27/2011 for the course CS 1050 taught by Professor Eric during the Spring '06 term at Salt Lake Community College.
 Spring '06
 Eric

Click to edit the document details