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.
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:
|\||Back Slash||Left Matrix Division|
|.*||Dot Asterisk||Array multiplication (element-wise)|
|./||Dot Slash||Right array divide (element-wise)|
|.\||Dot Back Slash||Left array divide (element-wise)|
|.^||Dot Caret||Array power (element-wise)|
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.
- Parentheses ()
- Power (^)
- Multiplication (*), right division (/), left division (\)
- Addition (+), subtraction (-)
MATLAB has all of the usual mathematical functions found on a scientific calculator including square root, logarithm, and sine.
Practice the following examples to familiarize yourself with the common mathematical functions. Be sure to read the relevant
docpages for functions that are not self explanatory.
formatfunction is used to control how the numeric values are displayed in the Command Window. The
shortformat is set by default and the numerical results are displayed with 4 digits after the decimal point (see the examples above). The
longformat produces 15 digits after the decimal point.
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
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:
Declaration of a Row Vector
Elements of a row vector are separated with blanks or commas.
Declaration of a Column Vector
Elements of a column vector is ended by a semicolon:
Declaration of a Matrix
Matrices are typed in rows first and separated by semicolons to create columns. Consider the examples below:
Systems of linear equations are very important in engineering studies. In the course of solving a problem, we often reduce the problem to simultaneous equations from which the results are obtained. As you learned earlier, MATLAB stands for Matrix Laboratory and has features to handle matrices. Using the coefficients of simultaneous linear equations, a matrix can be formed to solve a set of simultaneous equations.
In the preceding section, we briefly learned about how to use MATLAB to solve linear equations. Equally important in engineering problem solving is the application of polynomials. Polynomials are functions that are built by simply adding together (or subtracting) some power functions. (see Wikipedia).
The coeffcients of a polynominal are entered as a row vector beginning with the highest power and including the ones that are equal to 0.
We can evaluate a polynomial
pfor a given value of
xusing the syntax
polyval(p,x)where p contains the coefficients of polynomial and x is the given number.
Consider the following equation:
Probably you have solved this type of equations numerous times. In MATLAB, we can use the
rootsfunction to find the roots very easily.
Splitting a Statement
You will soon find out that typing long statements in the Command Window or in the the Text Editor makes it very hard to read and maintain your code. To split a long statement over multiple lines simply enter three periods "..." at the end of the line and carry on with your statement on the next line.
Comments are used to make scripts more "readable". The percent symbol % separates the comments from the code. Examine the following examples:
|sum||Sum of array elements|
|prod||Product of array elements|
|log10||Common logarithm (base 10)|
|max||Maximum elements of array|
|min||Minimum elements of array|
|mean||Average or mean value of arrays|
|( )||Prioritize operations|
|[ ]||Construct array|
|:||Specify range of array elements|
|,||Row element separator in an array|
|;||Column element separator in an array|
|...||Continue statement to next line|
|.||Decimal point, or structure field separator|
|%||Insert comment line into code|
Summary of Key Points
- MATLAB has the common functions found on a scientific calculator and can be operated in a similar way,
- MATLAB can store values in variables. Variables are case sensitive and some variables are reserved by MATLAB (e.g.
- Variable Editor can be used to enter or manipulate matrices,
- The coefficients of simultaneous linear equations and polynomials are used to form a row vector. MATLAB then can be used to solve the equations,
formatfunction is used to control the number of digits displayed,
- Three periods "..." at the end of the line is used to split a long statement over multiple lines,
- The percent symbol % separates the comments from the code, anything following % symbol is ignored by MATLAB.