# Getting Started

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


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

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


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


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

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


### Linear Equations

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.  


### Polynomials

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. 

### The polyval Function

We can evaluate a polynomial p for a given value of x using the syntax polyval(p,x) where p contains the coefficients of polynomial and x is the given number.

### The roots Function

Consider the following equation:
Probably you have solved this type of equations numerous times. In MATLAB, we can use the roots function 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

Comments are used to make scripts more "readable". The percent symbol % separates the comments from the code. Examine the following examples:



### Basic Operations


### Special Characters


### Summary of Key Points

1. MATLAB has the common functions found on a scientific calculator and can be operated in a similar way,
2. MATLAB can store values in variables. Variables are case sensitive and some variables are reserved by MATLAB (e.g. pi stores 3.1416),
3. Variable Editor can be used to enter or manipulate matrices,
4. The coefficients of simultaneous linear equations and polynomials are used to form a row vector. MATLAB then can be used to solve the equations,
5. The format function is used to control the number of digits displayed,
6. Three periods "..." at the end of the line is used to split a long statement over multiple lines,
7. The percent symbol % separates the comments from the code, anything following % symbol is ignored by MATLAB.