Вы находитесь на странице: 1из 9

Exercise No.

1
THE MATLAB ENVIRONMENT AND BASIC FUNCTIONS

NAME:Eric John A. Hornales DATE PERFORMED: June 29, 2018


SECTION: BSEE-IV DATE SUBMITTED: July 6, 2018
GROUP NO.: INSTRUCTOR: Engr. Catipon

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.

The MATLAB environment allows the user to:


• manage variables
• import and export data
• perform calculations
• generate plots
• develop and manage files for use with MATLAB.

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

The desktop includes these panels:

• Current Folder — Access your files.


• Command Window — Enter commands at the command line, indicated by the prompt (>>).
• Workspace — Explore data that you create or import from files.
• Command History — View or rerun commands that you entered at the command line.

Exercise 1 – The Matlab Environment and Basic functions


Student’s Manual Page 1 of 9
D. Issuing commands to create variables and call functions

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

Commands involving variables:


• who: lists the names of defined variables
• whos: lists the names and sizes of defined variables
• clear: clears all varialbes, reset the default values of special variables.
• clear name: clears the variable name
• clc: clears the command window
• clf: clears the current figure and the graph window.

E. Basic operations and functions in Matlab:

MATLAB has all of the basic arithmetic operations built in:


+ addition
- subtraction
* multiplication
\ division
^ exponentiation

as well as many more complicated 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.
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.

Exercise 1 – The Matlab Environment and Basic functions


Student’s Manual Page 2 of 9
acot - Inverse cotangent.
acotd - Inverse cotangent, result in degrees.
acoth - Inverse hyperbolic cotangent.
hypot - Square root of sum of squares.

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.

Rounding and remainder:


fix - Round towards zero.
floor - Round towards minus infinity.
ceil - Round towards plus infinity.
round - Round towards nearest integer.
mod - Modulus (signed remainder after division).
rem - Remainder after division.
sign - Signum.

F. In getting help:
type one of following commands in the command window:

• help – lists all the help topic


• help topic – provides help for the specified topic
• help command – provides help for the specified command
• help help – provides information on use of the help command
• helpwin – opens a separate help window for navigation
• lookfor keyword – Search all M-files for keyword

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

2. Create a few more variables.

b=2

Exercise 1 – The Matlab Environment and Basic functions


Student’s Manual Page 3 of 9
b=
2

3. Implement the sum of a and b as:


c=a+b

Note: Matlab will display the result

c=
14

4. Try other basic operations:

d=a-b

d=
10

e=b-a

e=
-10

f=a*b

f=
24

g=a/b

g=
6

5. Matlab can also compute exponents and scientific notations

to get 23 we type 2^3

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

Exercise 1 – The Matlab Environment and Basic functions


Student’s Manual Page 4 of 9
7. Matlab can be used to compute trigonometric functions.
For example:

To get sin 30 = 0.5000, type:

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

8. Matlab can compute exponentials like:

To compute ln 24 = 3.1781

type

log(24)

ans=
3.1781

To compute log10 45 = 1.6532

type

log10(45)

ans=
1.6532

To compute log2 60 = 5.9069

type

log2(60)

ans=

5.9069

9. Matlab can compute square roots and nth roots

To compute for the square root of 81, type:

sqrt(81)

ans =
9

To compute for the real cube root of -2, type:

nthroot(-2, 3)
ans =
-1.2599
By comparison,
(-2)^(1/3)

Exercise 1 – The Matlab Environment and Basic functions


Student’s Manual Page 5 of 9
returns a complex cube root of -2.
ans =
0.6300 + 1.0911i
Note: Syntax is y = nthroot(X, n) where y = nthroot(X, n) returns the real nth root of the elements of X. Both X and n must be real
and n must be a scalar. If X has negative entries, n must be an odd integer.

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);

(no answer will be displayed)

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

Note: It will return a table with the following


Name Size Elements Bytes Density Complex

13. A similar command, called who, only provides the names of the variables that are active.
Type:

who

Note: It will return a table with


Your variables are:

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

now if we ask about b


Type:

Error using ==> evalin


Undefined function or variable 'b'.

we get the error message that it's not a variable in memory - we've succeeded in getting rid of it.

To get rid of everything in memory, just type:


clear all

15. You can get a list of all the built-in functions by typing
help elfun

Exercise 1 – The Matlab Environment and Basic functions


Student’s Manual Page 6 of 9
16. When we quit a Matlab program, all variables are lost. However, invoking the command save filename before exiting, causes all
variables to be written to a binary file called filename.mat. When we start MATLAB again, we may retrieve the information in this file
with the command load filename.

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.

17. End a Matab program by typing:

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)

ans = 8.7280 x 1042

Matlab answer: 8.7280e+42

Code: 2^(-3) +7^(8) -8^(7) -9^(0) +3^(-4)+5^(9) +7^(12)-87^(6) +9^(45) -42^(6)

B. sin 20 – 3cos 25 + 4tan 35 in degrees

Computed: 0.4239
Solution:
sin 20 – 3cos 25 + 4tan 35

Matlab answer: 0.4239

Code: (sind(20))-(3*cosd(25)) + (4*tand(35))

C. (1.2 X 10-4)(3.67 X 106) + 34.56 X 10-7

Computed: 440.4000
Solution: (1.2 X 10-4)(3.67 X 106) + 34.56 X 10-7

Matlab answer: 440.4000

Code: 1.2e-4 * 3.67e+6 + 34.56e-7

Exercise 1 – The Matlab Environment and Basic functions


Student’s Manual Page 7 of 9
D. (log 45 – log 32) – log2 12

Computed: -3.4369
Solution: (log 45 – log 32) – log2 12

Matlab answer: -3.4369

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)

Matlab answer: 1.6613

Code: ((sqrt(5))*(sind(12))) + ((nthroot(12,4))*(cosd(50)))

F. (sin2 45)(cos 32)/ (tan3 30), θ in degrees

Computed: 2.2033
Solution: (sin2 45)(cos 32)/ (tan3 30)

Matlab answer: 2.2033

Code: (((sind(45))^2)*(cosd(32)))/((tand(30))^3)

G. 14.5 Arccos 0. 25 + 4.36 Arcsin 0. 55, θ in degrees

Computed: 1.2406 x 103


Solution: 14.5 Arccos 0. 25 + 4.36 Arcsin 0. 55

Matlab answer: 1.2406e+03

Code: (14.5*acosd(1/4))+(4.36*asind(.55))

Exercise 1 – The Matlab Environment and Basic functions


Student’s Manual Page 8 of 9
3
H. sin 45 − √8 + sin 12 , θ in degrees
4

Computed: -2.3346
3
Solution: sin 45 − √8 + sin 12
4

Matlab answer: -2.3346

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

Matlab answer: 2.5883

Code: (5*(log10(7)))+((cosd(34))-(nthroot(15,3)))

2
J. 14.5X10-19 √156 cos2 12, θ in degrees

Computed: 1.7328 x 10-17


2
Solution: 14.5X10-19 √156 cos2 12,

Matlab answer: 1.7328e-17

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.

Exercise 1 – The Matlab Environment and Basic functions


Student’s Manual Page 9 of 9

Вам также может понравиться