Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.

Similar presentations


Presentation on theme: "Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative."— Presentation transcript:

1 Basics of Matlab UC Berkeley Fall 2004, E77 http://jagger.me.berkeley.edu/~pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. http://jagger.me.berkeley.edu/~pack/e77http://creativecommons.org/licenses/by-sa/2.0/

2 Matlab Basics Starting Matlab –double-click the Matlab icon, or –in a terminal window, type matlab, and return The Command Window and the Matlab prompt >> The Desktop menu –undocking command window –tiling other windows (command history, workspace, current directory, profiler) The File menu –Preferences The Help menu

3 Matlab Basics Matlab as a calculator –Type expressions at the >>, and press return –Result is computed, and displayed as ans –Use numbers, +, *, /, -, (), sin, cos, exp, abs, round,… Precedence rules in expressions –Left-to-right within a precedence group –Precedence groups are (highest first) Highest precedence is parenthesis, then… Power (^) Multiplication and division (*, /) Addition and subtraction (+, -)

4 Examples of expressions Legal expressions >> 4 >> 5 + pi >> 6*sqrt(2)^4-12 >> 6 * sqrt( 2.0) ^ 4 - 12 >> sin(pi/3)^2 + cos(pi/3)^2 >> 1.0/0.0 >> -4/inf >> 0/0 Illegal expressions >> 2 4 >> (2,4) Error messages –Read them carefully – a large portion of the time you will quickly figure out what is wrong

5 Variables Use names to assign result of an expression to a variable –Variables do not need to be declared before assignment A single “equal” sign (=) is the assignment operator, LHS = RHS Read this as –evaluate expression on the right-hand side, and then… – assign the result to the variable named on the left-hand-side Therefore –The right-hand-side needs to be a legal Matlab expression –The left-hand-side needs to be a single variable name (this will get more sophisticated later on) A semicolon at the end of the RHS expression suppresses the display, but the assignment still takes place.

6 Examples of Variables and Assignment Legal >> A = sqrt(13) >> B = exp(2); >> A = 2*B >> A = A + 1 >> C = tan(pi/4) Illegal (all for different reasons) >> D = sqrt(E) + 1; >> 3 = E >> 3*A = 14 >> F = 2 3

7 The “workspace” All variables that you create are accessible from the prompt >> Variables are accessed using their name as a reference Builtin Matlab commands >> who >> whos are used to see what is in the workspace. You can clear (ie, erase) variables with the clear command >> clear A clears the variable A from the workspace. How do we check that it worked?

8 Comparison (as opposed to assignment) Compare the equality of two expressions with a double equal sign, == Several arithmetic comparisons are available. Can also compare –greater than (with >) –less than (with <) –greater than or equal (with >=) –less than or equal (with <=), and –not equal (with ~=) The comparison is itself an expression –Its value is either 1 (true) or 0 (false), and can be used in assignment >> 5==sqrt(25) >> E = 1.72>tan(pi/3) What do you need to know to determine the result of >> E = 4>5-2 Precedence group is lower than addition and subtraction, so…

9 Quiting Matlab Type quit at the prompt, or Select Exit Matlab from the File menu However…

10 Saving the workspace When you “quit” Matlab, the variables in the workspace are erased from memory. If you need them for later use, you must save them. You can save all variables (or just some of them) to a file using the command save >> save saves all of the variables in the workspace into a file called matlab.mat (it is saved in the current directory) >> save Andy saves all of the variables in the workspace into a file called Andy.mat >> save Important A B C D* saves the variables A, B, C and any variable beginning with D in the workspace into a file called Important.mat

11 Loading from a.mat file load is the opposite of save. It reads a.mat file, putting all variables contained in the.mat file into the workspace >> load loads all of the variables from the file matlab.mat >> load Andy loads all of the variables from the file andy.mat There are no known security problems with load. Hence, you can safely send (as attachment), receive and use.mat files from others.

12 Complex Numbers All arithmetic in Matlab works on complex numbers as well. When you start Matlab, two variables already exist, and are equal to. They are i and j. It’s common to overwrite them without even realizing, but you can always create the number with the expression sqrt(-1) >> i >> sqrt(-1) >> j >> 4 + 6j >> 4 + 6*j >> C = 1 – 2i; >> real(C) >> imag(C) >> abs(C) >> angle(C)*180/pi


Download ppt "Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative."

Similar presentations


Ads by Google