ece220matlab - A Very Brief Introduction to Matlab by John...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
A Very Brief Introduction to Matlab by John Walsh, for ECE 220 Fall 2004 August 23, 2004 1 The Basics You can type normal mathematical operations into MATLAB as you would in an electronic calculator. Begin by typing 1+2 [Enter] , you should see EDU>> 1+3 ans = 4 Your display may not include the EDU before the prompt. This just indicates that you are using a version other than the student version of MATLAB. Other mathematical operations are just as simple as addition. For example, here’s how to subtract two numbers EDU>> 1-2 ans = -1 Multiplication is naturally just as simple, as is handling non-integer numbers EDU>> 2.25*2 ans = 4.5000 To calculate a number to an exponent use the syntax EDU>> 2^2 ans = 4 The syntax for other binary operations implemented in MATLAB is shown in Table 1. Suppose we wish to to store the result of an operation so we can access it later. We can do this by assigning a variable name to the result of an operation, i.e. 1
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
MATLAB Expression Description x+y Add x and y. x-y Subtract y from x. x*y Multiply x and y. x/y Divide x by y. x y x to the yth power ( ) Parenthesis x= y Assign the value of y to x x == y Test to see if each elements of x and y are equal Table 1: Arithmetic Operations in MATLAB. EDU>> r=3*5; EDU>> s=2.2; EDU>> r*s ans = 33 Here, the variable names were r and s. ”Variable names consist of a letter, followed by any number of letters, digits, or underscores. MATLAB uses only the first 31 characters of a variable name. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. A and a are not the same variable. To view the matrix assigned to any variable, simply enter the variable name.” 1 Also, you should not pick variable names that are the same as the name of a function (to be discussed later). Notice we suppressed MATLAB’s output for the first two commands by including a semicolon (;) at the end of the command. Notice also that = here works differently from the usual mathematical notion of equality, instead it acts as an assignment operator. If you want to test whether or not two items are equal, you should use ==. MATLAB will return 1 if the two items are equal and 0 if they are not. EDU>> s==2.2 ans = 0 EDU>> s==33 ans = 1 2 Vectors and Matrices in MATLAB MATLAB is built to naturally work with vectors and matrices (the former are a special case of the later with one of the two dimensions having length 1). To input a column vector into MATLAB, surround the vector you wish to input with brackets, and separate its elements with semicolons: 1 Quote from MATLAB documentation. 2
Image of page 2
EDU>> v=[1 ; 2 ; 3 ; 4] v = 1 2 3 4 Row vectors are input in a similar manner, only the elements are separated by commas or spaces. EDU>> v2=[1,2,3,4] v2 = 1 2 3 4 EDU>> v2=[1 2 3 4] v2 = 1 2 3 4 Note that another way to make this same vector is EDU>> v2=[1:4] v2 = 1 2 3 4 The second way of creating v2 exploited an operator, :, in MATLAB that counts in increments of 1 from its left argument to its right argument. Increments other than 1 are also possible, one just uses a : x : b , where MATLAB will produce a row vectors whose elements count in increments of x from a to b .
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern