MATLAB
MATRIX stands for MATrix LABoratory
The aim of education is to teach students how
Introduction
• MATLAB is a mathematical and graphical
software package; it has numerical, graphical, and programming capabilities. It has built-in functions to do many operations, and there are toolboxes that can be added to augment these functions (e.g., for signal processing). There are versions available for different
• There are several commands that can serve as an introduction to MATLAB and allow you to get help: • info will display contact information for the
product;
• demo has demos of several options in MATLAB; • help will explain any command;
Starting MATLAB
• You can enter MATLAB by double-clicking on the MATLAB shortcut icon on your Windows desktop. When you start
MATLAB, a special window called the MATLAB desktop appears. The desktop is a window that contains other windows. The
major tools within or accessible from the desktop are: • The Command Window
• The Command History • The Workspace
• The Current Directory • The Help Browser
Quitting MATLAB
• To end your MATLAB session, type quit at the
prompt in the Command Window, or select File → Exit MATLAB in the desktop main
Uses of MATLAB
• MATLAB is widely used as a computational tool in science
and engineering encompassing the fields of physics,
chemistry, math and all engineering streams. It is used in a range of applications including:
• signal processing and communications • image and video processing
• control systems
Creating variables in MATLAB
• The name must begin with a letter of the alphabet. After that, the name can contain letters, digits, and the underscore character (e.g., value_1), but it cannot have a space.
• There is a limit to the length of the name; the built-in function
namelengthmax tells how many characters this is.
• MATLAB is case-sensitive. That means that there is a difference between upper- and lowercase letters. So, variables called mynum, MYNUM, and
Mynum are all different.
• There are certain words called reserved words that cannot be used as variable names.
• MATLAB is case-sensitive. That means that
there is a difference between upper- and
lowercase letters. So, variables called mynum, MYNUM, and Mynum are all different.
• There are also certain words called reserved
Reserved or keywords of MATLAB
• The reserved words, called keywords, are:
• Additionally, variable names should always be
mnemonic, which means they should make some sense. For example, if the variable is storing the radius of a circle, a name such as “radius” would make sense as against a
Variables and assignment statements
• One easy way to create a variable is to use an assignment statement. The format of an assignment statement is
• variablename = expression
• The variable is always on the left, followed by the assignment
operator, = (unlike in mathematics, the single equal sign does not
mean equality), followed by an expression. The expression is evaluated and then that value is stored in the variable.
• For example, this is the way it would appear in the Command Window: • >> mynum = 6
• mynum = • 6
• Since the equal sign is the assignment operator, and does not mean equality, the statement should be read as “mynum gets the value of 6” (not “mynum equals 6”).
• Note that the variable name must always be on the left, and the expression on the right. An error will occur if these are reversed. • >> 6 = mynum
• ??? 6 = mynum • |
• Error: The expression to the left of the equals sign is not • a valid target for an assignment.
Assignment
• In MATLAB, variables must always have values
before they are used.
• To create a variable in MATLAB, you declare it and
assign an initial value to it. This initial value could be 0, 1, 1000, or whatever value it is supposed to be.
• Examples: • >> x = 2;
Examples of expressions
• rho = (1+sqrt(5))/2 • rho = 1.6180
Commands that relate to variables
• The following commands relate to variables:
• who shows variables that have been defined in this Command
Window
• (this just shows the names of the variables)
• whos shows variables that have been defined in this
Command Window (this shows more information on the variables, similar to what is in the Workspace Window)
• clear clears out all variables so they no longer exist • clear variablename clears out a particular variable
• If nothing appears when who or whos is entered, that means
Operators
• Operators
• There are in general two kinds of operators: unary operators, which
operate on a single value or operand; and binary operators, which operate on two values or operands. The symbol “–”, for example, is both the unary operator for negation and the binary operator for subtraction.
• Here are some of the common operators that can be used with numeric expressions:
• + addition
• – negation, subtraction
• * multiplication
• / division (divided by e.g. 10/5 is 2) • \ division (divided into e.g. 5\10 is 2)
Initializing, Incrementing, and Decrementing
• Frequently, values of variables change. Putting the
first or initial value in a variable is called initializing
the variable.
• Adding to a variable is called incrementing. For
example, the statement
• mynum = mynum + 1
increments the variable mynum by 1. Likewise,
Operator precedence
• The order from highest to lowest are: • () parentheses
• ^ exponentiation • – negation
MATLAB special variables
• pi Value of π
• eps Smallest incremental number • inf Infinity
• NaN Not a number e.g. 0/0 • i and j, i = j = square root of -1
MATLAB relational operators
• MATLAB supports six relational operators. • Less Than <
• Less Than or Equal to <= • Greater Than >
• Greater Than or Equal to >= • Equal To ==
• Not Equal To ~= (NOT; equivalent to != like in
MATLAB logical operators
• MATLAB supports three logical operators. • not ~ % highest precedence
• When assignments are long, we can extend to
another line by using an ellipsis (…). For example,
• initial_velocity = 0; • acceleration = 9.8; • time = 20;
• Complex Numbers
• MATLAB can work with complex numbers as easily as with real numbers. For example, to find the roots of the quadratic
polynomial x2 + 2x + 5 enter
• >> a = 1; b = 2; c = 5;
• >> x1 = ( -b + sqrt( b^2 - 4*a*c ) ) / (2*a) • >> x2 = ( -b - sqrt( b^2 - 4*a*c ) ) / (2*a) • The output is
• -1.0000 + 2.0000i • and
• There are standard functions for obtaining the real
part, the imaginary part, and the complex
conjugate of a complex number or variable. For example,
• >> x = 3 - 5i • >> real(x) • >> imag(x) • >> conj(x)
• 1. Consider a triangle with sides a, b, and c and corresponding
angles ab, ac, and bc.
• (a) Use the law of cosines, i.e., • c2 = a2 + b2 - 2ab cos ab;
• to calculate c if a = 3.7, b = 5.7, and ab = 79⁰. • (b) Then show c to its full accuracy.
• (c) Use the law of sines, i.e., •
• = , •