Foundation and Fundamental Concepts
Entering Commands and Expressions •
The prompt >> is displayed in the Command Window and when the Command Window is active, a blinking cursor should appear to the right of the prompt.
•
This cursor and the MATLAB prompt signify that MATLAB is waiting to perform a mathematical operation.
Commands for Managing a Session clear
- Removes all the variables from the workspace. - Frees up system memory
who
- Displays the list of variables currently in the memory.
whos
- Will display more details which include size, space, allocation and class of variables
exist
- Checks for existence of the variable.
global
- Declares variable to be global.
help
- Searches for help topic
lookfor
- Searches help entries for keyw keyword ord
quit
- Stops the MATLAB
Help Features in MATLAB helpbrowser
Opens the help window
help function_name
Displays Displays the help document in the command window
Displays Displays the help document in helpwin function_name separate window doc function_name
Displays Displays detailed help document in separate window
Numeric Display Formats f ormat ormat
- Controls the display output of the command window
C o m m o n F o r m a t Fu Fu n c t i o n s
short
- Four decimal digits
long
- Sixteen decimal digits
short e
- Five decimal digits plus exponent
long e
- Sixteen digits plus exponent
bank
- Two decimal digits
Mathematical Functions Special Variables and Constants ans
- Most recent answer
i, j
- The imaginary unit
Inf
- Infinity
NaN
- Undefined numerical result (not a number)
pi
- The number
Mathematical Functions Elementary Functions cos(x)
Cosine
abs(x)
Absolute value
sin(x)
Sine
ceil(x)
Round towards + Inf
tan(x)
Tangent
floor(x)
Round towards - Inf
acos(x)
Arc cosine
round(x)
Round
asin(x)
Arc sine
rem(x)
Remainder after division
atan(x)
Arc tangent
angle(x)
Phase
exp(x)
Exponential
conj(x)
Complex Conjugate
sqrt(x)
Square root
imag (x)
imaginary
log(x)
Natural Logarithm
real (x)
Real number
log10(x)
Common Logarithm
primes (x)
Prime Number
Mathematical Functions Scalar Arithmetic Symbol
Operation
+
Addition
-
Subtraction
*
Multiplication
/
Right Division
\
Left Division
^
Exponentiation
Mathematical Functions Exponential and Logarithmic Functions exponential
exp (x)
Natural Logarithm
ln (x)
Common Logarithm
log10 (x) = [ log10 (x) ]
Square root
sqrt (x)
Example1: 2 + 3 – 4 In Command Window >>2 + 3 – 4 ans = 1 Example2: 6÷3 In Command Window >> 6 / 3 ans = 2
Example3: 3÷6 In Command Window >> 6 \ 3 ans = 0.5 •
MATLAB has assigned the answer to a variable called ans, which is an abbreviation for answer. A variable in MATLAB is a symbol used to contain a value.
•
MATLAB does not care about spaces for the most part. Spaces in the line improve its readability.
•
When you want to calculate a more complex expression use parentheses parentheses,, in the usual way, to indicate precedence.
•
The mathematical operations represented by the symbols + – * / \, and ^ follow a set of rules called precedence precedence.. ..
•
Mathematical expressions are evaluated starting from the left, with the exponentiation operation having the highest order of precedence, followed by multiplication and division with equal precedence, followed by addition and subtraction with equal precedence.
•
Parentheses can be used to alter this order. Evaluation begins with the innermost pair of parentheses, and proceeds outward.
•
To avoid mistakes, you should feel free to insert parentheses wherever you are unsure of the effect precedence will have on the calculation.
Example4: 4 3 23 14.7 6 3.5 In Command Window
>> (3*(23 + 14.7 – (4 / 6))) / 3.5
Naming constants and variables •
MATLAB allows us variables names of powerful facility that help in avoiding input
to give constants and our choice. This is a can reduce work and errors.
•
When the user begins a session in which the same values must be used several times, the user can define them once and then call them by name.
Example5:
Given: a = 2, A = 3 Find: a. 2a b. w=3A In Command Window
>>a=2 a= 2 >>A=3;
•
•
When you write the semicolon ‘ ; ‘ at the end of a statement, the computer will not display the result of the command, and it will not echo the input. >>2*a ans = 4 >>w=3*A w= 9 MATLAB does not tell you the value of all the variables; it merely gives you their names. To find their values, you must enter their names at the MATLAB prompt.
>>a = 4 a= 4 >>2*a ans = 8 •
If you reuse a variable in the preceding example, or assign a value to one of the special variables, its prior value is overwritten and lost. However, any other expressions computed using the prior value do not change.
Example6:
Given:
= 30, = 52, = 76 Find: a. Sin b. Sin c. Sin In Command Window
>> alpha = 30; >> beta = 52; >> gamma = 76;
>> sin (alpha) ans = –0.9880 parentheses theses is used after the • A pair of paren function’s name to enclose the value – called the function’s argument – that is operated on by the function. >> sin (beta) ans = 0.9866 >> sin (gamma) ans = 0.5661
•
MATLAB remembered past information.
•
To recall previous commands, MATLAB uses the cursor keys, , , , , on your keyboard.
•
In addition, all text after a percent sign (%) is taken as a comment statement.
Example of Formating:
or pi In Command Window
>> pi ans = 3.1416 •
MATLAB uses high precision for its computations, but by default it usually displays its results using four decimal places. This is called the short format format.. Using the format command can change this default.
>> format long >> pi ans = 3.14159265358979 •
MATLAB uses the notation e to represent exponentiation to a power of 10. >> format short e >> pi ans = 3.1416e+000
>> format long e >> pi ans = 3.14159265358979e+000 >> format bank >> pi ans = 3.14 To return to default format >> format >> pi ans = 3.1416
•
Most interesting is the format rat: rat: it yields a rational approximation of a real number, that is a fraction that approximates a given number. >> format rat >>pi ans = 355/113 >>format >>355/113 ans = 3.1416
Practice Set:
1. Pe Perf rfor orm m th the e in indi dica cate ted d op oper erat atio ion: n: a. 2{ – 4 – [6 + 3 + (7 –(1 + 8)) + 12] – 3} + 5 b. –5{ – [ – 2 + 6 – 8(4 – (7 – 4) – 2) + 2] – 1} c. d.
− − − −+ −−8 − −
÷
8− − −
e. Given: =
Determine: i. y = sin x ii. when y = 1, what is z = sin -1 y
0.
g. h. cos 2
2. Suppose that x = 3 and y = 4. Use MATLAB to compute the following. a. 1 b. 3 c. d.
−8 − −
−
3. Assuming that the variables a, b, c, d and f are scalars, write MATLAB statements to compute and display the following expressions. Test your statements for the values a = 1.12, b = 2.34, c = 0.72, d = 0.81, f = 19.83 a. = 1 b. =
c. =
− − + + +
d. =
4.
Evaluate the following expressions MATLAB, for the values x = 5 + 8i, y = – 6 + 7i. a. u = x + y b. v = xy c. w = x / y d. z = ex e. = f. s = xy2
in