Southern Luzon State University
College of Engineering
ECE Department
ECM02- ADVANCED ENGINEERING MATHEMATICS LAB
ACTIVITY 1
MATLAB FUNDAMENTAL
Group No: 5 (Set A) Date Performed: February 18, 2024
Names: Fabie, Yuan Ashley S. Date Submitted: February 19, 2024
Palima, Marko M.
Placino, Ellah Marie F.
Yr. & Sec: BSECE II - GG
RATING
Sir Rene M. Inson
INSTRUCTOR
I. OBJECTIVES:
1. Identify the best function to be used in every computational problem.
2. Know the syntax and the use of each built-function.
3. Perform basic calculations in the command line.
4. Perform series of mathematical calculations to solve complex calculations using
scripts.
II. INTRODUCTORY INFORMATION
MATLAB can function as a computing device. It can serve as a calculator and can
perform basic as well as complex computations. Some computations require series of
operations and require a sequence of commands called scripts. In MATLAB scripts is
saved in an m-file and can be executed
III. MATLAB BACKGROUND
When typing functions or any other commands in the command window, it is important to
keep in mind the following notes:
• Commands and functions are typed at the command window at the end of the
blinking command prompt >>
• The precedence of operations follow the PEMDAS (parenthesis, exponentiation,
(*,/) multiplication and division and then (+,-).
• The grouping symbol used are ( ), or { } to vary the precedence. Note that brackets
are not being used as a grouping symbol.
• When you write the semicolon ‘ ; ‘ at the end of a statement, the computer will
process the command but will not display the result of the command, and it will not
echo or display the command when placed inside another function or script.
• MATLAB remembers past information. To recall previous commands, MATLAB
uses the cursor keys, ←, ↑, →, ↓, on your keyboard or Double click the command
previously inputted at the command History.
In performing some basic computation as well as advanced calculation MATLAB offers wide
array of built in commands and functions that will be able to provide solutions to some
mathematical and engineering problems. MATLAB offers support on the usage of the built
in commands as listed below:
COMMAND BRIEF DEFINITION
lookfor Searches all m-file documentations that contains the keyword after the
lookfor command for keyword high-level functions
help Display m-file manual for MATLAB function in the Command Window
helpwin Display m-file help and provide access to m-file for all functions
doc Display HTML documentation in Help Browser
helpbrowser Opens the help browser window
Visual Examples:
In order to search for commands associated with the tangent function, we type on the
command window:
2 | ECM02L- Advanced Engineering Mathematics for ECE Lab
Upon pressing the return key, MATLAB displays the list all commands associated with
tangent or has tangent in its documentation:
The MATLAB DIARY
This is a command to save all the commands being typed in the command window and stores
it to a certain file set by the user as <filename> and can be access anytime using the m-file
editor or notepad.
COMMAND BRIEF DEFINITION
diary <filename> creates a diary file <filename> on the current directory and starts the
saving session
diary off Suspends the writing on the file and saves the file.
diary on Turns the diary back on
diary Toggles the diary from on to off or off to on
MATLAB has its built-in functions and some predefined constants. It is then advised to
check the availability of the keywords before we assign the result of certain operation to
these symbols/words.
MATLAB RESERVED KEYWORDS
KEYWORD BRIEF DEFINITION
Ans Default answer handler. All results of mathematical operation is saved to this
variable.
i, j Imaginary number. Equivalent to
Inf Infinity
NaN Not a Number
π = 3.1415926
pi
BUILT – IN MATLAB FUNCTIONS Functions in MATLAB returns one or more values
and accepts arguments enclosed in a parenthesis
SYNTAX
Variable = <command>(argument)
3 | ECM02L- Advanced Engineering Mathematics for ECE Lab
Visual Example
Trigonometric Functions
FUNCTION BRIEF DEFINITION
sin, cos, tan, Trigonometric functions in radians
csc, sec, cot
asin, acos, atan, Inverse trigonometric functions in radians
acsc, asec, acot
sind, cosd, tand, Trigonometric functions in degrees
cscd, secd, cotd
asind, acosd, atand, Inverse trigonometric functions in degrees
acscd, asecd, acotd
Other Transcendental Functions
FUNCTION BRIEF DEFINITION
sinh, cosh, tanh, Hyperbolic functions
csch, sech, coth
asinh, acosh, atanh, Inverse hyperbolic functions
acsch, asech, acoth
exp, sqrt Exponentiation and Square Root
log, log10 Natural and Common Logarithm
Functions for Complex Variables
FUNCTION BRIEF DEFINITION
abs The absolute or the magnitude of the complex number
angle The angle of the complex number
Conj The conjugate of complex number
imag Extracts the imaginary part of a complex number
Real Extracts the real part of a complex number
Coordinate Transformation
FUNCTION BRIEF DEFINITION
cart2sph[θ, ϕ, 𝑟]=cart2sph(x,y,z) Transforms cartesian to spherical coordinates
cart2pol [ϕ ρ]=cart2pol(x,y) Transform Cartesian to cylindrical polar coordinates
pol2cart [x,y]=pol2cart(ϕ, ρ) Transform polar to Cartesian coordinates
sph2cart[x,y,z]=sph2cart(θ, ϕ, 𝑟) Transform Cartesian to spherical coordinates
Rounding Off Commands
FUNCTION BRIEF DEFINITION
fix Rounds toward zero
floor Rounds toward the lower value
ceil Rounds toward the higher value
round Rounds toward the closest integer
Number Theory Commands
FUNCTION BRIEF DEFINITION
primes List down the prime numbers less than the given number
gcd Greatest Common Divisor
lcm Least Common Multiple
4 | ECM02L- Advanced Engineering Mathematics for ECE Lab
rat Rational Approximation
rats Rational Form
mod Modulus after division
rem Remainder after division
MATLAB SCRIPTS
M-File Editor is used to create, edit and debug series of commands and functions in form a
script or a function. Script is a series of instructions/commands to be processed by MATLAB.
When script is used, two or more commands are processed one after the other in a command
line. When an M-File editor is used, scripts are saved as Standard ASCII text files. The
M-File saved with an extension of .m contains series of MATLAB expressions stored
together in a predefined sequence. The downside of using scripts is that all variables are
created/added in the workspace and existing variables in the workspace may be overwritten
Two ways of opening the M-FILE Editor
1. On the command line type
edit <filename.m>
2. On the windows icon
Start>>Programs>>Matlab>>M File Editor
INPUT COMMAND
The input command is used to prompt the user for a numeric or string input where input
parameters to functions are preferred.
Syntax
entry = input (‘Text Display’)
entry = input (‘Text Display’, ‘s’ )
Visual Example:
DISPLAY COMMAND
The display command is used to show text or array on the command window.
Syntax
disp(‘Text Display’)
Visual Example
5 | ECM02L- Advanced Engineering Mathematics for ECE Lab
COMMENTS
MATLAB Comments are statement or characters written in the command line or in an m-file
script or function that are not being interpreted by MATLAB. These comments are used to
annotate the program for easy use and interpretation. When used inside a script, the
comments will be shown when the help command is used to access the script. Comments are
made by placing statements after the percent sign (%).
Example:
>> % This line will not be processed by MATLAB
>> A = 1 + 1 % This performs addition of 1 and 1, then stores it to the variable A
IV. EXERCISES
Answer the following on space provided.
1. Using the help features identify the command that would best be applicable to apply in
solving the following:
Operation to be done Applicable Command
a. Sine of an angle in Radians y = sin (x)
b. Inverse Secant of Angle in Degrees y = asecd (x)
c. Natural Logarithm of a Number y = log (x)
d. Exponentiation of a Number at Base e y = exp (x)
e. Logarithm of a number at base 10 y = log10 (x)
a. Sine of an angle in Radians y = sin (x)
2. Setup your diary with a filename activity3_goup#.doc. Perform the following operations.
Turn-off your diary in between trials. Make sure that your diary contain only correct answers.
Turn your diary off after excercise2.
Problem Answer
2 2 𝑜
a. 𝐴 = 4 + 2 − 2(4)(7) 𝑐𝑜𝑠 (25 )
0.0000 + 5.5456i
Syntax: A = sqrt ( 4^2 + 2^2 - 2 * (4) * (7) * cosd ( 25 ) ) )
−5−8 8−(−4)
b. 𝐵 = 4−(−2)
÷ 2−5
0.5417
Syntax: B = ( ( - 5 - 8 ) / ( 4 - ( - 2 ) ) ) /( ( 8 - ( - 4 ) ) / ( 2 - 5 ) )
6 | ECM02L- Advanced Engineering Mathematics for ECE Lab
| 𝑡𝑎𝑛 (450−𝑐𝑜𝑠−1( 45 ) ) ||
c. 𝐶 = 𝑙𝑛 | 2
| 7 −6𝑠𝑖𝑛 (3π/7) |
Syntax: C = log (abs ( ( tand ( 45 - cos^-1 ( 4/5 ) ) ) / ( 7^2 - 6 * -5.7106
sin (3 * pi / 7 ) ) ) )
| 𝑐𝑜𝑠 (30°+𝑠𝑖𝑛−1( 25 ) ) |
log | 2 |
| 2 −3𝑠𝑖𝑛 (2π/9) |
d.𝐷 = 3𝑒
Syntax: D = 3 * exp (log (abs ((cosd ( 30 + sin^-1 ( 2/5 ) ) ) ) / ( 0.8598
2^2 - 3 * sin (2 * pi / 9 ) ) ) )
e. 𝐸 = 2{− 4 − [6 + 3 + (7 − (1 + 8)) + 12] − 3} + 5
Syntax: E = 2 * (- 4 - (6 + 3 + (7 - (1 + 8 ) ) + 12) -3 ) + 5 -47
7 | ECM02L- Advanced Engineering Mathematics for ECE Lab
3. Program Analysis:
The script shown below is a simple m-file script. Narrate on the right side the processes
being done.
PROGRAM NARRATION
% This is a program that Converts C to F This is the starting program that Converts C
to F.
disp(‘Hello, Welcome to C to F converter’) It will display “Hello, Welcome to C to F
converter.”
Celsius=input(‘Enter the value in Celsius’) It will display “Enter the value in Celsius”
and it will input the entered value as
“Celsius.”
Fahrenheit = 9/5*Celsius +32 Using the formula of conversion of C to F, it
will calculate the inputted value.
disp(‘The value of the temperature in It will display “The value of the temperature
Fahrenheit is’) in Fahrenheit is.”
disp(Fahrenheit) It will display your entered value of Celsius
into a Fahrenheit using the formula.
disp(‘Thank you for using this program’) It will display “Thank you for using this
program.”
4. Machine Problem
Create a simple that would be able to accept two sides (a and b) and an included angle of a
triangle. The program shall compute for the third side and all angles of the triangle. The
program shall show all the sides and its corresponding angles.
2 2 2
𝐻𝑖𝑛𝑡𝑠: 𝐶𝑜𝑠𝑖𝑛𝑒 𝐿𝑎𝑤: 𝑐 = 𝑎 + 𝑏 − 2𝑎𝑏 cos 𝑐𝑜𝑠 𝐶
𝐴𝑟𝑒𝑎 = ½ 𝑎𝑏 𝑠𝑖𝑛 𝐶
PROGRAM COMMENTS
% This is a program that computes the
third side and all angles of the triangle.
a = input('Enter the value of side a '); % First input represents the value of
side a of the triangle.
b = input('Enter the value of side b '); % Second input represents the value of
side b of the triangle.
z = input('Enter the value of angle of % Third input represents the angle
side a and side b ');
between side a and b of the triangle.
c = sqrt(a^2 + b^2 - 2 * a * b * cosd(z)); % The third side, side c, of the triangle,
is computed using the two given sides
and the angle measured between them.
x = asind (a/c); % The angle of x is computed using the
product of side a and sin(a/c), measured
in degree.
y = asind (b/c); % The angle of y is computed using the
product of side a and sin(b/c), measured
in degree.
8 | ECM02L- Advanced Engineering Mathematics for ECE Lab
disp ('side c = ') % The variable name, side c, is
displayed.
disp (c) % The computed equivalent value of
side c is displayed.
disp ('the angle of side a and c is ') % The quoted statement,’ the angle
between side a and c is ‘, is displayed.
disp (x) % The computed corresponding value
of x is displayed.
disp ('the angle of side b and c is ') % The quoted statement, ‘ the angle
between side b and c is ‘ is displayed.
disp (y) % The computed corresponding value
of y is displayed.
5. Describe and differentiate the appearance of the various help commands and identify
situations when it would be most suited to be used.
help sin helpwin sin doc sin
Appearance Appearance Appearance
9 | ECM02L- Advanced Engineering Mathematics for ECE Lab
Application Application Application
It can help identify what commands to use helpwin lists topics for groups of functions doc opens the web browser displaying
in specific topics with the command help and It shows brief descriptions of the topics documentation for the functionality
and also it shows its other uses of that specified by name, such as its function.
topic and its documentation for a
description of that topic.
V. SUMMARY AND CONCLUSION
In conclusion, during this experiment, the members identified some functions of
Matlab; afterward, explored different commands, that satisfied the given mathematical
problems. As we went further through trials and errors, we acquired knowledge about
the syntaxes and the use of each command, along with various functions applied.
Moreover, using MatLab allowed us to execute basic calculations in one-lined
computation and each variable was separated using parentheses; lack of one opening
or closing parenthesis would lead to error, and lack of paired parentheses would result in
different answers. Hence, MatLab can carry out numerous functions to solve basic and
complex calculations using appropriate commands that will provide answers to the
needed problems; it can also work with inverse and trigonometric functions, either in
degrees or radians. The guide of given example commands and lectures written in this
experiment and through online materials made each step easy enough to process.
Hence, in this task, we attained knowledge and ideas about MatLab and how it functions
like a computer calculator, the right command will present an appropriate answer.
References:
● https://www.mathworks.com/help/matlab/index.html?s_tid=CRUX_lftnav
10 | ECM02L- Advanced Engineering Mathematics for ECE Lab