# Introduction

This will show you how to use MATLAB/Octave as a basic calculator using scalars (individual numbers) and revise some of the underlying mathematics where necessary. I am using Octave which is the open source version, available for everyone to use. The user interface of MATLAB and functionality is superior (Octave seems to be like MATLAB 10 years ago) however they use the same underlying language and I have wrote these guides taking you through the basics and thus these guides are equally applicable to both products.

Let’s get started.

# The Command Window # Addition

The plus key + is used for addition:

1+2 $\displaystyle 1+2=3$

2+3 $\displaystyle 2+3=5$

# Subtraction

The dash key is used for subtraction or to denote a negative number:

1-2 $\displaystyle 1-2=-1$

2-3 $\displaystyle 2-3=-1$

# Multiplication

The star key * is used for multiplication:

2*3 $\displaystyle 2*3=6$

3*4 $\displaystyle 3*4=12$

# Division

The forward slash key / is used for division:

6/3 $\displaystyle 6/3=\frac{6}{3}=2$

# The function pi

π is the ratio between circumference and diameter in all circles and the angles of a circle add up 2π. The function pi returns the value of pi. By default MATLAB/Octave displays values using 5 significant figures however internally it will calculate the values up to 16 digits of precision

pi $\displaystyle pi=\pi =3.1416$

# Trigonometry in Radians

The sums of the angles in a triangle sum up to π. The Trigonometry relationships, can be understood by looking at a right angle triangle. One can see that cosine is a phase shifted sine function by pi. Drawing a circle, centred at the origin of radius 1, with a right angle triangle in the first quadrant, lets one also have a look at the sine and cosine function in more detail. The Hypotenuse (H) is therefore equal to the radius of the circle and fixed at 1, meaning the Adjacent (A) and Opposite (O) change with respect to the angle $\displaystyle \alpha$. Although we only have this triangle drawn in the first quadrant we can see by the symmetry of the circle that the second quadrant will follow the same behaviour as the first quadrant but the sign of the Adjacent (A) is reversed, meaning the cosine function in this quadrant will be negative. In the third quadrant, it will again follow a similar behaviour but this time both the sign of the Adjacent (A) and Opposite (O) are reversed meaning both the sine and cosine will be negative here. Finally the fourth quadrant will also follow a similar behaviour but this time the Opposite (O) is negative meaning the sine will be negative here. A plot of sin(x) and cos(x):

