<< Chapter < Page | Chapter >> Page > |
log10
for common (base 10) logarithm. To find the log of 10, type in log10(10) and press enter, (ans = 1).Practice the following examples to familiarize yourself with the common mathematical functions. Be sure to read the relevant
help
and
doc
pages for functions that are not self explanatory.
Calculate the following quantities:
MATLAB inputs and outputs are as follows:
2^3/(3^2-1)
(ans = 1)sqrt(5)-1
(ans = 1.2361)pi/4*2^2
(ans = 3.1416)Calculate the following exponential and logarithmic quantities:
MATLAB inputs and outputs are as follows:
exp(2)
(ans =
7.3891)log((5^10))
(ans =
16.0944)log10(10^5)
(ans =
5)Calculate the following trigonometric quantities:
MATLAB inputs and outputs are as follows:
cos(pi/6)
(ans =
0.8660)tan(45*pi/180)
(ans =
1.0000)sin(pi)+cos(45*pi/180)
(ans =
0.7071)format
FunctionThe
format
function is used to control how the numeric values are displayed in the Command Window. The
short
format is set by default and the numerical results are displayed with 4 digits after the decimal point (see the examples above). The
long
format produces 15 digits after the decimal point.
Calculate
and display results in
short
and
long
formats.
The
short
format is set by default:
>>theta=tan(pi/3)
theta =1.7321>>
And the
long
format is turned on by typing
format long
:
>>theta=tan(pi/3)
theta =1.7321>>format long>>theta
theta =1.732050807568877
In MATLAB, a named value is called a variable. MATLAB comes with several predefined variables. For example, the name pi refers to the mathematical quantity π, which is approximately
pi ans = 3.1416
Variables in MATLAB are generally represented as matrix quantities. Scalars and vectors are special cases of matrices having size 1x1 (scalar), 1xn (row vector) or nx1 (column vector).
The term scalar as used in linear algebra refers to a real number. Assignment of scalars in MATLAB is easy, type in the variable name followed by = symbol and a number:
a = 1
Elements of a row vector are separated with blanks or commas.
Let's type the following at the command prompt:
b = [1 2 3 4 5]
We can also use the New Variable button to assign a row vector. In the tool strip, select Home>New Variable. This action will create a variable called unnamed which is displayed in the workspace. By clicking on the title unnamed, we can rename it to something more descriptive. By double-clicking on the variable, we can open the Variable Editor and type in the values into spreadsheet looking table.
Notification Switch
Would you like to follow the 'A brief introduction to engineering computation with matlab' conversation and receive update notifications?