Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Foundation and Fundamental

Concepts
Help Features in MATLAB
• help function_name
• helpwin function_name
• doc function_name
• helpbrowser
General Purpose Commands
Managing Commands and Functions
help Display M-file for MATLAB functions
in the Command Window
helpwin Display M-file help and provide
access to M-file for all functions
doc Display HTML documentation in
Help Browser
Managing Variables and the Workspace
clear Remove items form the workspace
who,whos List the variables in the workspace
Controlling the Command Window
clc Clear Command Window
format Control the display format for output
Working with Files and the Operating
Environment
edit Edit an M-file
Mathematical Functions
Special Variables and Constants
ans, i, j, Inf, NaN, pi
Trigonometric Functions
sin, cos, tan, csc, sec, cot, asin, acos, atan,
acsc, asec, acot, sinh, cosh, tanh, csch, csch,
sech, coth
Exponential Functions
exp, log, log10, sqrt
Complex Function
abs, angle, conj, imag, real
Rounding and Remainder Function
fix, floor, ceil, round, mod, rem
Coordinate Transformation Function
cart2sph, cart2pol, pol2cart, sph2cart
Number Theoretic Function
primes, gcd, lcm, rat, rats
Scalar Arithmetic Operations
Symbol Operation
+ Addition
– Subtraction
* Multiplication
/ Right Division
\ Left Division
^ Exponentiation
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.
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, 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
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. 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}
c.  3( 2)  3
 12  3(2)
d 58 8  ( 4 )

4  ( 2 ) 25
e. Given:
5
x
2
Determine:
i. y = sin x
ii. when y = 1, what is z = sin-1 y
cos 1  0.3 
g. e
h. cos ( / 2)
2. Suppose that x = 3 and y = 4. Use MATLAB
to compute the following.
1
a.  1
1  5 
 x2 
b. 3  x
c. 3y
4x  8
d. 4( y  5 )
3x  6
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. a c
x  1  2
b f
b. ba
s
dc
c. r  1 d. 1f2

1 1 1 1 y  ab
   c 2
a b c d
4. Evaluate the following expressions in
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. r  y
f. s = xy2

You might also like