You are on page 1of 6

16165 Engineering Analysis 1 2014/2015

Activity 7.1 : Learning MATLAB

1. Performing basic arithmetic


At the core of any mathematical software is a very powerful calculator. Try typing in the following commands in
the Command Window. Note, you need to press the ENTER key after each command in order to execute it.

>> 1 + (2 6)

>> 4*8

>> 1e4 / -1.25

Notice how the answers are displayed after each command. What does the e do? Hint: try entering just 1e2
in the command line and see what value is displayed.

Almost all calculator functions have equivalent commands in Matlab. Enter each of the following commands
individually and watch the output.
sin(90)
sin(90*pi/180)
(sin(1.25))^2 + (cos(0.85))^2
sqrt(25)
exp(1.0)
log(10)
exp(log(10))
Why is sin(90) not equal to 1?

These expressions are using a number of predefined Matlab functions, for example cosine and sine functions
(cos and sin respectively), natural logarithm (log) and corresponding exponential (exp) functions. Matlab
can also return values for infinity (-Inf, Inf ) and Not a Number (NaN).

>> 0/0
Warning: Divide by zero.
ans =
NaN

>>1/0
Warning: Divide by zero.
ans =
Inf

1/6
2. Command history
The Command History window lists all the commands that have been entered by the user. Note, if you cannot
see the Command History window, you can open it by going under the MATLAB START menu > Desktop Tools >
Command History.
The content of the Command Window can also be recorded to a text file by using the command diary. The
command causes a copy of all the subsequent command window inputs and most of the resulting command
window output to be appended to the user-supplied file. If no filename is specified, the file 'diary' is used and is
saved in the Current Folder. Note that the Current Folder must point to a folder on your own network drive; if
you save any files to the local hard drive of the computer, the files will be lost when you log off.

>> diary on
Will start saving everything to a default text file called diary (with no file extension).

>> diary foo.txt


Will start saving everything to a file called foo.txt. This file can be named almost anything you want however you
cannot use any special characters such as spaces or dashes (minus sign) in the filename. Filenames must also
start with a letter.

>> diary off


Will stop the recording of the command window entries (but will not delete the file).

3. Working with variables


Now try entering variables. Enter each of the following commands individually in the Command Window and
watch what happens:
>> a = 4
>> a = 4;
>> b = 6;
>> a * b
>> c = a^2 + b^3
>> d = a^2 + b^3;
>> c, d

See if you can find the variables you just entered (a, b, c, d) in the Workspace window. What values are
entered for each of the four variables?

What does the semi-colon (;) do? What is the difference between putting a semi-colon after a command, and
not?

Enter each of the following commands:


>> a = b
>> c = 5
>> d = 10*b;

2/6
Now look at what values are displayed in the Workspace window for each of the variables. Matlab only
remembers the last value assigned for each variable. For example, the variable c is equal to 5 and not 232 (a^2
+ b^3) since 5 was the last value it was set equal to.

What happens if you try to enter the following:


>> g
>> g + 6

Why did this give an error?


Every variable in Matlab must always have a number value assigned to it. In the case above, since you did not
tell Matlab beforehand what the variable g was equal to, it gives an error essentially saying it doesnt know the
value since it was never defined.

Matlab is also case-sensitive, which means that a variable called A is different than a variable named a.
>> R = 5
>> r = 10
Check the variables in the Workspace, you should have two entries: one for the capital letter R and another for
the small letter r.
Matlab stipulates that variable names must begin with a letter, followed by any combination of letters, numbers
or underscores up to a maximum of 31 characters. There are a number of reserved, or pre-defined, variable
names. These can be overwritten by the user, so be careful when choosing variable names. The following
example for the constant pi ( = 3.14).
>> pi
ans =
3.14159265358979
>> pi = 6;
>> pi
pi =
6
You should also avoid using variable names that are the same as in-built functions, for example sin=6, should
not be used.

In addition to the Workspace Window, all the variable that have been entered can be displayed to the screen by
the command who. To erase a variable (and reset the variable name), use the command clear.
>> who
>> clear a pi
>> who
Notice that the variables a and pi do not show up anymore. The command clear all will erase all the
variables in memory.

3/6
4. Accessing the help
Help is available through the command prompt, and through the Help menu in the top toolbar. Help is available
from the command line prompt. Typing the general command help will yield a list of general topics.

>> help
HELP topics

matlab\general - General purpose commands.


matlab\ops - Operators and special characters.
matlab\lang - Programming language constructs.
matlab\elmat - Elementary matrices and matrix
manipulation.
matlab\randfun - Random matrices and random streams.
matlab\elfun - Elementary math functions.
matlab\specfun - Specialized math functions.
matlab\matfun - Matrix functions - numerical linear
algebra.
matlab\datafun - Data analysis and Fourier transforms.
matlab\polyfun - Interpolation and polynomials.
matlab\funfun - Function functions and ODE solvers.
matlab\sparfun - Sparse matrices.
matlab\scribe - Annotation and Plot Editing.
matlab\graph2d - Two dimensional graphs.
matlab\graph3d - Three dimensional graphs.
etc.

To obtain further help on any of the topics listed, type help followed by that topic. For example,

>> more on
>> help elfun
Elementary math functions.

Trigonometric.
sin - Sine.
sind - Sine of argument in degrees.
sinh - Hyperbolic sine.
asin - Inverse sine.
asind - Inverse sine, result in degrees.
asinh - Inverse hyperbolic sine.
cos - Cosine.
cosd - Cosine of argument in degrees.
cosh - Hyperbolic cosine.
acos - Inverse cosine.
acosd - Inverse cosine, result in degrees.

4/6
acosh - Inverse hyperbolic cosine.
tan - Tangent.
--more--

The command more on (and the corollary more off) pauses Matlab after one screen of data. Pressing
CTRL+C will stop any process that is running in Matlab, and will bring up the command prompt. To see more
information about a particular function, type help followed by the function name. For example,

>> help sin


SIN Sine of argument in radians.
SIN(X) is the sine of the elements of X.

See also asin, sind.

Overloaded methods:
codistributed/sin

Reference page in Help browser


doc sin

Use the help command to look over some of the commands that you have seen in this activity.

If you do not know the exact name of a command, the lookfor [keyword] command will search all Matlab
commands for that keyword. Note: this command is often slow as it has to search through a large amount of
non-indexed data, so do not be concerned if nothing appears right away.

>> lookfor plot


meshgrid - X and Y arrays for 3-D plots.
callAllOptimPlotFcns - Helper function that manages the plot
functions.
odeplot - Time series ODE output function.
optimplotfunccount - Plot number of function evaluations at each
iteration.
optimplotfval - Plot value of the objective function at
each iteration.
optimplotx - Plot current point at each iteration.
--more--

Using the help, find out what the following commands do:
clc
zeros
whos
format

5/6
5. Find the answers to the following:

( ( ) )

( )

( ) ( )

6. Calculate the surface area of a cylinder of radius = 1.3 m and length = 4.5 m, by first entering variables
with values for the radius and length, then solving the equation for the area of a cylinder given below.

Using the above procedure, calculate the surface area of:


a) A cylinder with a radius of 3 cm and a length of 1 m
b) A sphere with a radius of 7 cm, where the area of a sphere is given by

6/6

You might also like