Contents
Basic Commands ........................................................................................ 2
Arithmetic Commands ................................................................................. 4
Trigonometric Commands............................................................................ 5
Mathematical Functions ............................................................................... 8
Array Operations....................................................................................... 10
User Input Operations ............................................................................... 15
Basic Commands
clc
Clears the command window to give a fresh start.
Fig: Before using clc Fig: After Using clc
clear ans
Removes the variable ‘ans’ from the workspace, making it no longer in use.
Fig: Before using ‘clear ans’
Fig: After using ‘clear ans’
clear all
Clears all variables from the workspace, providing a clean slate.
Fig: Before using clear all Fig: After Using clear all
%
Used to add comments in the code to explain the process.
close all
Closes all open plots and figures at once.
Arithmetic Commands
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Right division)
\ (Left division)
^ (Power)
Trigonometric Commands
sin()
Calculates the sine of an angle in radians.
cos()
Calculates the cosine of an angle in radians.
tan()
Calculates the tangent of an angle.
cot()
Calculates the cotangent of an angle.
sec()
Calculates the secant of an angle.
sind()
Calculates the sine of an angle in degrees.
cosd()
Calculates the cosine of an angle in degrees.
tand()
Calculates the tangent of an angle in degrees.
asin()
Calculates the inverse sine of a value in radians.
acos()
Calculates the inverse cosine of a value in radians.
atan()
Calculates the inverse tangent of a value in radians.
asind()
Calculates the inverse sine of a value in degrees.
acosd()
Calculates the inverse cosine of a value in degrees.
atand()
Calculates the inverse tangent of a value in degrees.
Mathematical Functions
exp()
Calculates the exponential value (e^x).
log()
Computes the natural logarithm (base e).
log10()
Computes the common logarithm (base 10).
sqrt()
Finds the square root of a number.
abs()
Returns the absolute value of a number.
rem(x, y)
Computes the remainder after dividing x by y.
gcd(x, y)
Finds the greatest common divisor of x and y.
lcm(x, y)
Finds the least common multiple of x and y.
factorial(x)
Finds the factorial of x (i.e., x!).
primes(x)
Generates all prime numbers less than x.
round()
Rounds a number to the nearest integer.
Array Operations
x = [1 2 3 4]
Creates a row matrix/vector: x = 1 2 3 4
y = [1; 2; 3; 4]
Creates a column vector: y = 1; 2; 3; 4
a = [1 2 3 4; 2 3 4 5; 3 4 5 6]
Creates a matrix with rows and columns:
a=1234
2345
3456
b = [1:5] / b = 1:5
Equivalent to b = 1:5 (row matrix): b = 1 2 3 4 5
c = [Link]
Generates a row matrix with values starting from 1 to 5, with an increment of 2: c = 1 3 5
d = linspace(1, 10, 3)
Generates a vector with three evenly spaced values between 1 and 10: d = 1 5.5 10
*a.b
Performs element-wise multiplication between two arrays. Both arrays must have the same
dimensions.
a.^2
Performs element-wise exponentiation on the array a, squaring each element.
a'
Performs transposition on the matrix degrees, converting rows to columns:
a*b
Performs matrix multiplication. The number of columns in a must match the number of
rows in b. This will throw an error if the arrays are incompatible.
table = [degrees', radians']
Creates a matrix table where the first column is the degrees values and the second column
is the corresponding radians values:
table'
Transposes the matrix table, swapping rows with columns:
User Input Operations
The example equation is:
𝑥 3 + 2𝑥 2 + 15
𝑥4
The solving code will be written on script, then it should be run: