A Brief Introduction to Engineering Computation with MATLAB by Serhat Beyenir - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Chapter 2Getting Started

2.1Essentials*

Essential skills to use MATLAB effectively.

MATLAB Essentials

Learning a new skill, especially a computer program in this case, can be overwhelming. However, if we build on what we already know, the process can be handled rather effectively. In the preceding chapter we learned about MATLAB Graphical User Interface (GUI) and how to get help. Knowing the GUI, we will use basic math skills in MATLAB to solve linear equations and find roots of polynomials in this chapter.

Basic Computation

Mathematical Operators

The evaluation of expressions is accomplished with arithmetic operators as we use them in scientific calculators. Note the addtional operators shown in the table below:

Table 2.1. Operators
OperatorNameDescription
+PlusAddition
-MinusSubtraction
*AsteriskMultiplication
/Forward SlashDivision
\Back SlashLeft Matrix Division
^CaretPower
.*Dot AsteriskArray multiplication (element-wise)
./Dot SlashRight array divide (element-wise)
.\Dot Back SlashLeft array divide (element-wise)
.^Dot CaretArray power (element-wise)

The backslash operator is used to solve linear systems of equations, see the section called “Linear Equations”.

Matrix is a rectangular array of numbers and formed by rows and columns. For example _autogen-svg2png-0001.png. In this example A consists of 4 rows and 4 columns and therefore is a 4x4 matrix. (see Wikipedia).

Row vector is a special matrix that contains only one row. In other words, a row vector is a 1xn matrix where n is the number of elements in the row vector. _autogen-svg2png-0002.png

Column vector is also a special matrix. As the term implies, it contains only one column. A column vector is an nx1 matrix where n is the number of elements in the column vector. _autogen-svg2png-0003.png

Array operations refer to element-wise calculations on the arrays, for example if x is an a by b matrix and y is a c by d matrix then x.*y can be performed only if a=c and b=d. Consider the following example, x consists of 2 rows and 3 columns and therefore it is a 2x3 matrix. Likewise, y has 2 rows and 3 columns and an array operation is possible. _autogen-svg2png-0004.png and _autogen-svg2png-0005.png then _autogen-svg2png-0006.png

Example 2.1

The following figure illustrates a typical calculation in the Command Window.

Example
Figure 2.1
Basic arithmetic in the command window.

Operator Precedence

MATLAB allows us to build mathematical expressions with any combination of arithmetic operators. The order of operations are set by precedence levels in which MATLAB evaluates an expression from left to right. The precedence rules for MATLAB operators are shown in the list below from the highest precedence level to the lowest.

  1. Parentheses ()

  2. Power (^)

  3. Multiplication (*), right division (/), left division (\)

  4. Addition (+), subtraction (-)

Mathematical Functions

MATLAB has all of the usual mathematical functions found on a scientific calculator including square root, logarithm, and sine.

Typing pi returns the number 3.1416. To find the sine of pi, type in sin(pi) and press enter.

The arguments in trigonometric functions are in radians. Multiply degrees by pi/180 to get radians. For example, to calculate sin(90), type in sin(90*pi/180).

Warning

In MATLAB log returns the natural logarithm of the value. To find the ln of 10, type in log(10) and press enter, (ans = 2.3026).

Warning

MATLAB accepts log10 for common (base 10) logarithm. To find the log of 10, type in log10(10) and press enter, (ans = 1).

Practice the following examples to familiarize yourself with the common mathematical functions. Be sure to read the relevant help and doc pages for functions that are not self explanatory.

Example 2.2

Calculate the following quantities:

  1. _autogen-svg2png-0007.png,

  2. 50.5−1

  3. _autogen-svg2png-0009.png for d=2

MATLAB inputs and outputs are as follows:

  1. _autogen-svg2png-0010.png is entered by typing 2^3/(3^2-1) (ans = 1)

  2. 50.5−1 is entered by typing sqrt(5)-1 (ans = 1.2361)

  3. _autogen-svg2png-0012.png for d=2 is entered by typing pi/4*2^2 (ans = 3.1416)

Example 2.3

Calculate the following exponential and logarithmic quantities:

  1. 2

  2. ln(510)

  3. log(105)

MATLAB inputs and outputs are as follows:

  1. exp(2) (ans = 7.3891)

  2. log((5^10)) (ans = 16.0944)

  3. log10(10^5) (ans = 5)

Example 2.4

Calculate the following trigonometric quantities:

  1. _autogen-svg2png-0016.png

  2. tan(45)

  3. sin(π)+cos(45)

MATLAB inputs and outputs are as follows:

  1. cos(pi/6) (ans = 0.8660)

  2. tan(45*pi/180) (ans = 1.0000)

  3. sin(pi)+cos(45*pi/180) (ans = 0.7071)

The format Function

The format function is used to control how the numeric values are displayed in the Command Window. The short format is set by default and the numerical results are displayed with 4 digits after the decimal point (see the examples above). The long format produces 15 digits after the decimal point.

Example 2.5

Calculate _autogen-svg2png-0019.png and display results in short and long formats.

The short format is set by default:

>> theta=tan(pi/3)

theta =

    1.7321

>> 

And the long format is turned on by typing format long:

>> theta=tan(pi/3)

theta =

    1.7321

>> format long
>> theta

theta =

   1.732050807568877

Variables

In MATLAB, a named value is called a variable. MATLAB comes with several predefined variables. For example, the name pi refers to the mathematical quantity π, which is approximately pi ans = 3.1416

Warning

MATLAB is case-sensitive, which means it distinguishes between upper- and lowercase letters (e.g. data, DATA and DaTa are three different variables). Command and function names are also case-sensitive. Please note that when you use the command-line help, function names are given in upper-case letters (e.g., CLEAR) only to emphasize them. Do not use upper-case letters when running functions and commands.

Declaring Variables

Variables in MATLAB are generally represented as matrix quantities. Scalars and vectors are special cases of matrices having size 1x1 (scalar), 1xn (row vector) or nx1 (column vector).

Declaration of a Scalar

The term scalar as used in linear algebra refers to a real number. Assignment of scalars in MATLAB is easy, type in the variable name followed by = symbol and a number:

Example 2.6

a = 1

Example1
Figure 2.2
Assignment of a scalar quantity.
Declaration of a Row Vector

Elements of a row vector are separated with blanks or commas.

Example 2.7

Let's type the following at the command prompt:

b = [1 2 3 4 5]

Example2
Figure 2.3
Assignment of a row vector quantity.

We can also use the Variable Editor to assign a row vector. In the menu bar, select File > New > Variable. This action will create a variable called unnamed which is displayed in the workspace. By clicking on the title unnamed, we can rename it to something more descriptive. By double-clicking on the variable, we can open the Variable Editor and type in the values into spreadsheet looking table.

Example2a
Figure 2.4
Assignment of a row vector by using the Variable Editor.
Declaration of a Column Vector

Elements of a column vector is ended by a semicolon:

Example 2.8

c = [1;2;3;4;5;]

Example3
Figure 2.5
Assignment of a column vector quantity.

Or by transposing a row vector with the ' operator:

c = [1 2 3 4 5]'

Example3a
Figure 2.6
Assignment of a column vector quantity by transposing a row vector with the ' operator.

Or by using the Variable Editor:

Example3b
Figure 2.7
Assignment of a column vector quantity by using the Variable Editor.
Declaration of a Matrix

Matrices are typed in rows first and separated by semicolons to create columns. Consider the examples below:

Example 2.9

Let us type in a 2x5 matrix:

d = [2 4 6 8 10; 1 3 5 7 9]

Example4
Figure 2.8
Assignment of a 2x5 matrix.