Unformatted text preview:

Intro To MatlabMatlab (short for Matrix-Laboratory) was originally designed as a front endfor numerical linear algebra routines (numerically finding eigenvalues andeigenvectors, etc.). Now it is an industry standard for fast development ofnumerical solutions to mathematics.Matlab is different than Maple: Maple is known as a spe cific example of acomputer algebra system, which means that Maple was designed to work sym-bolically (e.g., factor a polynomial, compute derivatives and antiderivativessymbolically, etc.). On the other hand, Matlab was developed for numericalwork. In fact, there are ways of calling Matlab from inside Maple, and viceversa (although we won’t be doing that).If you have had a class in computer programming, then Matlab will lookvery familiar. There are three ways of running Matlab, and we will considereach:• Run Matlab “live”, by typing in commands.• Run Matlab by running a script file. A script file is just a text file withthe Matlab commands typed in.• Run Matlab by calling programs that you have written. We’ll see howto do this in just a moment.Starting the Matlab ProgramFirst you need an account in the Mathematics computer lab- See your in-structor if you need an account of if you’ve forgotten your password.Once you have logged in, open a shell window and type matlab at theprompt. The splashscreen will come up, and you’ll get a command windowthat looks like:< M A T L A B >Copyright 1984-2001 The MathWorks, Inc.Version 6.1.0.450 Release 12.1May 18 2001To get started, select "MATLAB Help" from the Help menu.>>To exit from Matlab, either type exit from the command window, orchoose Exit from the window menu (under File).Saving your workTypically, homework and larger projects should be done by writing a scriptrather than typing in commands live. It makes it e asy to change and debug,rather than typing all your work over again!1Matlab has a very nice text editor that you can use to type out and saveMatlab functions and scripts- To access the editor, type edit in the Matlabcommand window.All of the Matlab files that we write will end in a *.m suffix (like Example01.m,etc.). Before we go much further, let’s open up Matlab and try some com-mands live:Introductory Commands1. ArithmeticMatlab understands all of the basic arithmetic functions, +, -, *, /, ^are addition, subtraction, multiplication, division and exponentiation.Type them in just as you would write them. For example, 25would betyped as 2^5.2. Trigonometric FunctionsMatlab understands the basic trig functions sine, cosine and tangentas sin , cos , tan . So, for example, the sine of 3.1 would be typedas: sin(3.1)The number π is used so frequently that Matlab has its (approximate)value built-in as the constant pi. For e xample, sin(π) is typed assin(pi). Note that π uses a lowercase “P”.3. Exponential and Logarithmic FunctionsMatlab does not have the number e built-in as a constant (like π). Totake the number e to a power, use the functional form: ex= exp(x)So if I want the number e, I would type exp(1), and so on.For the natural log (log base e), use the notation log. For example,ln(3) is written as log(3). We will only use the natural log- if in thefuture you want a different base, look up the log command by typinghelp log.4. Complex Numbers and ArithmeticMatlab has complex arithmetic built-in. Either the letter i or j canbe used to represent√−1, but a word of caution is in order here:You can only use i or j for√−1 ONLY if you have not previouslydefined them. If you think you’re going to use complex numbers, donot use the letter i for anything but complex arithmetic! Example:(0.2+3*i)*(5+2*i) will multiply the two complex numbers together(using complex arithmetic).2Helpful Administrative CommandsThe following commands are useful as you begin to use Matlab more andmore:who List all variables currently in use.whos List all variables, and their sizes.ls or dir List the contents of the current directory.cd Change the directory. For e xample, cd examples would change thecurrent directory to your file named examples. To go up the structureinstead of down, type cd ..pwd Tell me where in the directory structure I’m currently at.where command Tell me where command.m is located.help command List the help file for the function command. For exam-ple, to get help on the sine function, type help sin.demo Lists all the demonstration programs that Matlab came with- Thisis fun to look at. We don’t have all of them; you can go to Matlab’swebsite to look at more: www.mathworks.com.A Programming Note: Assignment v. EqualityIn computer programming, the equal sign does not mean mathematical equal-ity. We use the equal sign as an assignment operator. For example,A=3;means to assign the value of 3 to the variable A- If A has not been assignedto anything before, this command will also create that variable.Definition: In computer programming, A = B means that we will assignthe value of B to the variable A.Using this, what will the following commands do?x=5;x=x+3;Answer: First, the value 5 is assigned to the variable x. Next, x + 3 iscomputed as 8, and finally, the value of 8 is assigned to the variable x.Not an example: 5=x;, you would get an error- the number 5 is not avariable.The assignment operator is also used to label the output of a function.For example, the following commands stores a vector [1, 2, 3] into the variablex, then we apply the sine function to each of those numbers and store themin y to get [sin(1), sin(2), sin(3)]:3x=[1,2,3];y=sin(x);MatricesMatlab was originally designed as a “front end” to access LINPACK and EIS-PACK, which are numerical linear algebra packages written in FORTRAN.From this beginning, Matlab’s basic data type is the matrix.I enter the following matrix:A =1 2 3 45 6 7 8as:A=[1 2 3 4; 5 6 7 8];or as:A=[1 2 3 45 6 7 8];Note the use of the semicolon: Inside a matrix, the semicolon indicates theend of a row. Outside the matrix, the semicolon suppresses Matlab output.You can also separate numbers using a comma if you’d prefer that. Rowsand columns are entered in a corresponding way, as either a 1 ×n matrix oras a n × 1 matrix.We access elements of the matrix in a natural way. For example, the(2, 3) element of A is written as A(2,3) in Matlab (in this case, A(2, 3) is 7).You can change the elements using the assignment operator =. For example,if we want to change the (1, 3) element of A from 3 to 6, type:A(1,3)=6;Special Commands: The


View Full Document

Whitman MATH 350 - Lecture Notes

Download Lecture Notes
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 Lecture Notes 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 Lecture Notes 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?