Week 7 C2 the MATLAB Environment
Week 7 C2 the MATLAB Environment
NAME: DATE:
SECTION: SCORE:
Start MATLAB® by double-clicking on the MATLAB icon found in your computer’s 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 MATLAB®’s desktop.
Command Window:
Workspace:
Current Folder:
Command History:
A. MATLAB® AS A CALCULATOR
>> 1 + 2
Press Enter and see what happens. MATLAB® serves as a calculator when you type statements after the
double arrow. The symbols of the four basic operations on scalar quantities are (in order of priority):
^ Exponentiation
* / Multiplication and division
+ - Addition and subtraction
Calculate the following in MATLAB® and write down the outputs exactly as they appear:
1. 1000(1/2 + 3/4)3 1:
1
2. 3 + 2𝜋 (Hint: MATLAB can recognize the word pi) 2:
1.2 3:
3. 23.2 (110.8) + √10
Take note the default number of decimal places in the outputs. When the output gets too small or too
large, MATLAB® reports it in scientific notation. For example, 6.022 × 1023 is written as 6.0220e+23.
When results are exactly integers and no large calculations are involved, MATLAB® does not output
decimal places. Type the following statements and write down the outputs exactly as they appear:
>> 2.0000/1.0000 1:
>> 422232723/(11*17) 2:
>> (9^9)/(9^8) 3:
>> (999^9)/(999^8) 4:
2
Variables are symbols where you can assign a certain value or a result from a calculation. The equal sign
“=” serves as an assignment operator. Type the following assignment statement, and press Enter:
>> a = 2
Observe how MATLAB® responds. Now, enter the following statement and press Enter:
>> a = 2;
Notice the difference between the two. Verify your thoughts by typing the following statements:
>> a = 3; b = 4;
>> c = a^2 + b^2;
>> a, b, c
>> a; b; c;
You can use commas or semicolons to separate two or more statements written on one line.
What is the difference between statements that end with a semicolon and those that do not?
Enter the following statements in MATLAB®. Warning: Some of them contain invalid variable names.
>> piperadius = 4;
>> PipeRadius = 10;
>> first_term = piperadius/PipeRadius;
>> term2 = piperadius – sqrt(PipeRadius);
>> _term3 = 0.5*piperadius;
>> 4term = piperadius^0.3;
Now, call all the valid variables by typing their names and pressing Enter.
There are variable names that are valid, but must not be used. Examples of which are built-in or predefined
variables. Type each of the following built-in variables and know their values.
MATLAB® has numerous built-in mathematical functions. You may try the following codes and explore
how to use the built-in functions coming from different topics in math.
>> a = 16;
>> sqrt(a)
>> sqrt(sqrt(a))
>> sin(pi)
>> asin(ans)
3
>> P = 22/7
>> floor(P)
>> round(P)
>> ceil(P)
>> help round
>> n = 5;
>> factorial(n)
>> factorial(n+1)
>> R = 5;
>> cosh(R)
>> sinh(R)
>> cosh(R)^2-sinh(R)^2
>> help cosh
Note: As in built-in variables, DO NOT use built-in function names as variable names also.
When operations are invalid, MATLAB® outputs either Inf or NaN, which are also built-in variables. Type
the following statements and see the results:
>> log(0)
>> 1/0
>> 0/0
>> exp(1000)
>> factorial(171)
List down the limits of some built-in functions in MATLAB®. At what input values will those functions
overflow (i.e. starts to output Inf)?
4
The MATLAB® desktop, or the Matrix Laboratory Desktop, by default, is divided into four sections:
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 button near the Current Folder field. Click the
Make New Folder button and rename the folder.
CLOSING MATLAB®
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
>> 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.
6
You can use the help command to check other built-in math functions in MATLAB®. The following table
summarizes some of these functions:
FUNCTION COMMAND
Raising to a power >> 5^2
Square root >> sqrt(5)
Natural logarithm >> log(100)
Base-10 logarithm >> log10(100)
Exponential (ex) >> exp(5)
Trigonometric functions >> 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.
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;
>> 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:
7
>> 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.
The semicolon (;) facilitates what we call output suppression in MATLAB®. Output suppression simply
means preventing MATLAB® from displaying the output of the command.
>> 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:
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)
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 short Sets the format to short (scaled fixed-point with 4 digits after the
decimal point)
>> format long Scaled fixed-point with 15 digits after the decimal point
>> format short e Floating point short
>> format long e Floating point long
>> format short g Chooses between fixed or floating point
>> format long g Chooses between fixed or floating point
>> format bank Fixed dollars and cent
>> format rat 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 MATLAB®’s Command Window: