Beruflich Dokumente
Kultur Dokumente
NAME:
DATE:
SECTION:
SCORE:
WORKSHEET
Environment
The MATLAB
Instructions: This shall serve as your proof of attendance for this meeting. Write what is asked of you on
the space provided beside each item.
Start MATLAB by double-clicking on the MATLAB icon found in your computers desktop. The
MATLAB desktop contains tools for managing files, variables, and applications associated with
MATLAB. Using the built-in help browser, (Help > Product Help) identify the functions of the different
components of MATLABs desktop.
Command Window:
Workspace:
Current Folder:
Command History:
1000 3
Then press Enter. MATLAB can serve as a calculator, and can perform a wide range of mathematical
operations. The symbols for the four basic operations are + (addition), - (subtraction), * (multiplication),
and / (division). Trigonometric functions follow the form sin(_), cos(_), and tan(_). Exponential
operations are indicated by ^. Perform the following operations in MATLAB, and write down your codes
and the output on the space provided:
60
1.
1000(0.1 + 0.15/12)
2.
1/(2 )
3.
1:
2:
3:
From the results you obtained, what can you conclude regarding the units of angle measurement in
MATLAB?
Variables are symbols where you can assign a certain value or expression. In the command window, enter
the following code:
>>
a = 2
2
Observe how MATLAB responds after you press Enter. Now, enter the following codes:
>>
a = 2;
Press Enter. Observe what happens to MATLABs output. Now enter the following codes:
>>
>>
>>
a = 5; b = 4; c = 7;
d = a^2 + b^c;
d
piperadius = 4;
PipeRadius = 10;
first_term = piperadius/PipeRadius;
term2 = piperadius sqrt(PipeRadius);
_term3 = 0.5*piperadius;
4term = pipeRadius^0.3
Now, call the different values of the variables by typing the variable name and then pressing Enter.
What generalizations can you make regarding naming of variables in MATLAB?
format rat
9.123
1/7
10
format long
9.123
1/7
10
>>
>>
>>
>>
format long e
9.123
1/7
10
>>
>>
>>
>>
format long g
9.123
1/7
10
>>
>>
>>
>>
format rat
9.123
format
9.123
3
What generalizations can you make regarding the use of the format function?
MATLAB has numerous built-in functions. You may try the following codes and explore these built-in
functions and their uses.
>>
>>
>>
>>
a = 3; b = 4;
c = sqrt(a^2 + b^2)
atan(a/c)
atand(a/c)
>>
>>
>>
>>
e = exp(1)
log(e)
e = exp(2)
log(e)
>>
>>
>>
>>
A = 100;
A1 = log2(A)
A2 = log10(A)
A3 = log(A)
>>
>>
>>
>>
>>
P = 22/7
floor(P)
round(P)
ceil(P)
fix(P)
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
C = 3+4i;D = -2-3i;
C+D-2*D
C*D+C/D
C^D
D^10
real(C)
imag(C)
abs(C)
C'
sign(C)
>>
>>
>>
>>
>>
>>
>>
M = 4;
mod(M,2)
mod(M,3)
mod(M,4)
factorial(M)
factorial(170)
factorial(171)
>>
>>
>>
>>
>>
>>
[n,d] = rat(6.3333333)
n/d
mod(n,d)
sqrt(d-n)
gcd(n,d)
gcd(48,27)
>>
>>
>>
>>
>>
R = 5;
cosh(R)^2-sinh(R)^2
R = 18;
cosh(R)^2-sinh(R)^2
R = 19;
4
>> cosh(R)^2-sinh(R)^2
MODULE 2 The
Environment
MATLAB
OBJECTIVE
At the end of this module, the student should be able to:
1. Familiarize with MATLABs basic working environment.
2. Use MATLAB as a calculator.
3. Define and create variables.
4. Know how to use MATLAB commands such as help, clc, doc, lookfor, why, who, whos, clear,
etc..
5. Understand the different MATLAB formats and the use of the format function.
6. Use the semicolon for output suppression.
7. Know how to make comments using the % operator.
b.
Command Window/Prompt commands are entered on this pane; program results and return
values for functions are also displayed here.
c.
5
d.
The layout of your MATLAB desktop can be changed by closing or resizing the window panes. To
restore the program to its default layout, go to Desktop, click on Desktop Layout and select Default.
CREATING FOLDERS
To create folders in MATLAB, you can select the
Make New Folder button and rename the folder.
CLOSING MATLAB
You may close MATLAB using two ways:
a. Go to File, and then select Exit; or
b. Type quit or exit in the Command Window.
Do not use the red X button on the title bar to exit MATLAB.
MATLAB is on standby (i.e., it is waiting for a user-input) when the cursor is repeatedly blinking. A
Ready remark is also seen in the Status Bar at the bottom of the window. When a program is running in
MATLAB or it is processing something, the cursor disappears, and the work Busy appears in the Status
Bar.
MATLAB responds to commands by printing results in the Command Window, or by opening a figure
window for graphical outputs.
MATLAB AS A CALCULATOR
Try the following commands on your Command Window.
>>
2 + 5 4
MATLAB displays the result by storing it in a default variable named ans. The answer to any
computation done in the Command Window, when no variable name is indicated, is stored automatically
to ans. You can use this default variable for succeeding computations:
>>
>>
ans/2
ans
=
1.5000
Note that the ans variable value changes for succeeding calculations.
Symbols for mathematical operations include + (addition), - (subtraction), * (multiplication), and /
(forward slash, for arithmetic division). Important: A backward slash \ indicates matrix division.
You can use the help command to check other built-in math functions in MATLAB. The following table
summarizes some of these functions:
FUNCTION
Raising to a power
Square root
Natural logarithm
Base-10 logarithm
x
Exponential (e )
Trigonometric functions
COMMAND
>>
5^2
>>
sqrt(5)
>>
log(100)
>>
log10(100)
>>
exp(5)
>>
sin(2)
VARIABLES
Variables are used in MATLAB to store values that will be recalled later. Variables are symbolic names
that act storage bins to data that can be called for future use. The use of variables helps programmers
create efficient programs that can run faster.
MATLAB has specific rules on variable naming:
a.
b.
c.
d.
e.
Some built-in variables in MATLAB include pi (which contains the value of pi) and ans. Built-in
functions in MATLAB include sin, cos, log, and log10.
MATLAB recognizes variable assignment when the variable name is followed by an equal sign, and the
value you would like to store. This value may be a decimal number, a character, or a string of characters.
The following lines show examples of variable assignment:
>>
>>
>>
>>
voldie1 = 5
voldie2 = harry potter is dead
a0 = 1;
aN = 10;
7
>>
>>
term = 10;
sum = 0.5*term*(a0 + aN);
Overwriting assignment refers to assigning a new value to a variable name. The following code shows an
example of overwriting:
>>
>>
>>
voldie = 5;
voldie = voldie + 1;
voldie = voldie*5;
The first line of code assigns a value of 5 to the variable voldie. The second line updates the old value of
voldie (found in the right-hand side of the equation) by adding 1 to it. This results to a new value for the
variable, which is 6. The third line of code multiples the old value of voldie by 5. You can confirm that
the current value of voldie is 30 by looking at the Workspace of your MATLAB desktop.
x = 5
x =
5
MATLAB automatically displays the value of the variable upon pressing the Enter button. To prevent
the program from doing this, we can add a semicolon:
>>
x = 5;
This suppresses the output from displaying, but still allows the variable assignment of the value of 5.
We can also use semicolons to create multiple statements in one line, as show below:
>>
x = 4; y = x^2; z = sqrt(x);
The use of semicolons for multiple statements completely suppresses the display of the output. When
you want to create multiple statements in one line but would like to display the output, commas can be
used:
>>
x = 4, y = x^2, z = sqrt(x)
Pressing the Enter button will display the values of x, y, and z.
8
The format function sets the display format for an output in MATLAB. Fixed-point numbers are
integers that have a fixed number of digits after the decimal point. Floating-point numbers are
expressed in scientific notation. Some display formats are summarized below:
>>
format command
format
>>
format short
>>
>>
>>
>>
>>
>>
>>
format
format
format
format
format
format
format
long
short e
long e
short g
long g
bank
rat
ACTION
Default format; scaled fixed-point with 4 digits after the decimal
point
Sets the format to short (scaled fixed-point with 4 digits after the
decimal point)
Scaled fixed-point with 15 digits after the decimal point
Floating point short
Floating point long
Chooses between fixed or floating point
Chooses between fixed or floating point
Fixed dollars and cent
Shows values as ratio of small integers
ADDITIONAL NOTES
The following table summarizes the actions of the functions listed. You may try writing each of the
commands in MATLABs Command Window: