Today’s lesson will cover adding comments to your code + built in functions in MATLAB.
First, there are two commands that can pull up full lists of built-in elementary and special functions, respectively. Built in function means the command is already “built-in” to MATLAB, so if you type it out MATLAB will know what you mean:
(a) “help elfun” : Typing “help elfun” in the command window gives a list of elementary functions, many of which I’m talking about below (eg. trig functions, square root function, etc). This list goes on for a while but here’s a screenshot of the first part of it:
(b) “help specfun” : Typing “help specfun” in the command window gives a list of special functions, many of which are not commonly used except in higher level calculations. This list goes on for a while but here’s a screenshot of the first part of it:
Adding Comments to Code
Comments are any words or code that you don’t want to put into your actual code. You will use the percent sign, %, before you begin typing, and MATLAB will color these in green. They can go anywhere in a code. Usually it is used when we want to describe each line of code without that description actually being a part of the code itself. For example, I have put a comment in green below explaining what my code does:
Common Built-In Functions and Predefined Constants
Here are two tables below. The first table has a list of some common built-in elementary functions. The second table has a list of some predefined constants you can enter into MATLAB. “Predefined” meaning that MATLAB will know what the value of these are when you enter them – for example, if you put “pi” it will know the value is 3.14159…. and so on.
Let’s use some built-in functions in some examples!
(a) Trig Functions IN RADIANS (sin, cos, tan): Here I have done examples using the trig functions: sin(x), cos(x) and tan(x). I have highlighted each example. You have to put a number in the brackets rather than x (you can even use “pi” or other predefined constants in the brackets that have number values). These values of x are read in Radians:
(b) Trig Functions IN DEGREES: Part (a) above explained how to use the trigonometric functions sin(x), cos(x) and tan(x). But MATLAB will read the numbers you put in for “x” in RADIANS. To do this in degrees (and this isn’t in Table 1 above), you need to use “sind” “cosd” and “tand.” Otherwise everything works the same way as it did for part (a). See the example below and note how the answers here are different than they are in part (a), even though I’m still using 15 in the brackets.
(c) Inverse Trig Functions IN RADIANS (arcsin, arccos, arctan): Usually, the inverse of the trig functions sin, cos, and tan are done using the exponent of -1 on a calculator. But when there’s an “arc” in front of the words, that also means to take the inverse. So arcsin(x), arccos(x), and arctan(x) respectively use the functions asin(x), acos(x), and atan(x). See this example below:
(d) Inverse Trig Functions IN DEGREES: This is the same reasoning as I explained in part (b). To use this function in degrees, we need “asind” “acosd” and “atand.” Take a look at this example:
(e) Exponential Function (and Exponents in general): To do exponents, eg. 2 to the power of 3, you need the ^ key (eg. 2^3 = 8).
To use the Exponential Function (e^x) use “exp(x)” where ‘x’ can be any value. I’ve done examples of both the exponential functions (in yellow) and using exponents in general (highlighted in blue):
(f) Square Root Function: To find the square root of a number, use “sqrt(x)”, where x can be any value. I’ve done a couple examples using it below:
(g) Natural Logarithm (which is base ‘e’ log) and Common Logarithm (base 10 log) – Natural Logarithm is more commonly seen as “ln,” and the examples are in blue. Common Log is usually seen as just “log,” and the examples are in yellow:
(h) Absolute Value Function: In math, absolute value functions make all numbers positive. So in MATLAB, if you enter a negative number, you’ll get the same number back but positive. If you enter a positive number to begin with, you’ll get the same number back, positive still. Use “abs(x)” for this. I’ve done two examples below using 10 and -10.
(i) Signum Function (aka Sign Function): I’ve never had to use this function for regular coding, it might be useful with higher level mathematical problems though. This function returns an array that is the same size as the array you input. An array is a set of numbers in rows or columns. The code for it is sign(x), and the output array will be the same size (meaning the same number of values) as array ‘x’. The values in the new array will be as follows:
(a) 1 if the corresponding element of x is greater than 0
(b) 0 if the corresponding element of x equals 0
(c) -1 if the corresponding element of x is less than 0
(d) x./abs(x) if x is a complex value
In this example below, I used ‘i’ which means imaginary number, then I used a negative number (-1), a positive number (1), and 0. This is to show the different values we get in the output array. Notice we have 4 values in the output array because we had 4 values in the array we made. Also note when we have “0.000i,” it means 0 so ignore those terms.
(j) Finding Maximum and Minimum Value: max([x]) and min([x]) are used to find the maximum and minimum values of a set of numbers, where [x] is the set of numbers. Note you need to enter the list of numbers in these square brackets [ ] which are inside the round brackets. When I didn’t use the [ ], it gave me an error. (A list of numbers in square brackets are vectors, but vectors are discussed in a future lessons):
(k) Rounding Functions – Ceiling, Floor and Round: ceil(x), floor(x) and round(x) will round numbers. ceil(x) rounds up, floor(x) rounds down, and round(x) rounds to the nearest integer, whether that’s rounding up or down. See the example below, where I input 123.45 into all three functions and you can notice how each function rounds the same value differently:
(l) Remainder After Divison: To find the remainder after dividing two values, use rem(x,y) if you are dividing x/y and finding the remainder. In this example I am dividing 24/5, so the remainder is 4:
(j) Finding the Phase Angle: To find the phase angle of an equation, use angle(x), where x is in radians by default. The first example I have highlighted is one where I used a complex number as x, then I have an example using a positive number 3, then a negative number -1.
(k) Finding the Complex Conjugate: To find the complex conjugate of a complex number (a complex number is one that has an imaginary part to it), use conj(x). It won’t work with numbers that don’t have ‘i’ in them, which is the part that makes a complex number imaginary!