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
The plus key + is used for addition:
The dash key – is used for subtraction or to denote a negative number:
The star key * is used for multiplication:
The forward slash key / is used for division:
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
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).
The function cos(x) will calculate the cosine of the number x (x is the first input argument for the function in radians).
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).
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.
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.
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.
Scientific Notation: Inputting Large or Small Numbers
You may use scientific notation to input large and small numbers in MATLAB/Octave.
For scientific notation, a number should be written to 1 decimal place with a positive or negative exponent.
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).
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
The function sqrt(x) will calculate the square root of the number x (x is the first input argument for the function).
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).
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:
We can make complex numbers by multiplying * numbers by the letter 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:
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:
We can also calculate the logarithm using the function log(x) where x is the input argument is the number in decimal format:
In mathematics 2*3+5 is different from 2*(3+5), these rules have to be applied when using MATLAB or Octave.