Beruflich Dokumente
Kultur Dokumente
>> 1 + (2 6)
>> 4*8
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).
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?
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.
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
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,
Overloaded methods:
codistributed/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.
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.
6/6