Donations

£1.00

# 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`

`2+3`

# Subtraction

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

`1-2`

`2-3`

# Multiplication

The star key * is used for multiplication:

`2*3`

`3*4`

# Division

The forward slash key / is used for division:

`6/3`

# 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`

# 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 . 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(x) will calculate the sine of the number x (x is the first input argument for the function, in radians).

`sin(0)`

`sin(pi/2)`

`sin(pi)`

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

`cos(0)`

`cos(pi/2)`

`cos(pi)`

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(x) will calculate the tangent of the number x (x is the first input argument for the function in radians).

`tan(0)`

`tan(pi/2)`

`tan(pi)`

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 , and :

The inverse sin(x), cos(x) and tan(x) functions 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 :

`asin(4/5)`

`acos(4/5)`

`atan(4/3)`

For :

We know:

For :

We know:

# 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)`

`sind(90)`

`sind(180)`

`cosd(0)`

`cosd(90)`

`cosd(180)`

`tand(0)`

`tand(90)`

`tand(180)`

The inverse sind(x), cosd(x) and tand(x) functions 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 :

`asind(4/5)`

`acosd(4/5)`

`atand(4/3)`

For :

We know:

For :

We know:

# Scientific Notation: Inputting Large or Small Numbers

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

`100`

**0**=1e3

**0**.001=1e-3

`100000`

**0**=1e6

**0**.000001=1e-6

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

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

sqrt(2)

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)

# 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)`

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

`2+3*i`

# Exponential and Logarithmic Functions

For a process with 100 % return during a single growth period the rateofgrowth will equal to:

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:

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:

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 growth periods:

For continuous growth

As you can see from above the numbers begin to converge at high and they converge to a constant known as . is thus defined as the maximum rate of a continuous process.

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

In MATLAB/Octave we can type in e directly:

` e`

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

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

`log(2*e)`

# 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`

Donations

£1.00