MATLAB is a environment for scientific computing that is ideal for computations that require extensive use of arrays and graphical analysis of data.
To start Matlab on the University of Maryland's glue network and perform some basic operations, try
|
where :~: is the unix shell prompt and >> is the MATLAB environment prompt. At this point, MATLAB is ready to begin a computational session. Hitting the return key generates a MATLAB prompt on a new line; we will make extensive use of MATLAB comments which are found after the % character.
It is important to understand the relationship between the MATLAB operating environment and your computer's file system stucture because
Try the following commands on your computer:
|
The default data type in MATLAB is a double-precision array. While
this data type tends to be used most often in computations, several
other data types also are important:
|
Arrays are defined and accessed based on the following concepts:
Now consider some basic Matlab operations/functions:
|
The arithmetic operations +, -, /, and * by default correspond to true matrix computations.
Now consider some basic Matlab operations/functions:
|
Using a text editor, create a file called CpAir.m with the following text
|
Now try running the script
|
Now let us check to make sure T is in the proper range
Download Cpscript.m
|
Now try running the script
|
To maximize the utility of the program we've written, we put it
into the form of a function:
Download Cpfun.m
|
Now try using the function
|
Download the following function
Download lorfun.m
|
and then try
|
|
Up to now we have focued on procedural programming techniques: translating numerical algorithms into source code in a very "top-down" approach.
Now we shift our focus to the data being processed
In Matlab, we have encountered such data types as
We now examine the struct data type which allows storage of set of variables of possibly different type in one variable:
A.val = 1.3
A.units = 'm'
Definition: A class defines the data fields of particular stuctures (objects) of that class and the methods that can be used on objects of that class
Example: We consider developing the "dimensional" class, with two data fields:
numerical value : A.val
units : A.units
and the following methods
Because plus.m and times.m are already defined in Matlab as functions that normally operate on variables of type double, we will be overloading these methods by our new definitions.
We define our new class in Matlab by creating a new directory named @Dimensional. Inside this directory (folder), we place the constructor function Dimensional.m
function B = Dimensional(val,units) % Dimensional class contructor method A.val = val; A.units = units; B = class(A,'Dimensional'); |
a display method display.m
function display(A) % Dimensional class display method disp([num2str(A.val),' ',A.units]) |
the overloaded addition function plus.m
function C = plus(A,B) % Dimensional class plus (+) method if strcmp(A.units,B.units) val = A.val+B.val; units = A.units; C = Dimensional(val,units); return end error('Units mismatch') |
and the overloaded term-by-term product method times.m
function C = times(A,B) % Dimensional class times (.*) method val = A.val.*B.val; if strcmp(A.units,B.units) units = [A.units,'^2']; else units = [A.units,' ',B.units]; end C = Dimensional(val,units); |
Consider then, defining
x = 1.3 m
y = -2.0 m
z = 5.0 sec
using the calls to the constructor method Dimensional.m
>> x = Dimensional(1.3,'m') 1.3 m >> y = Dimensional(-2,'m') -2 m >> z = Dimensional(5,'sec') 5 sec |
and noting the format of the echoed output, defined by the display.m method. We then try out representative calculations with our overloaded methods:
>> x + y -0.7 m >> x .* y -2.6 m^2 >> x .* z 6.5 m sec >> x + z ??? Error using ==> Dimensional/plus Units mismatch >> x==y ??? Error using ==> == Function '==' not defined for variables of class 'Dimensional'. |
Note that MATLAB returns an error as a result of the final command because the definition of equivalence (eq.m) has not yet been defined for objects of the Dimensional class.
The Dimensional class was constructed from scratch by defining a new data type and its associated methods. We can also build new classes from existing classes, where this child class inherits all the data fields and methods of the parent class; new data fields can be added to the child as well as methods. The new methods either overload some or all of the parent's methods, and entirely new methods can be defined. In either case, the methods defined for the child class can access data fields defined specifically for the child class or fields that were inherited from the parent.
Consider, for example, changing the constructor method for the Dimensional class to the following:
function B = Dimensional(val,units) % Dimensional class contructor method A.val = val; A.units = units; B = class(A,'Dimensional',Comparable); |
The Dimensional class now inherits the single method of the Comparable class, which is the equivalence operator (==). Therefore, we now can determine if two objects are the same:
>> x == y ans = 0 >> x == x ans = 1 |