Академический Документы
Профессиональный Документы
Культура Документы
1
THE MATLAB ENVIRONMENT AND BASIC FUNCTIONS
I. OBJECTIVE:
To introduce the concept of mathematical programming using the software called MATLAB and study how to define variables and
write simple MATLAB codes.
II. OVERVIEW:
A. What is MATLAB ?
MATLAB is a computer program that combines computation and visualization power that makes it particularly useful tool for engineers.
It is an executive program, and a script can be made with a list of MATLAB commands like other programming language.
MATLAB Stands for MATrix LABoratory. The system was designed to make matrix computation particularly easy.
B. Opening MATLAB
START > PROGRAMS > MATLAB 6.5 > MATLAB 6.5
Or shortcut creation/activation on the desktop
C. Starting Matlab
When you start MATLAB, the desktop appears in its default layout
Variable names:
• Must start with a letter
• May contain only letters, digits, and the underscore “_”
• Matlab is case sensitive, i.e. one & OnE are different variables.
• Matlab only recognizes the first 31 characters in a variable name.
Special variables:
• ans : default variable name for the result
• pi: π = 3.1415926…………
• eps: Є = 2.2204e-016, smallest amount by which 2 numbers can differ.
• Inf or inf : ∞, infinity
• NaN or nan: not-a-number
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.
acosh - Inverse hyperbolic cosine.
tan - Tangent.
tand - Tangent of argument in degrees.
tanh - Hyperbolic tangent.
atan - Inverse tangent.
atand - Inverse tangent, result in degrees.
atan2 - Four quadrant inverse tangent.
atanh - Inverse hyperbolic tangent.
sec - Secant.
secd - Secant of argument in degrees.
sech - Hyperbolic secant.
asec - Inverse secant.
asecd - Inverse secant, result in degrees.
asech - Inverse hyperbolic secant.
csc - Cosecant.
cscd - Cosecant of argument in degrees.
csch - Hyperbolic cosecant.
acsc - Inverse cosecant.
acscd - Inverse cosecant, result in degrees.
acsch - Inverse hyperbolic cosecant.
cot - Cotangent.
cotd - Cotangent of argument in degrees.
coth - Hyperbolic cotangent.
Exponential:
exp - Exponential.
expm1 - Compute exp(x)-1 accurately.
log - Natural logarithm.
log1p - Compute log(1+x) accurately.
log10 - Common (base 10) logarithm.
log2 - Base 2 logarithm and dissect floating point number.
pow2 - Base 2 power and scale floating point number.
realpow - Power that will error out on complex result.
reallog - Natural logarithm of real number.
realsqrt - Square root of number greater than or equal to zero.
sqrt - Square root.
nthroot - Real n-th root of real numbers.
nextpow2 - Next higher power of 2.
Complex:
abs - Absolute value.
angle - Phase angle.
complex - Construct complex data from real and imaginary parts.
conj - Complex conjugate.
imag - Complex imaginary part.
real - Complex real part.
unwrap - Unwrap phase angle.
isreal - True for real array.
cplxpair - Sort numbers into complex conjugate pairs.
F. In getting help:
type one of following commands in the command window:
G. Exiting Matlab:
• exit – used to terminate a Matlab program, same as quit
• quit – terminate a Matlab program
• finish – termination file for Matlab program
III. IMPLEMENTATION:
1. For example, create a variable named a by typing this statement at the command line:
a=12
Note: MATLAB adds variable a to the workspace and displays the result in the Command Window.
a=
12
b=2
c=
14
d=a-b
d=
10
e=b-a
e=
-10
f=a*b
f=
24
g=a/b
g=
6
ans=
8
Note: When you do not specify an output variable, MATLAB uses the variable ans, short for answer, to store the results of your
calculation.
"Scientific notation" expressed with "10^" replaced by "e" - that is, 107 is written 1e7 and 2.15x10-3 is written 2.15e-3.
For example, type:
1.5e-2
ans =
0.0150
2e-3
ans =
0.002
6. Matlab has some built in variables; one of the most useful is pi:
pi
ans =
3.1416
sind(30)
ans=
0.5000
cosd(45)
ans=
0.7071
tand(60)
ans=
1.7320
Note: the d in the function indicates that the result will be in degrees
To compute ln 24 = 3.1781
type
log(24)
ans=
3.1781
type
log10(45)
ans=
1.6532
type
log2(60)
ans=
5.9069
sqrt(81)
ans =
9
nthroot(-2, 3)
ans =
-1.2599
By comparison,
(-2)^(1/3)
10. If you end a statement with a semicolon, MATLAB performs the computation, but suppresses the display of output in the
Command Window.
h=3*sind(45)
h=
2.1213
h=3*sind(45);
i=h+5
i=
7.1213
To mean that the answer for h was actually stored but not displayed
11. You can recall previous commands by pressing the up- and down-arrow keys, ↑ and ↓.
Press the arrow keys either at an empty command line or after you type the first few characters of a command. For example, to recall
the command b = 2, type b, and then press the up-arrow key.
12. It is good practice to keep track of what variables are defined and occupy our workspace. Due to the fact that this can be
cumbersome, MATLAB can do it for us. The command whos gives all sorts of information on what variables are active.
Type:
whos
13. A similar command, called who, only provides the names of the variables that are active.
Type:
who
14. If we no longer need a particular variable we can “erase” it from memory using the command clear variable name. Using the
clear command, to remove b from memory:
clear b
we get the error message that it's not a variable in memory - we've succeeded in getting rid of it.
15. You can get a list of all the built-in functions by typing
help elfun
Note: We can also create an ascii (text) file containing the entire MATLAB session if we use the command diary filename at the
beginning and at the end of our session. This will create a text file called filename (with no extension) that can be edited with any text
editor, printed out etc. This file will include everything we typed into MATLAB during the session (including error messages but
excluding plots). We could also use the command save filename at the end of our session to create the binary file described above
as well as the text file that includes our work.
quit
or
exit
IV. EXERCISES:
1. Use a calculator to solve for the following then use Matlab. Write answers in the space provided. Write also the codes used for
Matlab implementation.
A. 2-3 +78 -87 -90 +3-4 +59 +7-12 – 876 +945 -426
Computed: 8.7280e+42
Solution:
2^(-3) +7^(8) -8^(7) -9^(0) +3^(-4)+5^(9) +7^(12)-87^(6) +9^(45) -42^(6)
Computed: 0.4239
Solution:
sin 20 – 3cos 25 + 4tan 35
Computed: 440.4000
Solution: (1.2 X 10-4)(3.67 X 106) + 34.56 X 10-7
Computed: -3.4369
Solution: (log 45 – log 32) – log2 12
Code: (log10(45)-log10(32))-(log2(12))
4
E. √5(sin 012) + √12 (cos 50), θ in degrees
Computed: 1.6613
4
Solution: √5(sin 012) + √12 (cos 50)
Computed: 2.2033
Solution: (sin2 45)(cos 32)/ (tan3 30)
Code: (((sind(45))^2)*(cosd(32)))/((tand(30))^3)
Code: (14.5*acosd(1/4))+(4.36*asind(.55))
Computed: -2.3346
3
Solution: sin 45 − √8 + sin 12
4
Code: ((3/4)*(sind(45)))-(sqrt(8+sind(12)))
3
I. 5 log 7 + cos 34 - √15, θ in degrees
Computed: 2.5883
3
Solution: 5 log 7 + cos 34 - √15
Code: (5*(log10(7)))+((cosd(34))-(nthroot(15,3)))
2
J. 14.5X10-19 √156 cos2 12, θ in degrees
Code: (14.5e-19)*(sqrt(156))*(cosd(12))^2
V. GENERALIZATION:
After engaging this activity, I have generalize the different basic functions of Matlab like trigonometric identities and logarithmic
functions. I also learned in this activity that when using Matlab, you have to be careful of inputing commands or else the answer
might be incorrect.