MATLAB

From Free net encyclopedia

Revision as of 07:48, 21 April 2006; view current revision
←Older revision | Newer revision→

Template:Infobox Software

MATLAB is a numerical computing environment and programming language. Created by The MathWorks, MATLAB allows easy matrix manipulation, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs in other languages. Although it specializes in numerical computing, an optional toolbox interfaces with the Maple symbolic engine, making it a full computer algebra system. It is used by more than one million people in industry and academia. The costs are $2000 for a commercial license without any toolboxes, and US$100 for an academic license with a limited set of Toolboxes.

Contents

History

Short for "MATrix LABoratory", MATLAB was invented in the late 1970s by Cleve Moler, then chairman of the computer science department at the University of New Mexico. He designed it to give his students access to LINPACK and EISPACK without having to learn Fortran. It soon spread to other universities and found a strong audience within the applied mathematics community. Jack Little, an engineer, was exposed to it during a visit Moler made to Stanford University in 1983. Recognizing its commercial potential, he joined with Moler and Steve Bangert. They rewrote MATLAB in C and founded The MathWorks in 1984 to continue its development. These rewritten libraries were lovingly known as JACKPAC. MATLAB was first adopted by control design engineers, Little's specialty, but quickly spread to many other domains. It is now also used in education, in particular the teaching of linear algebra and numerical analysis.

Syntax

MATLAB's M-Code (or simply m) is primarily value oriented. Unlike languages like Java and [[C++]], m is not statically typed, meaning that variables themselves do not have types, only the runtime values stored in those variables have types, more like PHP or Javascript.

Variables

Variables are defined with the assignment operator, '='. For instance:

x = 17

defines a variable named x, and assigns it a value of 17. Values can come from literal values such as string constants or numeric immediates, or from the values of other variables, or from the output of a function.

Vectors/Matrices

MATLAB is the "Matrix Laboratory", and so provides many convenient ways for creating arrays of various dimensions. In the MATLAB vernacular, a 'vector refers to a one dimensional (1×N or N×1) matrix, commonly referred to as an array in other programming languages. a matrix generally refers to a multi-dimensional matrix, that is, a matrix with more than one dimension, for instance, an N×M, an N×M×L, etc., where N, M, and L are greater than 1. In other languages, such a matrix might be referred to as an array of arrays, or array of array of arrays, etc.

MATLAB provides a simple way to define simple arrays using the syntax: init:increment:terminator. For instance:

array = 1:2:9

array = 
        1 3 5 7 9

defines a variable named array (or assigns a new value to an existing variable with the name array) which is an array consisting of the values 1, 3, 5, 7, and 9. That is, the array starts at 1, the init value, and each value increments from the previous value by 2 (the increment value), and stops once it reaches but not exceeding 9 (9 being the value of the terminator).

array = 1:3:9

array = 
        1 4 7

the increment value can actually be left out of this syntax (along with one of the colons), to use a default value of 1.

ari = 1:5

ari = 
      1 2 3 4 5 

assigns to the variable named ari an array with the values 1, 2, 3, 4, and 5, since the default value of 1 is used as the incrementer.

Semicolon

The semicolon (';') has somewhat unexpected use in m; it is not required to terminate commands as in Java, C++ and others. Instead, the semicolon is included to prevent the output of the line from being echoed to the standard out, which is what happens if you don't include the semicolon at the end of the line.

Code Snippets

This code, excerpted from the function magic.m, creates a magic square M for odd values of n.

[J,I] = meshgrid(1:n);
A = mod(I+J-(n+3)/2,n);
B = mod(I+2*J-2,n);
M = n*A + B + 1;

Note that this code performs operations on vectors and matrices without the use of "for" loops. Idiomatic MATLAB programs usually operate on whole arrays at a time. The MESHGRID utility function above creates arrays like these:

J =

     1     2     3
     1     2     3
     1     2     3


I =

     1     1     1
     2     2     2
     3     3     3

Most scalar functions can also be used on arrays, and will apply themselves in parallel to each element. Thus mod(2*J,n) will (scalar) multiply the entire J array with 2, before reducing each element modulo n.

MATLAB does include standard "for" and "while" loops, but using MATLAB's vectorized notation often produces code that is easier to read and faster to execute.

Criticism

MATLAB itself is a proprietary product of The MathWorks. Unlike common programming languages such as C or FORTRAN, the MATLAB language is not managed or specified by a 3rd party standards committee such as ANSI. Obtaining a fully compatible and up to date MATLAB platform requires purchasing the product. Some programs are available that implement significant subsets of the MATLAB programming language (see the list of numerical analysis software), but these are not 100% compatible and do not include various domain-specific tools. Consequently, MATLAB customers may be subject to vendor lock-in.

MATLAB was originally implemented in FORTRAN and later re-written in C. The language shows this mixed heritage with a sometimes erratic syntax: neither C nor FORTRAN, but a combination of both. This mixed syntax can lead to interpretation problems. For example, the expression:

   y = f(x)

could either refer to function f with argument x or the x value of matrix f. Although this ambiguous syntax can facilitate a switch between a procedure and a lookup table, both of which are known as functions in Mathematics, a very careful reading of the code is required to establish the original intent. Similar difficulties surround the * and ' operators.

Though other datatypes are available, the default is a matrix of doubles. This array of numbers is devoid of important attributes required by real world data such as engineering units or sampling rates. Although time and date markers were added in SP3 with the time series object, the lack of sample rate information is a serious shortcoming for signal processing applications, where data is typically sampled at a constant interval. These attributes must be managed by the user with custom programming, which is error-prone and time-consuming.

MATLAB is a procedural programming language (with some object-oriented programming capabilities), so it cannot automatically update variables in response to input changes as one might want for simulations or exploratory data analysis. Consider, for example, the following fragment:

  t = 1:100;
  y = log(t);

If variable t changes, e.g. t = 100:1000, the user must manually re-evaluate y to obtain the updated result. The MathWorks offers a supplementary package, Simulink, that partially automates these tasks for systems modeling and simulation applications.

MATLAB doesn't support references, which makes it very difficult to implement data structures which contain indirections, such as open hash tables, linked lists, trees, and various other common computer science data structures. In addition, it makes the use of objects cumbersome, because every change in the object creates a new object instead of changing the current one ("this" or "self" is Copy-On-Write). the only way to change an object property is to "overwrite" the old object by the new object that was created after the property change, like this:

  obj = do_something(obj);

instead of the more common idioms in other languages:

  obj.do_something();

Despite these shortcomings, MATLAB continues to be employed in many technical analysis applications, though several viable competitors are emerging.

See also

For a list of programs similar to MATLAB, see the list of numerical analysis software.

Toolboxes and other add-ons:

External links

Template:Wikibookspar

de:MATLAB es:MATLAB fa:متلب fr:Matlab it:MATLAB lt:MATLAB ja:MATLAB pl:MATLAB pt:Matlab ru:MATLAB sv:MATLAB vi:MATLAB th:แมตแล็บ tr:Matlab zh:MATLAB