MATLAB is an acronym or Matrix Laboratory. MATLAB is a numerical computing environment. It uses a proprietary computer language to allow developers to create procedural and object-oriented modules to perform computations both imperatively and declaratively .
To gain a complete understanding of the MATLAB environment we need to start at a basic level. Most modern computing devices such as your smart phone, tablet or personal computer are made up of several dozen electronic devices. The most basic are memory and processors . The memory is a storage space for the computing device; a place to store binary digits. Some memory is read only and is designed to never change the values placed in it during manufacture. Most memory is random access and the binary values stored in it change throughout the course of using the computing device. Most modern computer devices contain several million (or billion) individually addressable memory locations. The computing device is designed to be able to access each of these addressable memory locations. However, the addressing scheme is not designed to be readable by people and most of us would find remembering several thousand of the ones we are most interested in an impossible task.
High level programming languages relieve us of the burden of remembering several thousand memory addresses by allowing us to give memory addresses names . These names are text strings that we define in our programming language of choice and use in our computations. These names are called variables . Groups of memory locations can be organized into data structures so that a single name can refer to one memory location or several million contiguous memory locations at once. In the MATLAB environment all of the names of our variables are displayed in the Workspace in the Integrated Development Environment (IDE).
Figure 1
In the image above a variable $\mathbf{x}$ has been created and assigned the value $5$. The name of the variable appears in the Workspace along with its value. The value of the variable is called the state of the variable. The state of the Workspace consists of the values of all variables currently defined by us in the MATLAB environment. A state space consists of the values of all of the variables that exist in a particular context . The only context we are concerned with right now is the global context ; the total environment of the MATLAB IDE (Integrated Development Environment). We call the state of the MATLAB IDE the global state . The MATLAB Workspace displays all of the user-defined global variables and from it we can determine the values contained in the memory locations of all of these global variables.
In computer science and computational programming we refer to the state of our variables as data . Data is a broader term that mathematically represents physical or abstract objects. In a computing device data is just a sequence of binary digits. The computer scientist or numerical programmer assigns meaning to thes binary sequences. The Institute of Electrical Engineers spent several decades developing the current definitions of binary sequences that represent common data types . Common or builtin data types are data structures that are defined by the programming language creators. MathWorks MATLAB builtin data types include double precision floating point numbers . The most recent IEEE floating point standard is a complicated document defining the binary representation of floating point numbers and the operations that can be used with them. We will discuss several of the builtin data types and use them in our computational programming this semester.
There are two kinds of binary sequences that can be found in a memory location; data and instructions . A processor or central processing unit consists of memory location called registers . Some registers are designed to contain data. Several registers contain special binary sequences called instructions . Just as with memory locations, state of the processor consists of the values in all of the registers of the processor. Instructions cause the processor to change its state in clearly defined operations . These operations are exploited by a computer scientist to represent mathematical results that people readily recognize. Instructions are given the names of the human readable operations they perform. Some instructions move values from one memory location to another. Some instructions add the values in two memory locations. The set of all of the instructions a processor contructed to perform comprises a machine language . People use high level programming languages like MATLAB because only a few people invest the time necessary to read machine language. A single statement in MATLAB typically results in several dozen machine language instructions. Only a few people need to spend time learning, reading and writing machine language when modern high level languages are available.
An imperative statement instructs the environment to complete a computation or change the state of the environment directly. The machine language used by the processors of our computational devices are imperative laguanges. In figure 1 you see an imperative statement in the command window, $x = 5$. After entering the statement and pressing the enter key, the MATLAB environment performs the instructions in the statement. The environment allocates a double precision floating point memory location, assigns the name $x$ to the memory location in the global Workspace and places the binary sequence for $5$ in the memory location. We call this imperative statement an assignment statement because it assigns the value $5$ to the memory location called $x$. We usually obscure the difference between the label $x$ and the memory location it represents. In this way we abstractly say that "$x$ is assigned the value of 5".
We will talk about
object oriented
programming and
declarative
statements in later sections. Imperative MATLAB statements plays a significant role in computational programming. Many of the equations that we write in algebra and calculus can be written as MATLAB imperative statements.
The two primary components of the computer laboratory of the MATLAB IDE are the global Workspace and the
Command Window
. A computational programmer enters a MATLAB imperative statement into the command window and presses the enter key. The MATLAB environment executes the statement. Usually the results of the statement are immediately displayed in the command window. A semicolon after an imperative statement suppresses output of the result to the command window. In figure 1, the imperative statement
$$ >> x = 5 $$
results in the output of the variable name followed by the value assigned to the variable. If instead the computational programmer uses a semicolon at the end of the statement
$$ >> x = 5;$$
then only the prompt for the next statement is produced. Notice that the variable is still created and still has the value $5$. The Workspace displays the result of the computation even though the output of the result was not displayed in the Command Window.
Figure 2
The IDE allows one to display the
Command History
. The Command History contains a list of the previous commands entered into the Command Window. One may copy the text of commands from the Command History and paste them into the Command Window or into a text editor.
Figure 3
MATLAB is an interpreted language because the MATLAB interpreter parses a single MATLAB statement or MATLAB code block and executes the statement or code block before moving on to the next one. This makes the computational programmer's work flow less stressful because one may execute a MATLAB statement and immediately see the result. One can copy a successful statement into a script or function and immediately execute the modified code and see the result.
A compiled language is converted to machine code and bundled into a separate executable program. The computational programmer then runs the program and views the results. Compilation may include several steps that take significant time to complete. For example, most modern compilers analyze code paths in a function and optimize the function to run efficiently on a target processor. While this results in much faster runtimes, the extra steps necessary to create a separate executable can slow the process of developing a solution.
An interpreted language allows one to experiment with statements to achieve a desired result. One may change the syntax of a statements and execute them to see the result without a separate build process to obtain an executable. MATLAB is named for this ability to investigate numerical algorithms and promptly obtain the result. Any attempts at expository writing will reveal that some prose fails to deliver the intent of the author. The author edits a sentence or paragraph to convey the correct semantical meaning. A programmer occasionally experiences the same revelation after executing a programming statement.
A bug is a generic term for statements that fail to obtain the computational result the programmer intended. There are two types of bugs, syntactical errors and semantic errors. A syntactical error occurs when a programming statement fails to conform to the syntax of the programming language. These types of bugs are easy to find and the interpreter or compiler usually reports the exact position of a syntax error to the developer. Determining the source of a semantic error in a script or function of several dozen statements requires patience and a special tool.
A compiled language also needs its own integrated development environment and a separate debugger to find semantic errors in the source code of an executable. A debugger is a program that allows one to step through a program instruction-by-instruction and determine from the change in state of the computer, whether the program steps result in the computations the programmer intended. This can be problematic for a compiled program as basic debuggers step through machine instructions. In a high level language, a single statement in the human-readable high level programming language can result in dozens, and even thousands, of machine language instructions. We distinguish the high level programming statements from the machine language instructions by calling the high level language statements source code . A source level debugger uses clues left by the compiler in the executable to associate each source code instruction with the machine language instructions produced. This allows one to step through the executable statement-by-statement however, sacrifices many of the advantages of the compilation process, such as optimization that results in faster execution.
The MATLAB IDE incorporates a source level debugger without separate software to accomplish this task. An interpreted language already executes each code bock or statement individually so step-by-step execution occurs at the source code level. No special compilation is required for source level debugging so any MATLAB script or function can be analyzed via step-by-step source code debugging. After executing a single MATLAB statement in a script or function, the full resources of the MATLAB IDE are available. One can view the entire global state of the Workspace, execute MATLAB instructions and change the values of MATLAB variables to aid in the debugging process.
One of the strengths of the MATLAB IDE is the large number of built-in functions and MATLAB libraries of programs supplied by MathWorks with their product. In this course we will explore mathematical computing and discover existing libraries and built-in functions that we can use to create our own MATLAB scripts to execute our computational algorithms.
You instructor will demonstrate several built-in functions to create and manipulate numbers and arrays of numbers in our first week. You can use the MATLAB built-in function
help
to obtain documentation on any MATLAB built-in function. The following figure demonstrates obtaining help for the MATLAB built-in function
zeros
. The function
zeros
returns an array of floating point numbers of the specified size with the value zero assigned to every member of the array.
Figure 4 (Notice that the command also appears in the Command History.)