Unformatted text preview:

The CS 100J MATLAB DescriptionThis handout outlines (through examples) the CS 100J "subset" of MATLAB. The MATLAB facility should be used for details. The book by Rudra Pratap is an excellent and inexpensive reference. helpGetting Started With MatlabOne-dimensional arrays are called and two-dimensional arrays are called Scalars are 1-by-1 matrices. The memory manager takes care of the necessary storage allocations. There are no declarations. The entries in a Matlab array may be real or complex. vectors matrices.When in the , you are prompted to enter commands with a double arrow ">>" command window Setting Up ArraysUse to learn more about ,, , , , and .help linspacelogspace ones zerosrand randn>> x = [ 10 20 30 ]x = 10 20 30>> x = [10; 20; 30]x =102030>> x = [10 20 30;40 50 60]x =10 20 3040 50 60 >> x = linspace(1,3,5)x = 1 1.5 2.0 2.5 3.0>> u = [1 2]; v = [3 4];>> x = [u v u]x =1 2 3 4 1 2>> x = [u;v;u]x =1 23 41 2>> x = rand(2,3)x = .293029 .343293 .292930 .411275Square brackets delimit arrays. Row vector entries are separated by blanks. For column vector, entries are separated by semicolons. Matrix rows are separated by semicolons.: create a length-n row vector of equally spaced values from a to b inclusive.linspace(a,b,n)Row vectors can be catenated to build longer row vectors. Blanks in between the vectors entries.Column vectors can be "stacked" to build longer column vectors. Semicolons in between the vector entries. generate an n-row, m-column matrix of random numbers, each selected from the uniform(0,1) distribution.rand(n,m):Some Built-In FunctionsUse to learn more about , , , , , , , , and .. Elementary functions like , , , , , , , , , , , , , , , , and accept array arguments and return the corresponding array of function evaluations. Try for a synopsis of Matlab's elementary functions. help length size max minsum cumsum prod cumprod sort medianabs sqrt exp log log10 sincos tan asin acos atan floor fix ceilround rem real, imaghelp elfun>> x = [30 10 80 20 60];>> n = length(x)n = 5 >> [z,idx] = max(x)z = 80idx = 3>> z = sort(x)z = 10 20 30 60 80>> x = [1 2 3; 4 5 6]x = 1 2 3 4 5 6>> [m n] = size(x)m = 2n = 3 The length of a vector equals the number of components. Use max to find the largest (i.e. most positive) entry in a vector. Two values are returned. The size of the largest value and the index that identifies its location. Works for row or column vectors.Sort the entries in a vector from smallest to largest (i.e. from most negative to most positive.)The size of a two-dimensional array is defined by the number of its rows and the number of its columns.Array-Level OperationsArray operations for column vectors and matrices are similarly defined. Learn more about array-level operations by typing .help opsx = [5 8 -2];>> y = [1 2 3];>> z = x+yz =6 10 1>> >> z = 3*xz =15 24 -6>> z = x+7z =12 15 5>> z = x.*yz =15 16 -6>> z = x./yz =5 4 -.66667>> z = x.^yz =5 64 -8>> z = x.^2z = 25 64 4>> z = [1 2 3]'z =123 You can add vectors as long as they have the same length and orientation. This is how you multiply every entry in an array by the same scalar. This is a shortcut for ]. z = x + [7 7 7 Componentwise multiplication. Again, the vectors involved must have the same length and orientation. Componentwise division and exponentiation. A shortcut for z = x.^[2 2 2]. You can change the orientation of an array this way. Row-vectors become column-vectors and vice versa.Numerical Input/OutputUse to learn more about , , , , and .help format dispsprintf input num2str>> x = pix = 3.1416>> format long>> x = pix = 3.14159265358979x= input(`Enter x:');y= sqrt(x);s= sprintf(`sqrt = %10.6f',y);disp(s) The effect of a command is displayed if there is no semicolon after the command. The command is used to establish the current display style. The format (5 decimal places) is in force when Matlab is first entered. The format displays full working precision. Scientificnotation is available via the and formats.format shortlongshort e long eTo solicit scalar input with a prompt, use .input displays strings and can be used to produce formatted output strings. specifies a decimal format with 6 decimal places and total space allocation of 10 characters. Use format for scientific notation and d for integers.Disp sprintf%10.6feAs You Learn More Mathematics...Matlab supports matrix-vector multiplication, linear equation solving, data fitting, differential equation solving, nonlinear equation solving, numerical integration, and many other important mathematicalactivities.Script FilesUse to learn more about , , , , , and .help script echo type cddir pathIf a sequence of Matlab commands is stored in a file , that sequence is invoked whenever the name of the file is entered. Thus, if the file Expo.m has the commands name.mx = input(`Enter the exponent x:')y = 2^xthen by typing you will be prompted for a value of and is assigned to and displayed.Expo x 2^x yFunction FilesType for more details.help functionNew functions may be added to Matlab's vocabulary if they are expressed in terms of other existing functions. The commands that define the new function must be put in a separate file. The name of file must be the name of the function with a " suffix. The first line in the file must be a valid function line that specifies the input and output parameters. Consider a file called with the following lines:".mstat.mfunction [mean,stdev] = stat(x)% Yields the mean and standard deviation of a vector x n= length(x); mean= sum(x)/n; stdev= sqrt(sum(x-mean).^2)/n);This defines a new function called " " that calculates the mean and standard deviation of a vector. The variables within the body of the function are all local variables. Comments in Matlab begin with a "%".statControl StructuresTry for a review of Matlab as a programming language including the , , and constructs. The usual symbols are used for booleans: " " (greater than), " " (at least), " " (equal), " " (atmost), " " (less than), and " " (not equal). The "and", "or", "not" operations are written as "&" , "| ", "~". See alsoand .help langfor while if> >=== <=< ~=anyalld= sqrt(b^2 -4*a*c;if d>0 r1= (-b+d)/(2*a); r2= (-b-d)/(2*a);else disp(`Complex roots')endif (x>y) & (x>z) maxval= x;elseif y>z maxval= y;else maxval= z;endif (x < 4) | (x > 5) disp(`x not in [4,5]') keyword A simple if-else construct.


View Full Document

CORNELL CS 100 - MATLAB Description

Download MATLAB Description
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view MATLAB Description and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view MATLAB Description 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?