The function sin(xwill calculate the sine of the number x (x is the first input argument for the function, in radians).

sin(0) $\displaystyle \sin (0)=0$

sin(pi/2) $\displaystyle \sin (\text{pi}/2)=\sin \left( {\frac{\pi }{2}} \right)=1$

sin(pi) $\displaystyle \sin (\text{pi})=\sin ({\pi})=1.2246\text{e}-16$

The function cos(xwill calculate the cosine of the number x (x is the first input argument for the function in radians).

cos(0) $\displaystyle \cos (0)=1$

cos(pi/2) $\displaystyle \cos (\text{pi}/2)=\cos \left( {\frac{\pi }{2}} \right)=\text{6}\text{.1230e}-17$

cos(pi) $\displaystyle \cos (\text{pi})=\cos (\pi )=-1$

We know the values of sin(pi)=0 and cos(pi/2)=0 however we are given the very small value 1.2246e-16 and 6.1230e-17 as a consequence of Octave/MATLAB using up to 16 digits of precision by default.

The function tan(xwill calculate the tangent of the number x (x is the first input argument for the function in radians).

tan(0) $\displaystyle \tan (0)=0$

tan(pi/2) $\displaystyle \cos (\text{pi}/2)=\cos \left( {\frac{\pi }{2}} \right)=\text{6}\text{.6331e+16}$

tan(pi) $\displaystyle \tan (\text{pi/2})=\tan \left( {\frac{\pi }{2}} \right)=\text{1}\text{.1230e-17}$

Likewise we know the values of tan(pi/2)=∞ and tan(pi)=0 but we are given the very high number 1.6331e+16 and very low number 1.1230e-17 respectively.

Suppose we are given the following right angle triangle and want to calculate the angles $\displaystyle \alpha$ $\displaystyle \beta$ and $\displaystyle \gamma$: The inverse sin(x), cos(x) and tan(xfunctions asin(y)acos(y) and asin(y) can be used with input arguments y=O/H, y=A/H and y=O/A respectively.

For $\displaystyle \alpha$:

asin(4/5) $\displaystyle \text{asin}(4/5)=0.92730$

acos(4/5) $\displaystyle \text{acos}(3/5)=0.92730$

atan(4/3) $\displaystyle \text{atan}(4/3)=0.92730$

For $\displaystyle \beta$:

We know: $\displaystyle \beta =\frac{\pi }{2}-\alpha =\text{0}\text{.64350}$

For $\displaystyle \gamma$:

We know: $\displaystyle \gamma =\frac{\pi }{2}$

# Trigonometry in Degrees

sin(x), cos(x) and tan(x) are functions which work with input arguments in radians, if we want to use a different measurement system degrees instead, we can use the functions sind(x), cosd(x) and tand(x) which work with input arguments in degrees.

sind(0) $\displaystyle \text{sind}(0)=0$

sind(90) $\displaystyle \text{sind}(90)=1$

sind(180) $\displaystyle \text{sind}(180)=0$

cosd(0) $\displaystyle \text{cosd}(0)=1$

cosd(90) $\displaystyle \text{cosd}(90)=0$

cosd(180) $\displaystyle \text{cosd}(180)=1$

tand(0) $\displaystyle \text{tand}(0)=0$

tand(90) $\displaystyle \text{tand}(90)=\text{Inf}$

tand(180) $\displaystyle \text{tand}(180)=0$ The inverse sind(x), cosd(x) and tand(xfunctions asind(y)acosd(y) and asind(y) can be used with input arguments y=O/H, y=A/H and y=O/A respectively.

For $\displaystyle \alpha$:

asind(4/5) $\displaystyle \text{asind}(4/5)=53.130$

acosd(4/5) $\displaystyle \text{acosd}(3/5)=53.130$

atand(4/3) $\displaystyle \text{atand}(4/3)=53.130$

For $\displaystyle \beta$:

We know: $\displaystyle \beta =90-\alpha =\text{36}\text{.870}$

For $\displaystyle \gamma$:

We know: $\displaystyle \gamma =90$

# Scientific Notation: Inputting Large or Small Numbers

You may use scientific notation to input large and small numbers in MATLAB/Octave. 1000=1e3

0.001=1e-3 $\displaystyle 1000-1\text{e}3=0$ $\displaystyle 0.001-1\text{e}-3=0$

1000000=1e6

0.000001=1e-6 $\displaystyle 1000000-1\text{e}6=0$ $\displaystyle 0.000001-1\text{e}-6=0$

For scientific notation, a number should be written to 1 decimal place with a positive or negative exponent.

123.456=1.23456e2

1.23456 the number before the exponent is known as the mantissa and 2 is known as the exponent. By default a base of 10 is used (10 different numbers 0,1,2,3,4,5,6,7,8,9). 0.000123456=1.23456e-4 1.23456 is the mantissa and -4 is the exponent. Once again a base of 10 is used.

# Powers and Roots

The hat key ^ is used to denote an exponent

2^-1 $\displaystyle {{2}^{{-1}}}=\frac{1}{{{{2}^{1}}}}=0.5$

The function sqrt(x) will calculate the square root of the number x (x is the first input argument for the function).

sqrt(2) $\displaystyle \text{sqrt}(2)=\sqrt{2}=1.4142$

The function nthroot(x,n) will calculate the nthroot of the number x (x is the first input argument for the function and n is the second input argument denoting the power of the nthroot).

nthroot(27,3) $\displaystyle \text{nthroot}(27,3)=\sqrt{{27}}=3$

# Imaginary Numbers

If we take the sqrt(x) function and take the input argument x to be a negative number for instance -1, we will get a complex number:

sqrt(-1) $\displaystyle \text{sqrt}(-1)=0+i$

We can make complex numbers by multiplying * numbers by the letter i:

2+3*i $\displaystyle 2+3*i=2+3i$

# Exponential and Logarithmic Functions

For a process with 100 % return during a single growth period the rateofgrowth will equal to: $\displaystyle \text{rateofgrowth=}{{\left( {1+\frac{{100\%}}{1}} \right)}^{1}}={{\left( {1+\frac{1}{1}} \right)}^{1}}=2$

In other words after 1 growth period with 100 % growth you get your starting investment value of 1 plus an additional value of 1 return.

After two growth periods: $\displaystyle \text{rateofgrowth}={{\left( {1+\frac{{100\%}}{2}} \right)}^{2}}={{\left( {1+\frac{1}{2}} \right)}^{2}}=2.25$

If instead you get an investment of 100 % growth over two growth periods, in the first of the two growth periods you will have your initial investment of 1, plus an additional value of 0.5 return. This means you start the second growth period with 1.5 and then after the second growth period you gain an additional value of 0.75 return. Thus you end up with your starting investment of 1 plus an additional value of 1.25.

After three growth periods: $\displaystyle \text{rateofgrowth}={{\left( {1+\frac{{100\%}}{3}} \right)}^{3}}={{\left( {1+\frac{1}{3}} \right)}^{3}}=2.3704$

If instead you get an investment of 100 % growth over three growth periods, in the first of the three growth periods you will have your initial investment of 1, plus an addition value of 0.3333 return. In the second growth period you thus start with 1.3333 and get 0.4444 return and this means in the third growth period, you start with 1.7777 and get 0.5925 in return. Thus you end up with your starting investment of 1 plus an additional value of 1.3702 (1.3704 if the calculation is carried out to more significant figures).

And after $\displaystyle n$ growth periods: $\displaystyle \text{rateofgrowth}={{\left( {1+\frac{{100\%}}{n}} \right)}^{n}}={{\left( {1+\frac{1}{n}} \right)}^{n}}$

For continuous growth $\displaystyle n\to \infty$ $\displaystyle \begin{array}{*{20}{c}} n & {{{{\left( {1+\frac{1}{n}} \right)}}^{n}}} \\ 1 & {2.0000} \\ 2 & {2.2500} \\ 3 & {2.3704} \\ 4 & {2.4414} \\ 5 & {2.4883} \\ 6 & {2.5216} \\ 7 & {2.5465} \\ 8 & {2.5658} \\ 9 & {2.5812} \\ {10} & {2.5937} \\ {100} & {2.7048} \\ {1000} & {2.7169} \\ {10000} & {2.7181} \\ {100000} & {2.7183} \\ {1000000} & {2.7183} \\ {10000000} & {2.7183} \\ {100000000} & {2.7183} \\ {1000000000} & {2.7183} \\ {10000000000} & {2.7183} \end{array}$

As you can see from above the numbers begin to converge at high $\displaystyle n$ and they converge to a constant known as $\displaystyle e$. $\displaystyle e$ is thus defined as the maximum rate of a continuous process. $\displaystyle e={{\lim }_{{_{{_{{n\to \infty }}}}}}}{{\left( {1+\frac{1}{n}} \right)}^{n}}$

The natural logarithm can be used to calculate the amount of product one has after $\displaystyle x$ units of time at continuous growth.

In MATLAB/Octave we can type in e directly:

 e $\displaystyle \text{e}=\text{2}\text{.7183}$

We can also convert decimal numbers to the base e using the function exp(x) where x is the input argument is the number in decimal format:

exp(1) $\displaystyle \text{exp(1)}=\text{2}\text{.7183}$

We can also calculate the logarithm using the function log(x) where x is the input argument is the number in decimal format:

log(e) $\displaystyle \text{log(e)}=\text{1}$

log(2*e) $\displaystyle \text{log(e }\!\!\hat{\ }\!\!\text{ 2)}=2\log (\text{e})=2*\text{1=2}$

# Precedence

In mathematics 2*3+5 is different from 2*(3+5), these rules have to be applied when using MATLAB or Octave.

2*3+5=6+5=11

2*(3+5)=2*8=16 $\displaystyle \begin{array}{*{20}{c}} {\text{Rank}} & {} & {} \\ 1 & {(\text{ })} & {} \\ 2 & \hat{\ } & {} \\ 3 & * & / \\ 4 & + & - \end{array}$

Advertisements