Lesson 4.1

Basic Algebra, Logarithms, and Trig

Summary

  • Implement basic math operations with variables and numbers.
  • Use sin(), cos(), etc. to find the values of trigonometric expressions.
  • Find logarithms using log() and log10().
  • Use exp() to find the value of the exponential function, ex.

Basic Math

Basic math (addition, subtraction, multiplication, etc.) syntax in MATLAB is the same as it is in most calculators (+, -, *, /, ^). These syntax are the same whether used with variables or numbers directly. See MathWorks for a full list of MATLAB operators.

Important Note: You must use the multiplication operator everywhere you have multiplication. 7x should be 7*x. 7(x+1) should be 7*(x+1). You will get an error if you are missing a multiplication operator.

Example 1: Basic Algebra
Editor
result = 3*(4 + 1) %Example with just numbers %Defining some arbitrary variables a = 3; b = 10; c = 1; product = a*b*c %Multiplying variables difference = a - c %Subtracting variables exponent = b^a %Raising a variable to an exponent Command Window Output
result = 15 product = 30 difference = 2 exponent = 1000 [Try this code yourself with Octave Online! Click Here]

Trigonometry

Finding the values of trigonometric functions is done in MATLAB by using the conventional abbreviated version of the function; i.e., sin(), cos(), or tan(). MATLAB assumes argument of a trig function is in radians. For an input in degrees, add the letter ā€œdā€ to the end of the trigonometric function (e.g., sind()).

If you want to find the sine of every element in a matrix, A, the syntax is exactly the same as normal (math-on-paper): sin(A). The same is also true for log(), log10(), and exp() functions.

Important Note: By default MATLAB assumes argument is in radians.

Example 2: Trigonometry
Editor
angle = 180; %Defining angle as a variable to allow for easy changes radSin = sind(angle); %Finding sine of 'angle' in degrees fprintf('The value of sin(%g) is %g\n.',angle,radSin) %Note: The variable "pi" is defined in MATLAB by default radTan = tan(pi/3) %Finding tangent of pi/3 invCos = acos(1) %Finding inverse cosine of 1 Command Window Output
The value of sin(180) is 1.22465e-16. radTan = 1.7321 invCos = 0 [Try this code yourself with Octave Online! Click Here]

Logarithms

The two most common logarithmic bases, 10 and e, have explicit functions in MATLAB. The natural log (log base e) in MATLAB can be found using the function log(). Log base 10 (log10) is the function log10(). The logarithm of any real and positive base can be found with the change of base formula.

Important Note: ā€œlnā€ is NOT the function for natural log in MATLAB. log(), as noted previously, is the function for natural log.

Example 3: Logarithms
Editor
ln = log(30); %finding natural log of 30 fprintf('The natural log of 30 is %g\n.',ln) base10 = log10(7) %finding log base 10 of 7 Command Window Output
The natural log of 30 is 3.4012. base10 = 0.8451 [Try this code yourself with Octave Online! Click Here]

Exponentials

MATLAB uses the function exp() to find the value of the function f(x)=ex. As with other functions, this one can accept numbers or variables (or both). You must use exp() and NOT ex in MATLAB as the latter will return an error. e is not a pre-defined variable in MATLAB like pi is.

Example 4: Use exp() with a Variable and a Number
Editor
e4 = exp(5) %Finding e^5 x = 1; %Define an arbitrary variable x ex = exp(x) %Finding e^x Command Window Output
e4 = 148.4132 ex = 2.7183 [Try this code yourself with Octave Online! Click Here]

Next Lesson

In the next lesson, we will explore implementing unknown variables into MATLAB and how to use them to solve for the roots of an equation.