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 who whos exist global - Removes all the variables from the workspace. - Frees up system memory
help lookfor quit
- Displays the list of variables currently in the memory. - Will display more details which include size, space, allocation and class of variables - Checks for existence of the variable. - Declares variable to be global. - Searches for help topic - Searches help entries for keyword - Stops the MATLAB
Help Features in MATLAB
helpbrowser Opens the help window
Displays the help document in the help function_name command window Displays the help document in helpwin function_name separate window doc function_name Displays detailed help document in separate window
Numeric Display Formats
- Controls the display output of the command window Common Format Functions 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
format
Mathematical Functions
Special Variables and Constants
ans
i, j
- Most recent answer
- The imaginary unit
Inf NaN pi
- Infinity - Undefined numerical result (not a number) - The number
Mathematical Functions
Elementary Functions
cos(x) Cosine abs(x) Absolute value
sin(x)
tan(x)
Sine
Tangent
ceil(x)
floor(x)
Round towards + Inf
Round towards - Inf
acos(x)
asin(x)
Arc cosine
Arc sine
round(x)
rem(x)
Round
Remainder after division
atan(x)
exp(x) sqrt(x) log(x) log10(x)
Arc tangent
Exponential Square root Natural Logarithm
angle(x)
conj(x) imag (x) real (x) primes (x)
Phase
Complex Conjugate imaginary Real number Prime Number
Common Logarithm
Mathematical Functions
Scalar Arithmetic
Symbol + Operation Addition
* /
Subtraction
Multiplication Right Division
\
^
Left Division
Exponentiation
Mathematical Functions
Exponential and Logarithmic Functions
exponential
Natural Logarithm Common Logarithm Square root
exp (x)
ln (x) log10 (x) = [ log10 (x) ] sqrt (x)
Example1: 2+34 In Command Window >>2 + 3 4 ans = 1 Example2: 63 In Command Window >> 6 / 3 ans = 2
Example3: 36 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, in the usual way, to indicate precedence. The mathematical operations represented by the symbols + * / \, and ^ follow a set of rules called 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 to give constants and variables names of our choice. This is a powerful facility that can reduce work and help in avoiding input 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 A pair of parentheses is used after the functions name to enclose the value called the functions 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. 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: 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. Perform the indicated operation: a. 2{ 4 [6 + 3 + (7 (1 + 8)) + 12] 3} + 5 b. 5{ [ 2 + 6 8(4 (7 4) 2) + 2] 1}
3 2 3 c. 12+3 2 58 d. 4 2 8 4 25
5 2
e. Given: =
Determine: i. y = sin x ii. when y = 1, what is z = sin-1 y
g. h. cos 2
1 0.3 cos
2. Suppose that x = 3 and y = 4. Use MATLAB to compute the following. a.
c. d.
b. 3
1 1 1 5 2
3 48 4 5 36
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. =
1
1 1 1 + + +
c. = 1
d. =
1 2 2
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